kounta_rest 0.0.1

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,16 @@
1
+ require "helper"
2
+
3
+ describe Kounta::Product do
4
+
5
+ subject { Kounta::Company.new.site(985).product(1) }
6
+
7
+ it "should be able to to test for tags" do
8
+ subject.tags_include?("Rosebud").should be(false)
9
+ subject.tags_include?("Fragile").should be(true)
10
+ end
11
+
12
+ it "should have a resource path" do
13
+ subject.resource_path.should eq({companies: 162, products: 1})
14
+ end
15
+
16
+ end
@@ -0,0 +1,95 @@
1
+ require "helper"
2
+
3
+ describe Kounta::Resource do
4
+
5
+ subject { Kounta::Resource }
6
+
7
+ before :each do
8
+ subject.has_one(:address, Kounta::Address, {:company_id => :id}, lambda { |klass, item_id| {companies: klass.id, addresses: item_id} })
9
+ subject.has_many :addresses, Kounta::Address, {:company_id => :id}, lambda { |klass| {companies: klass.id, addresses: nil} }
10
+ @instance = subject.new({:id => 345})
11
+ end
12
+
13
+ it "should populate details if created with a hash" do
14
+ @instance.id.should be(345)
15
+ end
16
+
17
+ it "should coerce an array of data to itself" do
18
+ results = subject.coerce [{:id => 1}, {:id => 2}, {:id => 3}]
19
+ results.each do |result|
20
+ result.should be_an_instance_of subject
21
+ end
22
+ end
23
+
24
+ it "should create a has_one relationship mapping" do
25
+ @instance.should respond_to(:address)
26
+ end
27
+
28
+ it "should respond with an object when supplied with an id" do
29
+ @instance.address(1).should be_an_instance_of Kounta::Address
30
+ WebMock.should have_requested(:get, singular_endpoint('addresses'))
31
+ end
32
+
33
+ it "should be able to create an empty instance of the has_one relationship and apply mappings to it" do
34
+ @instance.id = 162
35
+ address = @instance.address
36
+ address.should be_an_instance_of Kounta::Address
37
+ address.company_id.should be(162)
38
+ end
39
+
40
+ it "should create a has_many relationship mapping" do
41
+ @instance.should respond_to(:addresses)
42
+ end
43
+
44
+ it "should respond with an array of objects" do
45
+ @instance.addresses.each do |address|
46
+ address.should be_an_instance_of Kounta::Address
47
+ end
48
+ end
49
+
50
+ it "should apply assignments to the has_many relationship" do
51
+ @instance.addresses.each do |address|
52
+ address.company_id.should be(@instance.id)
53
+ end
54
+ end
55
+
56
+ it "should have a reference to the rest client" do
57
+ @instance.client.should be_an_instance_of Kounta::REST::Client
58
+ end
59
+
60
+ it "should be able to serialise itself to a hash" do
61
+ @instance.to_hash.should eq({:id => 345})
62
+ @instance.to_hash({:merge => "me"}).should eq({:id => 345, :merge => "me"})
63
+ end
64
+
65
+ it "should raise an error when presented with an unknown attribute" do
66
+ expect { subject.new({:id => 345, :new_attribute => 'value'}) }.to raise_error Kounta::Errors::UnknownResourceAttribute
67
+ end
68
+
69
+ it "should be able to save a new object" do
70
+ subject.property :number
71
+ subject.property :product_id
72
+ subject.property :quantity
73
+ subject.property :notes
74
+ subject.property :price_variation
75
+ instance = subject.new
76
+ instance.id = nil
77
+ instance.define_singleton_method :resource_path, lambda { {companies: 162, orders: 6789, lines: nil} }
78
+ instance.save!.should be_an_instance_of subject
79
+ WebMock.should have_requested(:post, group_endpoint('lines'))
80
+ end
81
+
82
+ it "should be able to update an existing object" do
83
+ subject.property :number
84
+ subject.property :product_id
85
+ subject.property :quantity
86
+ subject.property :notes
87
+ subject.property :price_variation
88
+ instance = subject.new
89
+ instance.id = 162
90
+ instance.define_singleton_method :resource_path, lambda { {companies: 162, orders: 6789, lines: 2345} }
91
+ instance.save!.should be_an_instance_of subject
92
+ WebMock.should have_requested(:put, singular_endpoint('lines'))
93
+ end
94
+
95
+ end
@@ -0,0 +1,38 @@
1
+ require "helper"
2
+
3
+ describe Kounta::REST::Client do
4
+
5
+ subject { Kounta::REST::Client.new }
6
+
7
+ it "should throw an error when creating a client without required tokens" do
8
+ Kounta.client_token = nil
9
+ expect { Kounta::REST::Client.new }.to raise_error Kounta::Errors::MissingOauthDetails
10
+ end
11
+
12
+ it "should be able to create a new client" do
13
+ subject.should be_an_instance_of(Kounta::REST::Client)
14
+ end
15
+
16
+ it "should be able to generate a url from an ordered hash" do
17
+ subject.path_from_hash({companies: 162, products: nil}).should eq("companies/162/products")
18
+ subject.path_from_hash({companies: 162, products: 1234}).should eq("companies/162/products/1234")
19
+ end
20
+
21
+ it "should be able to create objects from a response" do
22
+ responses = subject.objects_from_response(Kounta::Product, :get, {companies: 162, products: nil})
23
+ responses.each do |response|
24
+ response.should be_an_instance_of Kounta::Product
25
+ end
26
+ end
27
+
28
+ it "should be able to create an object from a response" do
29
+ subject.object_from_response(Kounta::Product, :get, {companies: 162, products: 555}).should be_an_instance_of Kounta::Product
30
+ end
31
+
32
+ it "should refresh the token automatically" do
33
+ stub_request(:get, group_endpoint('noop')).to_return(:body => {:message => "The access token provided has expired"}, :headers => endpoint_headers, :status => 400)
34
+ stub_request(:post, "https://api.kounta.com/v1/token.json")
35
+ expect { subject.perform({:company_id => 123, :noop => nil}, :get) }.to raise_error OAuth2::Error
36
+ end
37
+
38
+ end
@@ -0,0 +1,11 @@
1
+ require "helper"
2
+
3
+ describe Kounta::Site do
4
+
5
+ subject { Kounta::Company.new.site(985) }
6
+
7
+ it "should have a resource path" do
8
+ subject.resource_path.should eq({companies: 162, sites: 923})
9
+ end
10
+
11
+ end
@@ -0,0 +1,67 @@
1
+ module Helpers
2
+
3
+ def company_me_endpoint
4
+ /\/.+\/companies\/me\.json/
5
+ end
6
+
7
+ def base_pricelist_endpoint
8
+ /\/.+\/price_lists\/base\.json/
9
+ end
10
+
11
+ def singular_endpoint(name)
12
+ /\/.+\/#{name}\/\d+\.json/
13
+ end
14
+
15
+ def group_endpoint(name)
16
+ /\/.+\/#{name}\.json/
17
+ end
18
+
19
+ def load_json_from_fixture(filename)
20
+ File.read("#{Kounta.root}/spec/fixtures/#{filename}")
21
+ end
22
+
23
+ def endpoint_headers
24
+ {'Content-Type' => 'application/json'}
25
+ end
26
+
27
+ def stub_endpoints
28
+
29
+ # create
30
+ stub_request(:post, group_endpoint('products')).to_return(:body => load_json_from_fixture('product.json'), :headers => endpoint_headers)
31
+ stub_request(:post, group_endpoint('customers')).to_return(:body => load_json_from_fixture('customer.json'), :headers => endpoint_headers)
32
+ stub_request(:post, group_endpoint('categories')).to_return(:body => load_json_from_fixture('category.json'), :headers => endpoint_headers)
33
+ stub_request(:post, group_endpoint('addresses')).to_return(:body => load_json_from_fixture('address.json'), :headers => endpoint_headers)
34
+ stub_request(:post, group_endpoint('payments')).to_return(:body => load_json_from_fixture('payment.json'), :headers => endpoint_headers)
35
+ stub_request(:post, group_endpoint('lines')).to_return(:body => load_json_from_fixture('line.json'), :headers => endpoint_headers)
36
+ stub_request(:post, group_endpoint('orders')).to_return(:body => load_json_from_fixture('order.json'), :headers => endpoint_headers)
37
+
38
+ # read
39
+ stub_request(:get, singular_endpoint('categories')).to_return(:body => load_json_from_fixture('category.json'), :headers => endpoint_headers)
40
+ stub_request(:get, group_endpoint('categories')).to_return(:body => load_json_from_fixture('categories.json'), :headers => endpoint_headers)
41
+ stub_request(:get, singular_endpoint('customers')).to_return(:body => load_json_from_fixture('customer.json'), :headers => endpoint_headers)
42
+ stub_request(:get, group_endpoint('customers')).to_return(:body => load_json_from_fixture('customers.json'), :headers => endpoint_headers)
43
+ stub_request(:get, singular_endpoint('addresses')).to_return(:body => load_json_from_fixture('address.json'), :headers => endpoint_headers)
44
+ stub_request(:get, group_endpoint('addresses')).to_return(:body => load_json_from_fixture('addresses.json'), :headers => endpoint_headers)
45
+ stub_request(:get, singular_endpoint('products')).to_return(:body => load_json_from_fixture('product.json'), :headers => endpoint_headers)
46
+ stub_request(:get, group_endpoint('products')).to_return(:body => load_json_from_fixture('products.json'), :headers => endpoint_headers)
47
+ stub_request(:get, singular_endpoint('sites')).to_return(:body => load_json_from_fixture('site.json'), :headers => endpoint_headers)
48
+ stub_request(:get, group_endpoint('sites')).to_return(:body => load_json_from_fixture('sites.json'), :headers => endpoint_headers)
49
+ stub_request(:get, singular_endpoint('price_lists')).to_return(:body => load_json_from_fixture('price_list.json'), :headers => endpoint_headers)
50
+ stub_request(:get, group_endpoint('price_lists')).to_return(:body => load_json_from_fixture('price_lists.json'), :headers => endpoint_headers)
51
+ stub_request(:get, base_pricelist_endpoint).to_return(:body => load_json_from_fixture('price_list.json'), :headers => endpoint_headers)
52
+ stub_request(:get, company_me_endpoint).to_return(:body => load_json_from_fixture('companies_me.json'), :headers => endpoint_headers)
53
+ stub_request(:get, group_endpoint('taxes')).to_return(:body => load_json_from_fixture('taxes.json'), :headers => endpoint_headers)
54
+ stub_request(:get, singular_endpoint('orders')).to_return(:body => load_json_from_fixture('order.json'), :headers => endpoint_headers)
55
+ stub_request(:get, group_endpoint('orders')).to_return(:body => load_json_from_fixture('orders.json'), :headers => endpoint_headers)
56
+
57
+ # update
58
+ stub_request(:put, singular_endpoint('products')).to_return(:body => load_json_from_fixture('product.json'), :headers => endpoint_headers)
59
+ stub_request(:put, singular_endpoint('customers')).to_return(:body => load_json_from_fixture('customer.json'), :headers => endpoint_headers)
60
+ stub_request(:put, singular_endpoint('categories')).to_return(:body => load_json_from_fixture('category.json'), :headers => endpoint_headers)
61
+ stub_request(:put, singular_endpoint('addresses')).to_return(:body => load_json_from_fixture('address.json'), :headers => endpoint_headers)
62
+ stub_request(:put, singular_endpoint('payments')).to_return(:body => load_json_from_fixture('payment.json'), :headers => endpoint_headers)
63
+ stub_request(:put, singular_endpoint('lines')).to_return(:body => load_json_from_fixture('line.json'), :headers => endpoint_headers)
64
+ stub_request(:put, singular_endpoint('orders')).to_return(:body => load_json_from_fixture('order.json'), :headers => endpoint_headers)
65
+
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,236 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kounta_rest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Richardson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-03 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.7.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.7.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 1.13.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 1.13.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: oj
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 2.1.4
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 2.1.4
97
+ - !ruby/object:Gem::Dependency
98
+ name: hashie
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: 2.0.5
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: 2.0.5
111
+ - !ruby/object:Gem::Dependency
112
+ name: oauth2
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '='
116
+ - !ruby/object:Gem::Version
117
+ version: 0.9.2
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '='
123
+ - !ruby/object:Gem::Version
124
+ version: 0.9.2
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '='
130
+ - !ruby/object:Gem::Version
131
+ version: 0.9.12.2
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '='
137
+ - !ruby/object:Gem::Version
138
+ version: 0.9.12.2
139
+ - !ruby/object:Gem::Dependency
140
+ name: yell
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '='
144
+ - !ruby/object:Gem::Version
145
+ version: 1.5.1
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '='
151
+ - !ruby/object:Gem::Version
152
+ version: 1.5.1
153
+ description: Library for accessing Kountas RESTful API
154
+ email:
155
+ - sam@richardson.co.nz
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - .gitignore
161
+ - .gitmodules
162
+ - Gemfile
163
+ - LICENSE.txt
164
+ - README.md
165
+ - Rakefile
166
+ - console.rb
167
+ - kounta.gemspec
168
+ - lib/kounta.rb
169
+ - lib/kounta/address.rb
170
+ - lib/kounta/category.rb
171
+ - lib/kounta/company.rb
172
+ - lib/kounta/customer.rb
173
+ - lib/kounta/errors.rb
174
+ - lib/kounta/line.rb
175
+ - lib/kounta/order.rb
176
+ - lib/kounta/payment.rb
177
+ - lib/kounta/price_list.rb
178
+ - lib/kounta/product.rb
179
+ - lib/kounta/resource.rb
180
+ - lib/kounta/rest/client.rb
181
+ - lib/kounta/site.rb
182
+ - lib/kounta/tax.rb
183
+ - lib/kounta/version.rb
184
+ - spec/helper.rb
185
+ - spec/kounta/address_spec.rb
186
+ - spec/kounta/category_spec.rb
187
+ - spec/kounta/company_spec.rb
188
+ - spec/kounta/customer_spec.rb
189
+ - spec/kounta/kounta_spec.rb
190
+ - spec/kounta/line_spec.rb
191
+ - spec/kounta/order_spec.rb
192
+ - spec/kounta/payment_spec.rb
193
+ - spec/kounta/product_spec.rb
194
+ - spec/kounta/resource_spec.rb
195
+ - spec/kounta/rest/client_spec.rb
196
+ - spec/kounta/site_spec.rb
197
+ - spec/support/endpoints.rb
198
+ homepage: http://www.richardson.co.nz
199
+ licenses:
200
+ - MIT
201
+ metadata: {}
202
+ post_install_message:
203
+ rdoc_options: []
204
+ require_paths:
205
+ - lib
206
+ required_ruby_version: !ruby/object:Gem::Requirement
207
+ requirements:
208
+ - - '>='
209
+ - !ruby/object:Gem::Version
210
+ version: '0'
211
+ required_rubygems_version: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - '>='
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ requirements: []
217
+ rubyforge_project:
218
+ rubygems_version: 2.0.3
219
+ signing_key:
220
+ specification_version: 4
221
+ summary: Will write this
222
+ test_files:
223
+ - spec/helper.rb
224
+ - spec/kounta/address_spec.rb
225
+ - spec/kounta/category_spec.rb
226
+ - spec/kounta/company_spec.rb
227
+ - spec/kounta/customer_spec.rb
228
+ - spec/kounta/kounta_spec.rb
229
+ - spec/kounta/line_spec.rb
230
+ - spec/kounta/order_spec.rb
231
+ - spec/kounta/payment_spec.rb
232
+ - spec/kounta/product_spec.rb
233
+ - spec/kounta/resource_spec.rb
234
+ - spec/kounta/rest/client_spec.rb
235
+ - spec/kounta/site_spec.rb
236
+ - spec/support/endpoints.rb