pokey 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 902eedd7df7d24c0ba7371ab75ed91dc59d1bf3f
4
- data.tar.gz: 153fd390ac187d01c62b426bdda0c360925aa6f7
3
+ metadata.gz: 1fdaf56b0fdd1c671eb53bfc977869c3e2d69bd0
4
+ data.tar.gz: d44fe9670c6768ecdb1243324984f7133b337c53
5
5
  SHA512:
6
- metadata.gz: 81d59aeed034302148895a9c828ec20c4958bf7316db5568a88c4baf8ce250fb98cb23022459f85c639f30b7736f714a2700aafdccfc370753d221eb420c647d
7
- data.tar.gz: b93105e18878dc227d46cb478f8f60775373f041aac5afb66ca270a6be01bb8473d174134607a952e2d79b0a89d00e1117491cf090a89237c63ae07e04f43ecb
6
+ metadata.gz: 82fcb403c1bd62b1a9d58dfb87a188ff1a01215f7fd4a1a7072e197590c1b8e3ee4c53ccfa7a2b9c9b942e2de451702389fbceca4a7691df0ccc0080a75ed137
7
+ data.tar.gz: dc43a6c15adca297a6a676cfe5a549824d261d71aaa3f2214edc9dedbaad5972261ef417099a40d801c7ef435c813bb26bc7016f65c0d264f9a3f41145976cf1
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Pokey
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/pokey.svg)](http://badge.fury.io/rb/pokey) [![Code Climate](https://codeclimate.com/github/ccallebs/pokey/badges/gpa.svg)](https://codeclimate.com/github/ccallebs/pokey)
4
+
3
5
  Pokey is a Ruby gem designed to easily simulate webhooks / other HTTP requests common
4
6
  to a production environment.
5
7
 
@@ -21,35 +23,40 @@ Or install it yourself as:
21
23
 
22
24
  ## Usage
23
25
 
24
- Create a new file in your initializers directory named `pokey.rb`. Here
25
- is where you'll be defining your API calls.
26
+ If you're using Rails, create the initializer by running:
27
+
28
+ $ rails g pokey:install
29
+
30
+ Otherwise, create a new file in your initializers directory named `pokey.rb`. You'll be
31
+ setting the default hook directory and (optionally) defining custom hooks here.
26
32
 
27
33
  ``` RUBY
28
34
  Pokey.configure do |config|
35
+ config.hook_dir = "app/pokey" # Defaults to app/pokey
36
+ config.run_on = [:development, :qa] # Only set environments you want pokey to
37
+ # simulate requests for. If not using Rails,
38
+ # this currently has no effect
39
+
29
40
  config.add_hook do |hook|
30
41
  hook.destination = "/my/webhook/endpoint"
31
42
  hook.data = {
32
43
  name: "Test endpoint",
33
44
  endpoint_id: 1
34
45
  }
46
+ hook.interval = 20 # in seconds
47
+ hook.http_method = :post # supports GET and POST for right now
35
48
  end
36
-
37
- Pokey::Scheduler.commit! # This isn't necessary in the config block
38
- # but is a decent enough place to put it. This
39
- # signifies that all endpoints have been added
40
- # and Rufus will begin to schedule hooks.
41
49
  end
42
50
  ```
43
51
 
44
- In addition, you can also define classes inside a designated directory to
45
- streamline creation of Pokey events.
52
+ If you would like to add many hooks to your project, you can place them in the `hook_dir`
53
+ you specified in the initializer. If you're using Rails, you can run
46
54
 
47
- ``` RUBY
48
- # initializers/pokey.rb
49
- Pokey.configure do |config|
50
- config.hook_dir = "app/pokey" # Defaults to app/pokey
51
- end
55
+ $ rails g pokey:hook sendgrid_event
56
+
57
+ to create a new `Pokey::Hook` template. Otherwise, create a file like the following:
52
58
 
59
+ ``` RUBY
53
60
  # app/pokey/sendgrid_event_hook.rb
54
61
  class SendgridEventHook < Pokey::Hook
55
62
  def destination
@@ -111,7 +118,7 @@ end
111
118
  ```
112
119
 
113
120
  As your data will inevitably get more complex to simulate actual events,
114
- Pokey::Hook sub-classes are the preferred way to declare endpoints.
121
+ `Pokey::Hook` subclasses are preferred over ad-hoc hook definitions.
115
122
 
116
123
 
117
124
  ## Contributing
@@ -0,0 +1,16 @@
1
+ require 'rails/generators'
2
+
3
+ module Pokey
4
+ module Generators
5
+ class HookGenerator < Rails::Generators::NamedBase
6
+ desc "Generates a Pokey hook"
7
+ check_class_collision suffix: "Hook"
8
+
9
+ source_root File.expand_path("../templates", __FILE__)
10
+
11
+ def create_hook_file
12
+ template "hook.rb", "#{Pokey.hook_dir}/#{file_path.tr('/', '_')}_hook.rb"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ class <%= class_name %>Hook < Pokey::Hook
2
+ def destination
3
+ end
4
+
5
+ def data
6
+ end
7
+
8
+ def interval
9
+ end
10
+
11
+ def http_method
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails/generators'
2
+
3
+ module Pokey
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ def copy_initializer
9
+ template "initializer.rb.erb", "config/initializers/pokey.rb"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ Pokey.configure do |config|
2
+ config.hook_dir = "app/hooks"
3
+
4
+ config.run_on = [:development, :qa] # Only set environments you want pokey
5
+ # to simulate requests for
6
+
7
+ # Add some hooks on-the-fly here. If your data is somewhat complicated, add
8
+ # a hook instead with `rails g pokey:hook hook_name`
9
+ # config.add_hook do |hook|
10
+ # hook.destination = "/my/webhook/endpoint"
11
+ # hook.data = {
12
+ # name: "Test endpoint"
13
+ # }
14
+ # hook.interval = 120 # in seconds
15
+ # hook.http_method = :post
16
+ # end
17
+ end
@@ -2,15 +2,22 @@ require "pokey/version"
2
2
  require "pokey/configuration"
3
3
  require "pokey/hook"
4
4
  require "pokey/hooks"
5
+ require "pokey/logger"
5
6
  require "pokey/request"
6
7
  require "pokey/scheduler"
7
8
 
9
+ if defined?(Rails)
10
+ require "generators/rails/install/install_generator"
11
+ require "generators/rails/hook/hook_generator"
12
+ end
13
+
8
14
  module Pokey
9
15
  class << self
10
16
  attr_writer :configuration
11
17
 
12
18
  def configure
13
19
  yield(configuration)
20
+ Pokey::Scheduler.run! if should_run?
14
21
  end
15
22
 
16
23
  def configuration
@@ -24,5 +31,15 @@ module Pokey
24
31
  def hook_dir
25
32
  configuration.hook_dir
26
33
  end
34
+
35
+ def should_run?
36
+ current_env.nil? || configuration.run_on.map(&:to_s).include?(current_env)
37
+ end
38
+
39
+ def current_env
40
+ if defined?(Rails)
41
+ Rails.env
42
+ end
43
+ end
27
44
  end
28
45
  end
@@ -1,9 +1,10 @@
1
1
  class Pokey::Configuration
2
- attr_accessor :hook_dir, :hooks
2
+ attr_accessor :hook_dir, :hooks, :run_on
3
3
 
4
4
  def initialize
5
5
  @hook_dir = "app/pokey"
6
6
  @hooks = []
7
+ @run_on = [:development]
7
8
  end
8
9
 
9
10
  def add_hook(&block)
@@ -26,14 +26,6 @@ class Pokey::Hooks
26
26
  @@hooks << klass.new
27
27
  end
28
28
 
29
- def self.add_from_file(file_path)
30
- require file_path
31
-
32
- base_name = File.basename(file_path, ".rb")
33
- klass = Helpers::Inflector.constantize(base_name.split('_').collect(&:capitalize).join)
34
- add_from_class(klass)
35
- end
36
-
37
29
  def self.add_from_dir(directory)
38
30
  directory += "/" if directory[-1] != "/"
39
31
 
@@ -0,0 +1,12 @@
1
+ require 'logging'
2
+
3
+ Logging.logger.root.appenders = Logging.appenders.stdout
4
+ Logging.logger.root.level = :info
5
+
6
+ class Pokey::Logger
7
+ attr_reader :log
8
+
9
+ def initialize
10
+ @log = Logging.logger[self]
11
+ end
12
+ end
@@ -7,6 +7,7 @@ class Pokey::Request
7
7
 
8
8
  def initialize(destination, http_method = :post, data = {})
9
9
  @destination, @http_method, @data = destination, http_method, data
10
+ @log = Pokey::Logger.new.log
10
11
  end
11
12
 
12
13
  def raw_request
@@ -22,6 +23,10 @@ class Pokey::Request
22
23
  end
23
24
 
24
25
  response = http.request(request)
26
+
27
+ if response
28
+ @log.info "Made request to #{uri.host}:#{uri.port} with following data: #{@data}"
29
+ end
25
30
  end
26
31
  end
27
32
 
@@ -5,7 +5,7 @@ class Pokey::Scheduler
5
5
  @rufus ||= Rufus::Scheduler.new
6
6
  end
7
7
 
8
- def self.commit!
8
+ def self.run!
9
9
  scheduler = new
10
10
 
11
11
  Pokey::Hooks.add_from_dir(Pokey.hook_dir)
@@ -1,3 +1,3 @@
1
1
  module Pokey
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
@@ -16,6 +16,7 @@ Gem::Specification.new do |spec|
16
16
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
17
 
18
18
  spec.add_runtime_dependency 'rufus-scheduler', '~> 3.1.2'
19
+ spec.add_runtime_dependency 'logging'
19
20
 
20
21
  spec.add_development_dependency "bundler", "~> 1.7"
21
22
  spec.add_development_dependency "rake", "~> 10.0"
@@ -35,4 +35,38 @@ describe Pokey do
35
35
  end
36
36
  end
37
37
  end
38
+
39
+ describe "#should_run?" do
40
+ context "when configured for current Rails.env" do
41
+ before do
42
+ allow(Pokey).to receive(:current_env).and_return("development")
43
+
44
+ Pokey.configure do |config|
45
+ config.run_on = [:development, :qa]
46
+ end
47
+ end
48
+
49
+ subject { Pokey.should_run? }
50
+
51
+ it "returns true" do
52
+ expect(subject).to eq true
53
+ end
54
+ end
55
+
56
+ context "when not configured for current Rails.env" do
57
+ before do
58
+ allow(Pokey).to receive(:current_env).and_return("production")
59
+
60
+ Pokey.configure do |config|
61
+ config.run_on = [:development, :qa]
62
+ end
63
+ end
64
+
65
+ subject { Pokey.should_run? }
66
+
67
+ it "returns false" do
68
+ expect(subject).to eq false
69
+ end
70
+ end
71
+ end
38
72
  end
@@ -31,18 +31,6 @@ RSpec.describe Pokey::Hooks do
31
31
  end
32
32
  end
33
33
 
34
- describe '.add_from_file' do
35
- let(:file) { './spec/pokey/sample_hooks/sample_hook.rb' }
36
-
37
- let(:subject) do
38
- Pokey::Hooks.add_from_file(file)
39
- end
40
-
41
- it 'successfully adds hook to list' do
42
- expect { subject }.to change { Pokey::Hooks.all.length }.by(1)
43
- end
44
- end
45
-
46
34
  describe '.add_from_dir' do
47
35
  let(:dir) { './spec/pokey/sample_hooks/' }
48
36
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pokey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chuck Callebs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-09 00:00:00.000000000 Z
11
+ date: 2015-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rufus-scheduler
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 3.1.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: logging
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -111,11 +125,16 @@ files:
111
125
  - LICENSE.txt
112
126
  - README.md
113
127
  - Rakefile
128
+ - lib/generators/rails/hook/hook_generator.rb
129
+ - lib/generators/rails/hook/templates/hook.rb
130
+ - lib/generators/rails/install/install_generator.rb
131
+ - lib/generators/rails/install/templates/initializer.rb.erb
114
132
  - lib/pokey.rb
115
133
  - lib/pokey/configuration.rb
116
134
  - lib/pokey/helpers/inflector.rb
117
135
  - lib/pokey/hook.rb
118
136
  - lib/pokey/hooks.rb
137
+ - lib/pokey/logger.rb
119
138
  - lib/pokey/request.rb
120
139
  - lib/pokey/scheduler.rb
121
140
  - lib/pokey/version.rb