[
  {
    "path": ".gitignore",
    "content": ".bundle/\nlog/*.log\npkg/\nspec/test_app\n.idea\n.ruby-gemset\n.ruby-version\n"
  },
  {
    "path": ".rspec",
    "content": "--color\n--require spec_helper\n"
  },
  {
    "path": "Gemfile",
    "content": "source 'https://rubygems.org'\n\n# Declare your gem's dependencies in notifyor.gemspec.\n# Bundler will treat runtime dependencies like base dependencies, and\n# development dependencies will be added by default to the :development group.\ngemspec\n\n# Declare any dependencies that are still in development here instead of in\n# your gemspec. These might include edge Rails or gems from your path or\n# Git. Remember to move these dependencies to your gemspec before releasing\n# your gem to rubygems.org.\n\n# To use a debugger\n# gem 'byebug', group: [:development, :test]\n\n"
  },
  {
    "path": "MIT-LICENSE",
    "content": "Copyright 2016 Erwin Schens\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\nLIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\nOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\nWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# Notifyor\n## [![](http://i.imgur.com/FrRacwt.png)]()\nGet realtime notifications (growl messages) on your desktop if something happens in your rails app.\nNotifyor publishes changes to a redis channel. Subscription is performed from your local machine via a ssh tunnel.\n\nSimply put:\nVery growl. Such notifications. Much Notifyor.\n\n## Installation\n\nAdd this line to your Gemfile:\n\n```ruby\ngem 'notifyor', '~> 0.8.1'\n```\n\nOr install it via rubygems if you just need the CLI.\n\n```bash\ngem install notifyor\n```\n\nAnd then execute:\n\n    $ bundle install\n\n## Getting started\nRun the bundle command to install it.\nAfter you install Notifyor create a new file **config/initializers/notifyor.rb** (a rails generator will be available soon for this task). Add the following content to your initializer.\n```ruby\nNotifyor.configure do |config|\n    #config.redis_connection = Redis.new\nend\n```\n\n### Linux\nIf you want to receive growl notifications on a linux system you have to install the *libnotify-bin*.\n\n```bash\nsudo apt-get install libnotify-bin \n```\n\n### Mac OS X\nNotifyor runs out of the box on Mac OS X. Lucky you.\n\n### Windows\nCurrently not supported (see roadmap)\n\n## Usage\n\n### Plugin\nNotifyor can be plugged into your models by adding the *notifyor* method to your class.\n```ruby\nclass SomeClass < ActiveRecord::Base\n    notifyor\nend\n```\nBy just including the method without options, notifyor will send notifications for the following events: *create*, *update* and *destroy*. The default message is the i18n key **notifyor.model.[create | update | destroy]** you have to provide in your application.\nIf you want to customize this message you can provide the following option to the notifyor method:\n```ruby\nclass SomeClass < ActiveRecord::Base\n    notifyor messages: {\n      create: -> (model) { \"My Message for model #{model.id}.\" },\n      update: -> (model) { \"My Message for model #{model.id}.\" },\n      destroy: -> (model) { \"My Message for model #{model.id}.\" }\n  }\nend \n```\n\nIf you dont want to receive a notification for a certain action just add the **only** option to notifyor.\n```ruby\nclass SomeClass < ActiveRecord::Base\n    notifyor only: [:create]\nend \n```\n\nThe default channel on which notifyor publishes messages is 'notifyor'. This can be changed via the **channel** option.\nYou can provide multiple channels and notifyor will publish messages on them. (See the channel option on the CLI to subscribe to those channels).\n```ruby\nclass SomeClass < ActiveRecord::Base\n    notifyor channels: ['channel1', 'channel2']\nend\n```\n\n### CLI\nThe CLI can be used independently just install the gem and run following command. \n\n```bash\nnotify_me --ssh-host some_host --ssh-port some_port --ssh-user some_user\n```\n#### Arguments for the CLI\n - **ssh-host** Provide the ssh host to which notifyor should connect to. (Default is localhost)\n - **ssh-port** Provide the ssh port on which notifyor should connect to. (Default is 22)\n - **ssh-user** Provide the ssh user for your remote server. (Please use ssh keys so that you just have to provide the *ssh-host*. -> Security reasons)\n - **tunnel-port** The tunnel port on which ssh will establish the connection. (Default is 2000)\n - **redis-port** The port of your redis server (Default is 6379)\n - **channel** Listen on another channel. Every message received on this channel will be displayed in a growl notification. (Default is notifyor)\n\n**If you dont provide a ssh host notifyor will subscribe from your local redis and display them.**\n\nEvery notify_me instance is an individual subscriber so multiple users can receive growl messages.\n\n## Roadmap\n- Notifications for multiple OS (currently only Mac OS X and Linux systems with libnotify-lib installed.)\n- Provide own logo in the growl notification\n- Specs\n- Documentation\n\n## Development\n\nAfter checking out the repo, run `bundle install` to install dependencies. \n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\n1. Fork it ( https://github.com/[my-github-username]/notifyor/fork )\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n"
  },
  {
    "path": "Rakefile",
    "content": "begin\n  require 'bundler/setup'\nrescue LoadError\n  puts 'You must `gem install bundler` and `bundle install` to run rake tasks'\nend\n\nrequire 'rdoc/task'\n\nRDoc::Task.new(:rdoc) do |rdoc|\n  rdoc.rdoc_dir = 'rdoc'\n  rdoc.title    = 'Notifyor'\n  rdoc.options << '--line-numbers'\n  rdoc.rdoc_files.include('README.rdoc')\n  rdoc.rdoc_files.include('lib/**/*.rb')\nend\n\n\n\n\n\n\nBundler::GemHelper.install_tasks\n\nrequire 'rake/testtask'\n\nRake::TestTask.new(:test) do |t|\n  t.libs << 'lib'\n  t.libs << 'test'\n  t.pattern = 'test/**/*_test.rb'\n  t.verbose = false\nend\n\n\ntask default: :test\n"
  },
  {
    "path": "bin/notify_me",
    "content": "#! /usr/bin/env ruby\nrequire 'notifyor/cli'\nrequire 'notifyor/remote/connection'\n\nbegin\n  cli = ::Notifyor::CLI.new\n  cli.parse\n  cli.check_notifications\nrescue => e\n  STDERR.puts e.message\n  STDERR.puts e.backtrace.join(\"\\n\")\n  exit 1\nend"
  },
  {
    "path": "lib/notifyor/cli.rb",
    "content": "require 'notifyor/version'\nrequire 'optparse'\n\nmodule Notifyor\n  class CLI\n\n    def parse\n      # Default configuration.\n      ENV['ssh_host'] = 'localhost'\n      ENV['ssh_port'] = '22'\n      ENV['ssh_tunnel_port'] = '2000'\n      ENV['ssh_redis_port'] = '6379'\n\n      ::OptionParser.new do |opts|\n        opts.banner = 'Usage: notify_me [options]'\n\n        opts.on('-v', '--version',\n                'Show the current version of this gem') do\n          puts \"Notifyor Version: #{::Notifyor::VERSION}\"; exit\n        end\n\n        opts.on('--ssh-host host', 'Provide the host address to your deployment/remote server') do |host|\n          ENV['ssh_host'] = host\n        end\n\n        opts.on('--ssh-port port', 'Provide the ssh port for the deployment/remote server') do |port|\n          ENV['ssh_port'] = port\n        end\n\n        opts.on('--ssh-user user', 'Provide the ssh user for the deployment/remote server') do |user|\n          ENV['ssh_user'] = user\n        end\n\n        opts.on('--tunnel-port tunnel_port', 'Provide the ssh user for the deployment/remote server') do |tunnel_port|\n          ENV['ssh_tunnel_port'] = tunnel_port\n        end\n\n        opts.on('--redis-port redis_port', 'Provide the ssh user for the deployment/remote server') do |redis_port|\n          ENV['ssh_redis_port'] = redis_port\n        end\n\n        opts.on('--channel [channel]', 'Provide channel on which notifyor should listen.') do |channel|\n          ENV['channel'] = channel\n        end\n      end.parse!\n    end\n\n    def check_notifications\n      connection = Notifyor::Remote::Connection.new\n      begin\n        connection.build_tunnel\n        connection.build_redis_tunnel_connection\n        connection.subscribe_to_redis\n      rescue => e\n        STDOUT.write \"Could not establish SSH tunnel. Reason: #{e.message}\"\n      end\n    end\n\n  end\nend"
  },
  {
    "path": "lib/notifyor/configuration.rb",
    "content": "require 'redis'\nrequire 'connection_pool'\nmodule Notifyor\n  class Configuration\n    attr_accessor :redis_connection\n\n    def initialize\n      @redis_connection = ::Redis.new\n    end\n  end\nend"
  },
  {
    "path": "lib/notifyor/growl/adapters/libnotify_notifier.rb",
    "content": "module Notifyor\n  module Growl\n    module Adapters\n      module LibnotifyNotifier\n        extend self\n\n        def create_growl(title, message)\n          %x(notify-send '#{title}' '#{message}')\n        end\n\n      end\n    end\n  end\nend"
  },
  {
    "path": "lib/notifyor/growl/adapters/terminal_notifier.rb",
    "content": "require 'terminal-notifier'\nmodule Notifyor\n  module Growl\n    module Adapters\n      module TerminalNotifier\n        extend self\n\n        def create_growl(title, message)\n          ::TerminalNotifier.notify(message, :title => title)\n        end\n\n      end\n    end\n  end\nend"
  },
  {
    "path": "lib/notifyor/growl.rb",
    "content": "require 'notifyor/util/os_analyzer'\nmodule Notifyor\n  module Growl\n    extend self\n\n    def adapter\n      return @adapter if @adapter\n      self.adapter =\n          case ::Notifyor::Util::OSAnalyzer.os\n            when :macosx\n              :terminal_notifier\n            when :linux\n              :libnotify_notifier\n            when :unix\n              :libnotify_notifier\n            else\n              raise 'Operating system not recognized.'\n          end\n      @adapter\n    end\n\n    def adapter=(adapter_name)\n      case adapter_name\n        when Symbol, String\n          require \"notifyor/growl/adapters/#{adapter_name}\"\n          @adapter = Notifyor::Growl::Adapters.const_get(\"#{adapter_name.to_s.split('_').collect(&:capitalize).join}\")\n        else\n          raise \"Missing adapter #{adapter_name}\"\n      end\n    end\n\n    def create_growl(title, message)\n      adapter.create_growl(title, message)\n    end\n\n  end\nend"
  },
  {
    "path": "lib/notifyor/plugin.rb",
    "content": "require 'active_support'\nrequire 'redis'\n\nmodule Notifyor\n  module Plugin\n    extend ::ActiveSupport::Concern\n\n    included do\n    end\n\n    module ClassMethods\n      def notifyor(options = {})\n        configure_plugin(options)\n      end\n\n      def configure_plugin(options = {})\n        configuration = default_configuration.deep_merge(options)\n        publish_channels = configuration[:channels] || ['notifyor']\n        append_callbacks(configuration, publish_channels)\n      end\n\n      def append_callbacks(configuration, publish_channels)\n        publish_channels.each do |channel|\n          configuration[:only].each do |action|\n            case action\n              when :create\n                self.after_commit -> { ::Notifyor.configuration.redis_connection.publish channel, configuration[:messages][:create].call(self) }, on: :create, if: -> { configuration[:only].include? :create }\n              when :update\n                self.after_commit -> { ::Notifyor.configuration.redis_connection.publish channel, configuration[:messages][:update].call(self) }, on: :update, if: -> { configuration[:only].include? :update }\n              when :destroy\n                self.before_destroy -> { ::Notifyor.configuration.redis_connection.publish channel, configuration[:messages][:destroy].call(self) }, if: -> { configuration[:only].include? :destroy }\n              else\n                #nop\n            end\n          end\n        end\n      end\n\n      def default_configuration\n        {\n            only: [:create, :destroy, :update],\n            messages: {\n                create: -> (model) { I18n.t('notifyor.model.create') },\n                update: -> (model) { I18n.t('notifyor.model.update') },\n                destroy: -> (model) { I18n.t('notifyor.model.destroy') }\n            }\n        }\n      end\n\n    end\n  end\nend"
  },
  {
    "path": "lib/notifyor/remote/connection.rb",
    "content": "require 'json'\nrequire 'redis'\nrequire 'notifyor/growl'\nrequire 'notifyor/util/formatter'\nrequire 'net/ssh/gateway'\nmodule Notifyor\n  module Remote\n    class Connection\n\n      def initialize\n        @ssh_host = ENV['ssh_host'] || 'localhost'\n        @ssh_port = ENV['ssh_port'] || '22'\n        @ssh_user = ENV['ssh_user']\n        @tunnel_port = ENV['ssh_tunnel_port'] || '2000'\n        @redis_port = ENV['ssh_redis_port'] || '6379'\n        @redis_channel = ENV['channel'] || 'notifyor'\n        @ssh_gateway = nil\n        @redis_tunnel_connection = nil\n      end\n\n      def build_tunnel\n        unless ['127.0.0.1', 'localhost'].include? @ssh_host\n          @ssh_gateway = Net::SSH::Gateway.new(@ssh_host, @ssh_user, port: @ssh_port)\n          @ssh_gateway.open('127.0.0.1', @redis_port, @tunnel_port)\n        end\n      end\n\n      def build_redis_tunnel_connection\n        redis_port = (['127.0.0.1', 'localhost'].include? @ssh_host) ? @redis_port : @tunnel_port\n        @redis_tunnel_connection = Redis.new(port: redis_port)\n      end\n\n      def subscribe_to_redis\n        @redis_tunnel_connection.subscribe(@redis_channel) do |on|\n          on.message do |channel, msg|\n            STDERR.write \"INFO - Message received on channel: #{channel} \\n\"\n            growl_message(msg)\n          end\n        end\n      end\n\n      def growl_message(message)\n        ::Notifyor::Growl.create_growl(\"Notifyor\", message) unless Notifyor::Util::Formatter.squish!(message).empty?\n      end\n    end\n  end\nend"
  },
  {
    "path": "lib/notifyor/util/formatter.rb",
    "content": "module Notifyor\n  module Util\n    module Formatter\n      extend self\n\n      def squish!(string)\n        string.gsub!(/\\A[[:space:]]+/, '')\n        string.gsub!(/[[:space:]]+\\z/, '')\n        string.gsub!(/[[:space:]]+/, ' ')\n        string\n      end\n\n    end\n  end\nend"
  },
  {
    "path": "lib/notifyor/util/os_analyzer.rb",
    "content": "require 'rbconfig'\nmodule Notifyor\n  module Util\n    module OSAnalyzer\n      extend self\n      def os\n        host_os = RbConfig::CONFIG['host_os']\n        case host_os\n          when /mswin|msys|mingw|cygwin|bccwin|wince|emc/\n            :windows\n          when /darwin|mac os/\n            :macosx\n          when /linux/\n            :linux\n          when /solaris|bsd/\n            :unix\n          else\n            raise \"unknown os: #{host_os.inspect}\"\n        end\n      end\n    end\n  end\nend\n"
  },
  {
    "path": "lib/notifyor/version.rb",
    "content": "module Notifyor\n  VERSION = \"0.8.1\"\nend\n"
  },
  {
    "path": "lib/notifyor.rb",
    "content": "$LOAD_PATH.unshift(File.dirname(__FILE__))\n\nrequire 'notifyor/plugin'\nrequire 'notifyor/configuration'\n\nmodule Notifyor\n  class << self\n    attr_accessor :configuration\n  end\n\n  def self.configure\n    self.configuration ||= Configuration.new\n    yield(configuration) if block_given?\n  end\n\n  ActiveRecord::Base.send(:include, ::Notifyor::Plugin) if defined?(ActiveRecord)\nend"
  },
  {
    "path": "notifyor.gemspec",
    "content": "$:.push File.expand_path(\"../lib\", __FILE__)\n\nrequire \"notifyor/version\"\n\nGem::Specification.new do |s|\n  s.name = \"notifyor\"\n  s.version = Notifyor::VERSION\n\n  s.authors = [\"Erwin Schens\"]\n  s.email = [\"erwin.schens@qurasoft.de\"]\n  s.homepage = \"https://github.com/ndea/notifyer\"\n  s.summary = \"Get realtime notifications on your desktop if something happens in your Rails app.\"\n  s.description = \"Notifyer creates growl notifications on your desktop if something happens in your Rails app.\"\n  s.license = \"MIT\"\n  s.executables   = [\"notify_me\"]\n  s.require_paths = [\"lib\"]\n\n  s.files = Dir[\"{app,config,db,lib,bin}/**/*\", \"MIT-LICENSE\", \"Rakefile\", \"README.rdoc\"]\n  s.test_files = Dir[\"spec/**/*\"]\n\n  s.add_dependency 'redis'\n  s.add_dependency 'connection_pool'\n  s.add_dependency 'terminal-notifier'\n  s.add_dependency 'net-ssh'\n  s.add_dependency 'net-ssh-gateway'\n\n  s.add_development_dependency \"sqlite3\"\n  s.add_development_dependency \"rspec\", \"~> 3.4.0\"\nend\n"
  },
  {
    "path": "spec/spec_helper.rb",
    "content": "# This file was generated by the `rspec --init` command. Conventionally, all\n# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.\n# The generated `.rspec` file contains `--require spec_helper` which will cause\n# this file to always be loaded, without a need to explicitly require it in any\n# files.\n#\n# Given that it is always loaded, you are encouraged to keep this file as\n# light-weight as possible. Requiring heavyweight dependencies from this file\n# will add to the boot time of your test suite on EVERY test run, even for an\n# individual file that may not need all of that loaded. Instead, consider making\n# a separate helper file that requires the additional dependencies and performs\n# the additional setup, and require it from the spec files that actually need\n# it.\n#\n# The `.rspec` file also contains a few flags that are not defaults but that\n# users commonly want.\n#\n# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration\nRSpec.configure do |config|\n  # rspec-expectations config goes here. You can use an alternate\n  # assertion/expectation library such as wrong or the stdlib/minitest\n  # assertions if you prefer.\n  config.expect_with :rspec do |expectations|\n    # This option will default to `true` in RSpec 4. It makes the `description`\n    # and `failure_message` of custom matchers include text for helper methods\n    # defined using `chain`, e.g.:\n    #     be_bigger_than(2).and_smaller_than(4).description\n    #     # => \"be bigger than 2 and smaller than 4\"\n    # ...rather than:\n    #     # => \"be bigger than 2\"\n    expectations.include_chain_clauses_in_custom_matcher_descriptions = true\n  end\n\n  # rspec-mocks config goes here. You can use an alternate test double\n  # library (such as bogus or mocha) by changing the `mock_with` option here.\n  config.mock_with :rspec do |mocks|\n    # Prevents you from mocking or stubbing a method that does not exist on\n    # a real object. This is generally recommended, and will default to\n    # `true` in RSpec 4.\n    mocks.verify_partial_doubles = true\n  end\n\n# The settings below are suggested to provide a good initial experience\n# with RSpec, but feel free to customize to your heart's content.\n=begin\n  # These two settings work together to allow you to limit a spec run\n  # to individual examples or groups you care about by tagging them with\n  # `:focus` metadata. When nothing is tagged with `:focus`, all examples\n  # get run.\n  config.filter_run :focus\n  config.run_all_when_everything_filtered = true\n\n  # Allows RSpec to persist some state between runs in order to support\n  # the `--only-failures` and `--next-failure` CLI options. We recommend\n  # you configure your source control system to ignore this file.\n  config.example_status_persistence_file_path = \"spec/examples.txt\"\n\n  # Limits the available syntax to the non-monkey patched syntax that is\n  # recommended. For more details, see:\n  #   - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/\n  #   - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/\n  #   - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode\n  config.disable_monkey_patching!\n\n  # This setting enables warnings. It's recommended, but in some cases may\n  # be too noisy due to issues in dependencies.\n  config.warnings = true\n\n  # Many RSpec users commonly either run the entire suite or an individual\n  # file, and it's useful to allow more verbose output when running an\n  # individual spec file.\n  if config.files_to_run.one?\n    # Use the documentation formatter for detailed output,\n    # unless a formatter has already been configured\n    # (e.g. via a command-line flag).\n    config.default_formatter = 'doc'\n  end\n\n  # Print the 10 slowest examples and example groups at the\n  # end of the spec run, to help surface which specs are running\n  # particularly slow.\n  config.profile_examples = 10\n\n  # Run specs in random order to surface order dependencies. If you find an\n  # order dependency and want to debug it, you can fix the order by providing\n  # the seed, which is printed after each run.\n  #     --seed 1234\n  config.order = :random\n\n  # Seed global randomization in this process using the `--seed` CLI option.\n  # Setting this allows you to use `--seed` to deterministically reproduce\n  # test failures related to randomization by passing the same `--seed` value\n  # as the one that triggered the failure.\n  Kernel.srand config.seed\n=end\nend\n"
  }
]