vending-machine 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +19 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +155 -0
  6. data/Rakefile +14 -0
  7. data/lib/vend.rb +46 -0
  8. data/lib/vend/errors.rb +12 -0
  9. data/lib/vend/has_resources.rb +34 -0
  10. data/lib/vend/modules/declarative_setters.rb +74 -0
  11. data/lib/vend/modules/finders.rb +5 -0
  12. data/lib/vend/modules/paths.rb +37 -0
  13. data/lib/vend/remote_resource.rb +46 -0
  14. data/lib/vend/resource.rb +100 -0
  15. data/lib/vend/resource_collection.rb +72 -0
  16. data/lib/vend/resources/brand.rb +4 -0
  17. data/lib/vend/resources/contact.rb +4 -0
  18. data/lib/vend/resources/customer.rb +8 -0
  19. data/lib/vend/resources/inventory.rb +14 -0
  20. data/lib/vend/resources/outlet.rb +4 -0
  21. data/lib/vend/resources/payment_type.rb +4 -0
  22. data/lib/vend/resources/price_book_entry.rb +10 -0
  23. data/lib/vend/resources/product.rb +30 -0
  24. data/lib/vend/resources/product_type.rb +7 -0
  25. data/lib/vend/resources/register.rb +9 -0
  26. data/lib/vend/resources/register_sale_payment.rb +6 -0
  27. data/lib/vend/resources/register_sale_product.rb +12 -0
  28. data/lib/vend/resources/sale.rb +17 -0
  29. data/lib/vend/resources/supplier.rb +6 -0
  30. data/lib/vend/resources/tax.rb +13 -0
  31. data/lib/vend/resources/user.rb +4 -0
  32. data/lib/vend/response.rb +9 -0
  33. data/lib/vend/store.rb +103 -0
  34. data/lib/vend/version.rb +3 -0
  35. data/lib/vending-machine.rb +1 -0
  36. data/spec/resource_spec.rb +22 -0
  37. data/spec/resource_urls_spec.rb +59 -0
  38. data/spec/spec_helper.rb +8 -0
  39. data/spec/store_spec.rb +21 -0
  40. data/spec/support/url_assertions.rb +23 -0
  41. data/spec/support/webmock.rb +53 -0
  42. data/vending-machine.gemspec +46 -0
  43. metadata +238 -0
