stubsvc 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 365970a5c55f1b31d4a056e34aa48361691881e3
4
+ data.tar.gz: ad5f8bb7bdbfe5d429ae940f3cf6f350ced92ef9
5
+ SHA512:
6
+ metadata.gz: 9bfa0cfb56edae1be4a996ba474c4c24b5c3983dc59e883ff746a7508baf9dacd28ebf5f438a8528f03f9662a776f6c6e4bcb324c49f3b74016da32f3533f9a2
7
+ data.tar.gz: 518f79ba958809cb894055a63d9fb0b7cb19faeacd7ddfa935e098bcfe3f04bb724cbce076dfb41f4fc01ec5f9c0884874f5e2018bfa7106ad03ec49076b47ee
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /stubs/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,69 @@
1
+ AllCops:
2
+ # Include gemspec and Rakefile
3
+ Include:
4
+ - '**/*.gemspec'
5
+ - '**/*.podspec'
6
+ - '**/*.jbuilder'
7
+ - '**/*.rake'
8
+ - '**/Gemfile'
9
+ - '**/Rakefile'
10
+ - '**/Capfile'
11
+ - '**/Guardfile'
12
+ - '**/Podfile'
13
+ - '**/Thorfile'
14
+ - '**/Vagrantfile'
15
+ Exclude:
16
+ - 'vendor/**/*'
17
+ - 'stubs/**/*'
18
+
19
+ # Checks formatting of special comments
20
+ CommentAnnotation:
21
+ Keywords:
22
+ - TODO
23
+ - FIXME
24
+ - OPTIMIZE
25
+ - HACK
26
+ - REVIEW
27
+
28
+ ########################################
29
+ # Style Cops
30
+
31
+ Style/Documentation:
32
+ Enabled: false
33
+
34
+ Style/FileName:
35
+ Enabled: false
36
+
37
+ Style/AlignParameters:
38
+ EnforcedStyle: with_fixed_indentation
39
+
40
+ Style/RegexpLiteral:
41
+ Enabled: false
42
+
43
+ Style/EmptyLinesAroundBlockBody:
44
+ Enabled: false
45
+
46
+ Style/RaiseArgs:
47
+ Enabled: false
48
+
49
+ Style/DoubleNegation:
50
+ Enabled: false
51
+
52
+ ########################################
53
+ # Lint Cops
54
+
55
+ Lint/Eval:
56
+ Enabled: false
57
+
58
+ ########################################
59
+ # Metrics Cops
60
+
61
+ Metrics/LineLength:
62
+ Max: 110
63
+
64
+ Metrics/MethodLength:
65
+ CountComments: false # count full line comments?
66
+ Max: 20
67
+
68
+ Metrics/ClassLength:
69
+ Max: 120
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
data/CHANGELOG ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in stubsvc.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # Stubsvc
2
+
3
+ **Version: 0.1.1**
4
+
5
+ A Ruby gem that makes it easy to stub out calls to external web services using Webmock.
6
+
7
+ Many applications are service-oriented or distributed in some way. Testing can be problematic in such an environment. `Stubsvc` makes it easy for unit tests to stub calls to external services, wrapping the Webmock `stub_request` method.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'stubsvc'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install stubsvc
24
+
25
+ ## Usage
26
+
27
+ We've only tested using RSpec. However it may work just fine with Minitest, Test::Unit or others. Please let us know your results and pull requests are always welcome.
28
+
29
+ ### Configuration
30
+
31
+ Configure your external services using environment variables as follows:
32
+
33
+ SVC_BASE_URI='http://localhost:5000/api/v4'
34
+
35
+ Where `SVC` in the variable name above is the symbol you will use for your service, and the value points to the base URI for the particular service you are testing.
36
+
37
+ For example, let's say I have a file service that handles uploads and downloads and all things file-related. I could define the environment variable as follows:
38
+
39
+ FS_BASE_URI='http://localhost:5000/api/v4'
40
+
41
+ We recommend using Brandon Keeper's [dotenv](https://github.com/bkeepers/dotenv) gem for defining your base URIs.
42
+
43
+ ### Stubbing
44
+
45
+ There are two ways to stub a service call:
46
+
47
+ 1. `Stubsvc.stub_action`
48
+ 2. `Stubsvc.stub_verb`
49
+
50
+ #### `stub_action`
51
+
52
+ `Stubsvc.stub_action` is for Rails [ActiveResource](https://github.com/rails/activeresource) type actions which accepts the following arguments:
53
+
54
+ Stubsvc.stub_action(service, action, resource, return_hash, status)
55
+
56
+ * service - Lowercase symbol matching the environment variable defined above minus `_BASE_URI`
57
+ * action - One of `:index, :show, :create, :update, :destroy, :where, :find`
58
+ * resource - Symbol representing the remote resource. It must be snake-cased. For example, if the resource is `RootFolder`, the symbol would be `:root_folder`
59
+ * return_hash - (optional) A hash representation of the JSON data you want returned for this call. If you don't care what gets returned and leave this argument out, it will default to `"{}"`
60
+ * status - (optional) The HTTP status code that should be returned. Defaults to 200
61
+
62
+ #### `stub_verb`
63
+
64
+ `Stubsvc.stub_verb` is for stubbing standard HTTP verbs which accepts the following arguments:
65
+
66
+ Stubsvc.stub_verb(service, http_verb, uri, return_hash, status)
67
+
68
+ * service - Lowercase symbol matching the environment variable defined above minus `_BASE_URI`
69
+ * http_verb - One of `:get, :post, :put, :delete`
70
+ * uri - A full or partial URI. It can be a regular expression or a string.
71
+ * return_hash - (optional) A hash representation of the JSON data you want returned for this call. If you don't care what gets returned and leave this argument out, it will default to `"{}"`
72
+ * status - (optional) The HTTP status code that should be returned. Defaults to 200
73
+
74
+ ### RSpec Example
75
+
76
+ Let's say I have a model called `Article`. Whenever an `Article` instance is created it automatically creates a `Folder` instance by calling my external file service.
77
+
78
+ We want to unit test the `create` method for `ArticlesController`, without having to fire up the external service. We can stub out the call to the file service `Folder.create` method like this:
79
+
80
+ RSpec.describe ArticlesController, type: :controller do
81
+ let(:valid_params) do
82
+ {
83
+ article: {
84
+ name: 'Test Article'
85
+ }
86
+ }
87
+ end
88
+
89
+ it 'persists an Article instance' do
90
+ Stubsvc.stub(:fs, :post, :folder, valid_params.merge(id: 1), 201)
91
+ expect do
92
+ post :create, valid_params
93
+ end.to change(Article, :count).by(1)
94
+ end
95
+ end
96
+
97
+ This effectively stubs the `POST` call to `http://localhost:5000/api/v4/folders` to simply return this JSON:
98
+
99
+ {"article":{"name":"Test Article","id":1}}
100
+
101
+ ...with a status of `201`.
102
+
103
+ ## Development
104
+
105
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
106
+
107
+ To 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).
108
+
109
+ ## Contributing
110
+
111
+ 1. Fork it ( https://github.com/[my-github-username]/stubsvc/fork )
112
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
113
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
114
+ 4. Push to the branch (`git push origin my-new-feature`)
115
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require 'bundler/gem_tasks'
2
+ begin
3
+ require 'midwire_common/rake_tasks'
4
+ rescue StandardError => e
5
+ puts(">>> Can't load midwire_common/rake_tasks.")
6
+ puts(">>> Did you 'bundle exec'?: #{e.message}")
7
+ exit
8
+ end
9
+
10
+ begin
11
+ require 'rspec/core/rake_task'
12
+ RSpec::Core::RakeTask.new(:spec)
13
+ task default: :spec
14
+ rescue LoadError
15
+ puts 'No RSpec for you!'
16
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "stubsvc"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,71 @@
1
+ require 'webmock'
2
+ require 'oj'
3
+ require 'active_support/core_ext/string/inflections'
4
+
5
+ module Stubsvc
6
+ class Stubber
7
+ include WebMock::API
8
+
9
+ attr_reader :service
10
+
11
+ def initialize(service)
12
+ @service = service
13
+ end
14
+
15
+ def stub_action(action, resource, return_hash = {}, status = 200)
16
+ http_verb = verb_from_action(action)
17
+ regex = regex_for_action(resource, action, return_hash)
18
+ stub_request(http_verb, regex).to_return(
19
+ status: status.to_i,
20
+ body: Oj.dump(return_hash)
21
+ )
22
+ end
23
+
24
+ def stub_verb(verb, uri, return_hash = {}, status = 200)
25
+ http_verb = verb_from_action(verb)
26
+ pattern = "#{base_uri}#{uri}"
27
+ stub_request(http_verb, pattern).to_return(
28
+ status: status.to_i,
29
+ body: Oj.dump(return_hash)
30
+ )
31
+ end
32
+
33
+ def base_uri
34
+ return @base_uri if @base_uri
35
+ env_key = "#{service.upcase}_BASE_URI"
36
+ @base_uri = ENV.fetch(env_key).sub(%r{/+$}, '')
37
+ end
38
+
39
+ private
40
+
41
+ def verb_from_action(action)
42
+ case action.to_sym
43
+ when :show, :index, :where, :find, :get then :get
44
+ when :create, :post then :post
45
+ when :update, :put then :put
46
+ when :destroy, :delete then :delete
47
+ else
48
+ fail("Unknown action or http verb specified: '#{action}'")
49
+ end
50
+ end
51
+
52
+ def regex_for_action(item_type, action, return_hash = {})
53
+ pattern = "#{base_uri}/#{item_type.to_s.pluralize.downcase}"
54
+ if requires_id_in_path?(action)
55
+ pattern << "/#{return_hash.fetch(:id, 1)}*"
56
+ else
57
+ pattern << '*'
58
+ end
59
+ Regexp.new(pattern)
60
+ end
61
+
62
+ def requires_id_in_path?(action)
63
+ case action.to_sym
64
+ when :index, :create, :where, :post then false
65
+ when :show, :destroy, :update, :find, :put, :delete then true
66
+ else
67
+ fail("Can't figure out if the path contains an id: '#{action}'")
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,3 @@
1
+ module Stubsvc
2
+ VERSION = "0.1.1"
3
+ end
data/lib/stubsvc.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'stubsvc/version'
2
+
3
+ module Stubsvc
4
+ class << self
5
+ def root
6
+ Pathname.new(File.dirname(__FILE__)).parent
7
+ end
8
+
9
+ def stub_action(service, action, resource, return_hash, status, stubber = Stubber.new(service))
10
+ stubber.stub_action(action, resource, return_hash, status)
11
+ end
12
+
13
+ def stub_verb(service, verb, uri, return_hash, status, stubber = Stubber.new(service))
14
+ stubber.stub_verb(verb, uri, return_hash, status)
15
+ end
16
+ end
17
+
18
+ autoload :Stubber, 'stubsvc/stubber'
19
+ end
data/stubsvc.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'stubsvc/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'stubsvc'
8
+ spec.version = Stubsvc::VERSION
9
+ spec.authors = ['Chris Blackburn']
10
+ spec.email = ['205abc7e@opayq.com']
11
+
12
+ spec.summary = 'Stub out calls to external web services.'
13
+ spec.description = <<-stop
14
+ A Ruby gem that makes it easy to stub out calls to external web services using Webmock.
15
+ stop
16
+ spec.homepage = ''
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.9'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'pry'
26
+ spec.add_development_dependency 'pry-nav'
27
+ spec.add_development_dependency 'rspec', '~> 3.2'
28
+ spec.add_development_dependency 'rest-client', '~> 1.8'
29
+ spec.add_development_dependency 'simplecov', '~> 0.10'
30
+
31
+ spec.add_dependency 'midwire_common', '~> 0.1.16'
32
+ spec.add_dependency 'webmock', '~> 1.22'
33
+ spec.add_dependency 'activesupport', '~> 4.2'
34
+ spec.add_dependency 'oj', '~> 2.12'
35
+ spec.add_dependency 'rack', '~> 1.6'
36
+ end
metadata ADDED
@@ -0,0 +1,226 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stubsvc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Blackburn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-nav
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rest-client
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.8'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.8'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.10'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.10'
111
+ - !ruby/object:Gem::Dependency
112
+ name: midwire_common
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.1.16
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.1.16
125
+ - !ruby/object:Gem::Dependency
126
+ name: webmock
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.22'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.22'
139
+ - !ruby/object:Gem::Dependency
140
+ name: activesupport
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '4.2'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '4.2'
153
+ - !ruby/object:Gem::Dependency
154
+ name: oj
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '2.12'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '2.12'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rack
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '1.6'
174
+ type: :runtime
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '1.6'
181
+ description: |2
182
+ A Ruby gem that makes it easy to stub out calls to external web services using Webmock.
183
+ email:
184
+ - 205abc7e@opayq.com
185
+ executables: []
186
+ extensions: []
187
+ extra_rdoc_files: []
188
+ files:
189
+ - ".gitignore"
190
+ - ".rspec"
191
+ - ".rubocop.yml"
192
+ - ".travis.yml"
193
+ - CHANGELOG
194
+ - Gemfile
195
+ - README.md
196
+ - Rakefile
197
+ - bin/console
198
+ - bin/setup
199
+ - lib/stubsvc.rb
200
+ - lib/stubsvc/stubber.rb
201
+ - lib/stubsvc/version.rb
202
+ - stubsvc.gemspec
203
+ homepage: ''
204
+ licenses: []
205
+ metadata: {}
206
+ post_install_message:
207
+ rdoc_options: []
208
+ require_paths:
209
+ - lib
210
+ required_ruby_version: !ruby/object:Gem::Requirement
211
+ requirements:
212
+ - - ">="
213
+ - !ruby/object:Gem::Version
214
+ version: '0'
215
+ required_rubygems_version: !ruby/object:Gem::Requirement
216
+ requirements:
217
+ - - ">="
218
+ - !ruby/object:Gem::Version
219
+ version: '0'
220
+ requirements: []
221
+ rubyforge_project:
222
+ rubygems_version: 2.4.5
223
+ signing_key:
224
+ specification_version: 4
225
+ summary: Stub out calls to external web services.
226
+ test_files: []