mocking_bird 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a6b4da41969a3b859d95426bcf785a00fd03892b
4
+ data.tar.gz: 246ba452a26416f616b58f639c25b98efefce80a
5
+ SHA512:
6
+ metadata.gz: fa848b794f9fea7e4d0d575872be253325a3b7d38fa312108f093d9f8c6ac3c5255aa5abd6b4ed7731e4826fa5cfc3bb62400021b76cc9c6d0091c277c00517d
7
+ data.tar.gz: 14d8effa82872a9230c63aa7bfebfe212c974edbb6a77595fbcc27f0682d682bc78b03363b83c20b105cbe9851073c12e03d19337b50e49bbf5593606a336ea2
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .idea/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mocking_bird.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Brandon Sislow
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Brandon Sislow
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,160 @@
1
+ # MockingBird
2
+
3
+ This is a singleton class that helps auto load json_api_client_mocks.
4
+
5
+ Pain point:
6
+ Setting up client mocks for json_api_client tests can be messy with situations like:
7
+
8
+ ```ruby
9
+ MyApi::Client::User.set_test_results([some data...], {<some conditions...>)
10
+ MyApi::Client::User.set_test_results([some data...], {<some conditions...>)
11
+ MyApi::Client::User.set_test_results([some data...], {<some conditions...>)
12
+ MyOtherApi::Client::Customer.set_test_results([some data...], {<some conditions...>)
13
+ MyOtherApi::Client::Customer.set_test_results([some data...], {<some conditions...>)
14
+ MyOtherApi::Client::Customer.set_test_results([some data...], {<some conditions...>)
15
+ ```
16
+ Placing this anywhere is ugly and time consuming.
17
+
18
+ MockingBird attempts to combine this into a more fixture-like convention by using the file structure of your
19
+ mocks to build client based test results that are accessible, or just trying to mimic fixtures.
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ gem 'mocking_bird'
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install mocking_bird
34
+
35
+ ## Usage
36
+
37
+ #### File System Structure
38
+
39
+ In your mock directory you would create the following structure:
40
+
41
+ ```
42
+ |
43
+ |-mocks
44
+ |-my_api
45
+ |-customer
46
+ create.yml
47
+ delete.yml
48
+ read.yml
49
+ update.yml
50
+ |-user
51
+ create.yml
52
+ delete.yml
53
+ read.yml
54
+ update.yml
55
+ |-my_other_api
56
+ |-order
57
+ create.yml
58
+ delete.yml
59
+ read.yml
60
+ update.yml
61
+ |-invocie
62
+ create.yml
63
+ delete.yml
64
+ read.yml
65
+ update.yml
66
+ ```
67
+
68
+ This will load mocks for `MyApi::Client::Customer, MyApi::Client::User, MyOtherApi::Client::Order, and MyOtherApi::Client::Invoice`
69
+ The mock files are grouped by action, but you can name them anything and they will be accessible by this actions(shown later).
70
+
71
+ **currently only YAMl files are accepted, with JSON coming soon.**
72
+
73
+ #### Mock File Structure
74
+
75
+ The file structure for each mock file is:
76
+
77
+ ```yaml
78
+ # user/create.yml
79
+ <mock_name>:
80
+ conditions:
81
+ name: 'test_uers'
82
+ response:
83
+ id: 1
84
+ name: 'test_uers'
85
+ ```
86
+
87
+ Example:
88
+
89
+ ```yaml
90
+ # my_api/users/create.yml
91
+ test_user:
92
+ conditions:
93
+ name: 'test_uers'
94
+ contact: 'test user'
95
+ email_address: 'test@test.com'
96
+ phone_number: '2062222222'
97
+ address_id: nil
98
+ credit_cards: []
99
+ address:
100
+ address_line1: '111 test ave'
101
+ addresS_line2: ''
102
+ city: 'Seattle'
103
+ state: 'WA'
104
+ count: nil
105
+ postal_code: '98109'
106
+ results:
107
+ id: 1
108
+ name: 'test_uers'
109
+ contact: 'test user'
110
+ email_address: 'test@test.com'
111
+ phone_number: '2062222222'
112
+ address_id: nil
113
+ credit_cards: []
114
+ address:
115
+ id: 1
116
+ address_line1: '111 test ave'
117
+ addresS_line2: ''
118
+ city: 'Seattle'
119
+ state: 'WA'
120
+ count: nil
121
+ postal_code: '98109'
122
+ ```
123
+
124
+ **Note:** MockingBird looks for `conditions:` and `:results` to set up the mocks properly. It will fail without them.
125
+
126
+
127
+ #### Building the mocks
128
+
129
+ Setting up the mocks is done by calling
130
+
131
+ ```ruby
132
+ MockingBird::setup_mocks(:path => Rails.root.join('test','mocks')
133
+ ```
134
+
135
+ #### Fetching mock data
136
+
137
+ Fetching the conditions and results you've set up is done by calling on MockingBird::Mocker:
138
+
139
+ ```ruby
140
+ # get the test user mock
141
+ MockingBird::Mocker.my_api.user.create.test_user
142
+ # Hash (2 element(s))
143
+ # conditions => <HashWithIndifferntAccess> - your conditions for test_user in create.yml file
144
+ # results => <HashWithIndifferntAccess> - your results for test_user in create.yml file
145
+ MockingBird::Mocker.my_other_api.invoice.create.invoice_1
146
+ ```
147
+
148
+ ## Contributing
149
+
150
+ 1. Fork it ( https://github.com/[my-github-username]/mocking_bird/fork )
151
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
152
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
153
+ 4. Push to the branch (`git push origin my-new-feature`)
154
+ 5. Create a new Pull Request
155
+ =======
156
+ mocking_bird
157
+ ============
158
+
159
+ Mock manager for json_api_client_mocks
160
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,5 @@
1
+ <atlassian-ide-plugin>
2
+ <project-configuration id="1">
3
+ <servers id="2" />
4
+ </project-configuration>
5
+ </atlassian-ide-plugin>
@@ -0,0 +1,8 @@
1
+ require "mocking_bird/version"
2
+
3
+ module MockingBird
4
+ autoload :Mocker, 'mocking_bird/mocker'
5
+ autoload :Flock, 'mocking_bird/flock'
6
+ autoload :Bird, 'mocking_bird/bird'
7
+ autoload :Mock, 'mocking_bird/mock'
8
+ end
@@ -0,0 +1,52 @@
1
+ require_relative 'mock'
2
+ module MockingBird
3
+
4
+ class Bird
5
+
6
+ attr_accessor :mocks, :service, :klass
7
+
8
+ def initialize(opts = {})
9
+ @service = opts.fetch(:service)
10
+ @path = opts.fetch(:path)
11
+ @klass = @path.split("/").last.classify
12
+ @mocks = {}
13
+
14
+ load_actions if @path.present?
15
+ end
16
+
17
+ def client_klass
18
+ @client_klass ||= "#{@service}::Client::#{@klass}".constantize
19
+ end
20
+
21
+ private
22
+
23
+ def load_actions
24
+ regex = File.join(@path,'*.yml')
25
+ Dir.glob(regex).each do | mock_file |
26
+ load_mocks(mock_file)
27
+ end
28
+ end
29
+
30
+ def load_mocks(mock_file)
31
+ action = File.basename(mock_file, ".*")
32
+ mock = Mock.new(mock_file, client_klass)
33
+ @mocks[action.downcase.to_sym] = mock
34
+ end
35
+
36
+ def method_missing(method, *args, &block)
37
+ if match = method.to_s.match(/^(.*)=$/)
38
+ raise NoMethodError
39
+ elsif has_mock?(method)
40
+ @mocks[method]
41
+ else
42
+ raise NoMethodError
43
+ end
44
+ end
45
+
46
+ def has_mock?(type)
47
+ @mocks.has_key? type
48
+ end
49
+
50
+
51
+ end
52
+ end
@@ -0,0 +1,43 @@
1
+ require_relative 'bird'
2
+ module MockingBird
3
+ class Flock
4
+ attr_accessor :service, :birds
5
+
6
+ def initialize(path = '')
7
+ @birds = {}
8
+ @path = path
9
+ @service = path.split("/").last.classify
10
+ load if @path.present?
11
+ end
12
+
13
+ private
14
+
15
+ def load
16
+ Dir.entries(@path).each do |dir|
17
+ spawn_birds(dir)
18
+ end
19
+ end
20
+
21
+ def spawn_birds(dir)
22
+ full_path = File.join(@path,dir)
23
+ if File.directory?(full_path) && dir != ".." && dir != "."
24
+ bird = Bird.new(:service => @service, :path => full_path)
25
+ @birds[bird.klass.downcase.to_sym] = bird
26
+ end
27
+ end
28
+
29
+ def method_missing(method, *args, &block)
30
+ if match = method.to_s.match(/^(.*)=$/)
31
+ raise NoMethodError
32
+ elsif bird = has_bird?(method)
33
+ @birds[method]
34
+ else
35
+ raise NoMethodError
36
+ end
37
+ end
38
+
39
+ def has_bird?(klass)
40
+ @birds.has_key?(klass)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,48 @@
1
+ require 'yaml'
2
+ module MockingBird
3
+ class Mock
4
+ attr_accessor :mock_set
5
+
6
+ def initialize(file, klass)
7
+ @file = file
8
+ @mock_set = {}
9
+ @klass = klass
10
+ load_mock if @file.present?
11
+ end
12
+
13
+
14
+ private
15
+
16
+ def load_mock
17
+ case File.extname(@file)
18
+ when '.yml' then load_yaml
19
+ else nil
20
+ end
21
+ end
22
+
23
+ def load_yaml
24
+ objects = YAML::load_file(@file)
25
+ objects = objects.with_indifferent_access if objects.present?
26
+ Hash(objects|| nil).each do |k,v|
27
+ @mock_set[k.to_sym] = @klass.set_test_results(v[:results],v[:conditions]).first if v[:results] && v[:conditions]
28
+ end
29
+ @mock_set
30
+ end
31
+
32
+ def method_missing(method, *args, &block)
33
+ if match = method.to_s.match(/^(.*)=$/)
34
+ raise NoMethodError
35
+ elsif has_mock?(method)
36
+ @mock_set[method]
37
+ else
38
+ raise NoMethodError
39
+ end
40
+ end
41
+
42
+ def has_mock?(name)
43
+ @mock_set.has_key?(name.to_sym)
44
+ end
45
+
46
+
47
+ end
48
+ end
@@ -0,0 +1,47 @@
1
+ require_relative 'flock'
2
+ module MockingBird
3
+ class Mocker
4
+ cattr_accessor :path, :flocks
5
+
6
+ class << self
7
+ def setup_mocks(options = {})
8
+ # iterate over mocks and create birds in teh flock
9
+ @flocks = {}
10
+ @path = options.fetch(:path, default_directory)
11
+ load_mocks if @path
12
+ end
13
+
14
+ def load_mocks
15
+ Dir.entries(@path).each do |dir|
16
+
17
+ full_path = File.join(@path,dir)
18
+ if File.directory?(full_path) && dir != ".." && dir != "."
19
+ flock = Flock.new(full_path)
20
+ @flocks[flock.service.downcase.to_sym] = flock
21
+ end
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def default_directory
28
+ Rails.root.join('test','mocks')
29
+ end
30
+
31
+ def method_missing(method, *args, &block)
32
+ if match = method.to_s.match(/^(.*)=$/)
33
+ super
34
+ elsif has_flock?(method)
35
+ @flocks[method]
36
+ else
37
+ nil
38
+ end
39
+ end
40
+
41
+ def has_flock?(name)
42
+ @flocks.keys.include? name
43
+ end
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module MockingBird
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mocking_bird/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mocking_bird"
8
+ spec.version = MockingBird::VERSION
9
+ spec.authors = ["Brandon Sislow"]
10
+ spec.email = ["bsislow@avvo.com"]
11
+ spec.summary = %q{Mock manager for cross services mocks}
12
+ spec.description = %q{A mock manager to load mocks based on convention, similar to fixtures, while being fetchable}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "json_api_client_mock"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mocking_bird
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Brandon Sislow
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json_api_client_mock
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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
+ description: A mock manager to load mocks based on convention, similar to fixtures,
56
+ while being fetchable
57
+ email:
58
+ - bsislow@avvo.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - atlassian-ide-plugin.xml
70
+ - lib/mocking_bird.rb
71
+ - lib/mocking_bird/bird.rb
72
+ - lib/mocking_bird/flock.rb
73
+ - lib/mocking_bird/mock.rb
74
+ - lib/mocking_bird/mocker.rb
75
+ - lib/mocking_bird/version.rb
76
+ - mocking_bird.gemspec
77
+ homepage: ''
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.2.2
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Mock manager for cross services mocks
101
+ test_files: []