@@ -0,0 +1,3 @@
1
+ module Vend
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/vend'
@@ -0,0 +1,22 @@
1
+ require_relative "spec_helper"
2
+
3
+ module Vend
4
+ describe Resource do
5
+ describe "#has_many" do
6
+ it "returns the same instance of ResourceCollection" do
7
+ class Widget < RemoteResource
8
+ attribute :name, String
9
+ end
10
+ resource = Class.new(RemoteResource) do
11
+ attribute :id, Integer
12
+ has_many :widgets, ->{ Widget }
13
+ end
14
+ i = resource.new widgets: [{ name: "Knob" }]
15
+ widgets = i.widgets
16
+ widgets.send(:records).size.must_equal 1
17
+ i.widgets << Widget.new(name: "Flange")
18
+ widgets.send(:records).size.must_equal 2
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,59 @@
1
+ ###
2
+ # Describes which API URLs are used for collections and resources
3
+ #
4
+ # Consider this the definitive spec for what URL vending machine
5
+ # will be retrieving resources from.
6
+ #
7
+ require_relative 'spec_helper'
8
+
9
+ module Vend
10
+ describe "Resource URLs" do
11
+ extend UrlAssertions
12
+
13
+ {
14
+ Customer: {
15
+ index: "/api/customers",
16
+ show: "/api/1.0/customer/:id",
17
+ create: "/api/customers",
18
+ update: "/api/customers"
19
+ },
20
+ Inventory: {
21
+ show: "/api/1.0/inventory/:id",
22
+ },
23
+ PaymentType: {
24
+ index: "/api/payment_types"
25
+ },
26
+ ProductType: {
27
+ show: "/api/1.0/product_type/:id"
28
+ },
29
+ Product: {
30
+ index: "/api/products",
31
+ show: "/api/1.0/product/:id",
32
+ create: "/api/products",
33
+ update: "/api/products",
34
+ destroy: "/api/products/:id"
35
+ },
36
+ Sale: {
37
+ index: "/api/register_sales",
38
+ show: "/api/1.0/register_sale/:id",
39
+ create: "/api/register_sales"
40
+ },
41
+ Supplier: {
42
+ show: "/api/1.0/supplier/:id"
43
+ },
44
+ Register: {
45
+ index: "/api/registers",
46
+ },
47
+ Tax: {
48
+ index: "/api/taxes",
49
+ create: "/api/taxes"
50
+ },
51
+ User: {
52
+ index: "/api/users",
53
+ show: "/api/1.0/user/:id"
54
+ }
55
+ }.each do |class_name, verb_routes|
56
+ assert_resource_urls("Vend::#{class_name.to_s}".constantize, verb_routes)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,8 @@
1
+ require "minitest/autorun"
2
+ require "turn"
3
+
4
+ require_relative "support/webmock"
5
+ require_relative "support/url_assertions"
6
+
7
+ $LOAD_PATH.unshift "./lib"
8
+ require "vend"
@@ -0,0 +1,21 @@
1
+ require_relative "spec_helper"
2
+
3
+ module Vend
4
+ describe Store do
5
+ describe "#url" do
6
+ it "should be the store api endpoint" do
7
+ store = Vend::Store.new('test-store', :a, :a)
8
+ store.send(:url).must_equal "https://test-store.vendhq.com/api"
9
+ end
10
+ end
11
+
12
+ describe "#faraday" do
13
+ it "should be an authorized Faraday client" do
14
+ store = Vend::Store.new('test-store', "abc", "123")
15
+ conn = store.send(:faraday)
16
+ assert auth = conn.headers['Authorization']
17
+ assert_equal Faraday::Request::BasicAuthentication.header("abc", "123"), auth
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ module UrlAssertions
2
+ def assert_resource_urls(klass, verb_routes)
3
+ verb_routes.each do |verb, route|
4
+ it "#{klass.name}##{verb} maps to #{route}" do
5
+ route.gsub!(/^\/api\//,'') # Remove the /api
6
+ case verb
7
+ when :index
8
+ klass.index_path.must_equal route
9
+ when :show
10
+ klass.new(id: ":id").show_path.must_equal route
11
+ when :create
12
+ klass.create_path.must_equal route
13
+ when :update
14
+ klass.new(id: ":id").update_path.must_equal route
15
+ when :destroy
16
+ klass.new(id: ":id").destroy_path.must_equal route
17
+ else
18
+ raise "I don't know how to #{verb}"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,53 @@
1
+ require "addressable/uri"
2
+ require "json"
3
+ require "webmock/minitest"
4
+
5
+ WebMock.disable_net_connect!
6
+
7
+ ##
8
+ # WebMock request expectations.
9
+ module RequestExpectations
10
+
11
+ def self.included(klass)
12
+ klass.before { @expected_requests = [] }
13
+ klass.after { @expected_requests.each { |sr| assert_requested(sr) } }
14
+ end
15
+
16
+ def stub_and_assert_request(*parameters)
17
+ stub_request(*parameters).tap do |request|
18
+ @expected_requests << request
19
+ end
20
+ end
21
+
22
+ def expect_request(options)
23
+ rack_method_override!(options, :patch)
24
+
25
+ url = options.fetch(:url)
26
+ parameters = options[:parameters]
27
+ method = options.fetch(:method)
28
+ request_body = options[:request_body]
29
+ request_headers = options[:request_headers]
30
+ response_body = options[:response_body]
31
+ response_headers = options[:response_headers]
32
+
33
+ if parameters
34
+ url = Addressable::URI.parse(url).tap do |url|
35
+ url.query_values = parameters
36
+ end.to_s
37
+ end
38
+
39
+ request = {}
40
+ request[:body] = request_body if request_body
41
+ request[:headers] = request_headers if request_headers
42
+
43
+ response = {status: options[:status] || 200}
44
+ response[:body] = JSON.generate(response_body) if response_body
45
+ response[:headers] = response_headers if response_headers
46
+
47
+ stub_and_assert_request(method, url).
48
+ with(request).
49
+ to_return(response)
50
+ end
51
+ end
52
+
53
+ MiniTest::Spec.send(:include, RequestExpectations)
@@ -0,0 +1,46 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vend/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "vending-machine"
8
+ gem.version = Vend::VERSION
9
+ gem.authors = ["Mal Curtis"]
10
+ gem.email = ["mal@mal.co.nz"]
11
+ gem.description = %q{Client for the Vend API}
12
+ gem.summary = %q{Exposes the Vend API as an object model}
13
+ gem.homepage = "https://github.com/snikch/vending-machine"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(spec)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ ##
21
+ # Runtime dependencies.
22
+
23
+ # Faraday: HTTP client.
24
+ gem.add_runtime_dependency "faraday"
25
+ gem.add_runtime_dependency "faraday_middleware"
26
+
27
+ # Addressable: URI implementation.
28
+ gem.add_runtime_dependency "addressable"
29
+
30
+ # Virtus: declare attributes on plain Ruby objects.
31
+ gem.add_runtime_dependency "virtus", "~> 0.5.3"
32
+
33
+ ##
34
+ # Development dependencies.
35
+
36
+ # MiniTest: unit test / spec library.
37
+ gem.add_development_dependency "minitest"
38
+ gem.add_development_dependency "ansi"
39
+ gem.add_development_dependency "turn"
40
+
41
+ # Rake: build tool.
42
+ gem.add_development_dependency "rake"
43
+
44
+ # WebMock: HTTP request stubs and expectations.
45
+ gem.add_development_dependency "webmock"
46
+ end
metadata ADDED
@@ -0,0 +1,238 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vending-machine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mal Curtis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: faraday_middleware
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: addressable
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: virtus
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.5.3
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.5.3
78
+ - !ruby/object:Gem::Dependency
79
+ name: minitest
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: ansi
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: turn
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: rake
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: webmock
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ description: Client for the Vend API
159
+ email:
160
+ - mal@mal.co.nz
161
+ executables: []
162
+ extensions: []
163
+ extra_rdoc_files: []
164
+ files:
165
+ - .gitignore
166
+ - Gemfile
167
+ - LICENSE
168
+ - LICENSE.txt
169
+ - README.md
170
+ - Rakefile
171
+ - lib/vend.rb
172
+ - lib/vend/errors.rb
173
+ - lib/vend/has_resources.rb
174
+ - lib/vend/modules/declarative_setters.rb
175
+ - lib/vend/modules/finders.rb
176
+ - lib/vend/modules/paths.rb
177
+ - lib/vend/remote_resource.rb
178
+ - lib/vend/resource.rb
179
+ - lib/vend/resource_collection.rb
180
+ - lib/vend/resources/brand.rb
181
+ - lib/vend/resources/contact.rb
182
+ - lib/vend/resources/customer.rb
183
+ - lib/vend/resources/inventory.rb
184
+ - lib/vend/resources/outlet.rb
185
+ - lib/vend/resources/payment_type.rb
186
+ - lib/vend/resources/price_book_entry.rb
187
+ - lib/vend/resources/product.rb
188
+ - lib/vend/resources/product_type.rb
189
+ - lib/vend/resources/register.rb
190
+ - lib/vend/resources/register_sale_payment.rb
191
+ - lib/vend/resources/register_sale_product.rb
192
+ - lib/vend/resources/sale.rb
193
+ - lib/vend/resources/supplier.rb
194
+ - lib/vend/resources/tax.rb
195
+ - lib/vend/resources/user.rb
196
+ - lib/vend/response.rb
197
+ - lib/vend/store.rb
198
+ - lib/vend/version.rb
199
+ - lib/vending-machine.rb
200
+ - spec/resource_spec.rb
201
+ - spec/resource_urls_spec.rb
202
+ - spec/spec_helper.rb
203
+ - spec/store_spec.rb
204
+ - spec/support/url_assertions.rb
205
+ - spec/support/webmock.rb
206
+ - vending-machine.gemspec
207
+ homepage: https://github.com/snikch/vending-machine
208
+ licenses: []
209
+ post_install_message:
210
+ rdoc_options: []
211
+ require_paths:
212
+ - lib
213
+ required_ruby_version: !ruby/object:Gem::Requirement
214
+ none: false
215
+ requirements:
216
+ - - ! '>='
217
+ - !ruby/object:Gem::Version
218
+ version: '0'
219
+ required_rubygems_version: !ruby/object:Gem::Requirement
220
+ none: false
221
+ requirements:
222
+ - - ! '>='
223
+ - !ruby/object:Gem::Version
224
+ version: '0'
225
+ requirements: []
226
+ rubyforge_project:
227
+ rubygems_version: 1.8.23
228
+ signing_key:
229
+ specification_version: 3
230
+ summary: Exposes the Vend API as an object model
231
+ test_files:
232
+ - spec/resource_spec.rb
233
+ - spec/resource_urls_spec.rb
234
+ - spec/spec_helper.rb
235
+ - spec/store_spec.rb
236
+ - spec/support/url_assertions.rb
237
+ - spec/support/webmock.rb
238
+ has_rdoc: