vend 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +57 -0
  3. data/README.rdoc +1 -1
  4. data/Rakefile +1 -1
  5. data/example.rb +3 -3
  6. data/lib/vend.rb +2 -1
  7. data/lib/vend/base.rb +12 -12
  8. data/lib/vend/base_factory.rb +2 -6
  9. data/lib/vend/client.rb +5 -5
  10. data/lib/vend/http_client.rb +30 -31
  11. data/lib/vend/null_logger.rb +9 -5
  12. data/lib/vend/oauth2/auth_code.rb +7 -10
  13. data/lib/vend/oauth2/client.rb +1 -5
  14. data/lib/vend/pagination_info.rb +2 -1
  15. data/lib/vend/resource/customer.rb +1 -3
  16. data/lib/vend/resource/outlet.rb +0 -2
  17. data/lib/vend/resource/payment_type.rb +0 -2
  18. data/lib/vend/resource/product.rb +0 -2
  19. data/lib/vend/resource/register.rb +0 -2
  20. data/lib/vend/resource/register_sale.rb +2 -5
  21. data/lib/vend/resource/register_sale_product.rb +1 -1
  22. data/lib/vend/resource/supplier.rb +23 -0
  23. data/lib/vend/resource/tax.rb +0 -2
  24. data/lib/vend/resource/user.rb +0 -2
  25. data/lib/vend/resource_collection.rb +11 -15
  26. data/lib/vend/scope.rb +1 -2
  27. data/lib/vend/version.rb +1 -1
  28. data/spec/integration/customer_spec.rb +12 -9
  29. data/spec/integration/outlet_spec.rb +2 -3
  30. data/spec/integration/payment_types_spec.rb +1 -2
  31. data/spec/integration/product_spec.rb +26 -29
  32. data/spec/integration/register_sale_spec.rb +9 -10
  33. data/spec/integration/register_spec.rb +1 -2
  34. data/spec/integration/supplier_spec.rb +14 -0
  35. data/spec/integration/tax_spec.rb +1 -2
  36. data/spec/integration/user_spec.rb +1 -2
  37. data/spec/mock_responses/suppliers.json +36 -0
  38. data/spec/spec_helper.rb +1 -2
  39. data/spec/support/matchers/have_attributes.rb +3 -3
  40. data/spec/support/shared_examples/integration.rb +7 -9
  41. data/spec/support/shared_examples/logger.rb +21 -10
  42. data/spec/vend/base_factory_spec.rb +11 -12
  43. data/spec/vend/base_spec.rb +22 -23
  44. data/spec/vend/client_spec.rb +40 -39
  45. data/spec/vend/http_client_spec.rb +70 -62
  46. data/spec/vend/null_logger_spec.rb +1 -1
  47. data/spec/vend/oauth2/auth_code_spec.rb +18 -17
  48. data/spec/vend/oauth2/client_spec.rb +21 -21
  49. data/spec/vend/pagination_info_spec.rb +40 -17
  50. data/spec/vend/resource/register_sale_product_spec.rb +8 -6
  51. data/spec/vend/resource/register_sale_spec.rb +7 -8
  52. data/spec/vend/resource_collection_spec.rb +108 -95
  53. data/spec/vend/scope_spec.rb +20 -12
  54. data/vend.gemspec +5 -6
  55. metadata +10 -20
@@ -2,9 +2,7 @@ require 'oauth2'
2
2
 
3
3
  module Vend
4
4
  module Oauth2
5
-
6
5
  class AuthCode
7
-
8
6
  DEFAULT_OPTIONS = {}
9
7
  AUTHORIZE_URL = '/connect'
10
8
  TOKEN_URL = '/api/1.0/token'
@@ -20,12 +18,12 @@ module Vend
20
18
  end
21
19
 
22
20
  def authorize_url
23
- get_oauth2_client.auth_code.authorize_url(:redirect_uri => redirect_uri)
21
+ get_oauth2_client.auth_code.authorize_url(redirect_uri: redirect_uri)
24
22
  end
25
23
 
26
24
  def token_from_code(code)
27
25
  client = get_oauth2_client(store)
28
- client.auth_code.get_token(code, :redirect_uri => redirect_uri)
26
+ client.auth_code.get_token(code, redirect_uri: redirect_uri)
29
27
  end
30
28
 
31
29
  def refresh_token(auth_token, refresh_token)
@@ -33,16 +31,15 @@ module Vend
33
31
  access_token.refresh!
34
32
  end
35
33
 
36
- protected
34
+ protected
35
+
37
36
  def get_oauth2_client(domain_prefix = 'secure')
38
37
  OAuth2::Client.new(client_id, secret, {
39
- site: "https://#{domain_prefix}.vendhq.com",
40
- authorize_url: AUTHORIZE_URL,
41
- token_url: TOKEN_URL
38
+ site: "https://#{domain_prefix}.vendhq.com",
39
+ authorize_url: AUTHORIZE_URL,
40
+ token_url: TOKEN_URL
42
41
  })
43
42
  end
44
-
45
43
  end
46
-
47
44
  end
48
45
  end
@@ -1,8 +1,6 @@
1
1
  module Vend
2
2
  module Oauth2
3
-
4
3
  class Client < Vend::Client
5
-
6
4
  DEFAULT_OPTIONS = {}
7
5
 
8
6
  include Logable
@@ -21,11 +19,9 @@ module Vend
21
19
 
22
20
  def http_client_options
23
21
  options.merge(
24
- :auth_token => @auth_token, :base_url => base_url
22
+ auth_token: @auth_token, base_url: base_url
25
23
  )
26
24
  end
27
-
28
25
  end
29
-
30
26
  end
31
27
  end
@@ -22,7 +22,8 @@ module Vend
22
22
  pages == page
23
23
  end
24
24
 
25
- protected
25
+ protected
26
+
26
27
  def pagination
27
28
  @pagination ||= (response['pagination'] || {"pages" => 1, "page" => 1})
28
29
  end
@@ -1,11 +1,9 @@
1
1
  module Vend
2
2
  module Resource
3
-
4
3
  class Customer < Vend::Base
5
4
  url_scope :since
6
5
  findable_by :email
7
- findable_by :name, :as => :q
6
+ findable_by :name, as: :q
8
7
  end
9
-
10
8
  end
11
9
  end
@@ -1,7 +1,5 @@
1
1
  module Vend
2
2
  module Resource
3
-
4
3
  class Outlet < Vend::Base; end
5
-
6
4
  end
7
5
  end
@@ -1,7 +1,5 @@
1
1
  module Vend
2
2
  module Resource
3
-
4
3
  class PaymentType < Vend::Base; end
5
-
6
4
  end
7
5
  end
@@ -1,12 +1,10 @@
1
1
  module Vend
2
2
  module Resource
3
-
4
3
  class Product < Vend::Base
5
4
  url_scope :since
6
5
  url_scope :active
7
6
 
8
7
  cast_attribute :supply_price, Float
9
8
  end
10
-
11
9
  end
12
10
  end
@@ -1,7 +1,5 @@
1
1
  module Vend
2
2
  module Resource
3
-
4
3
  class Register < Vend::Base; end
5
-
6
4
  end
7
5
  end
@@ -1,12 +1,10 @@
1
-
2
1
  module Vend
3
2
  module Resource
4
-
5
3
  class RegisterSale < Vend::Base
6
4
  url_scope :since
7
5
  url_scope :outlet_id
8
6
  url_scope :tag
9
- findable_by :state, :as => :status
7
+ findable_by :state, as: :status
10
8
 
11
9
  def register_sale_products
12
10
  attrs["register_sale_products"].collect do |sale_product_attrs|
@@ -15,9 +13,8 @@ module Vend
15
13
  end
16
14
 
17
15
  def self.default_collection_request_args
18
- super.merge(:url_params => {:page_size => 200})
16
+ super.merge(url_params: {page_size: 200})
19
17
  end
20
18
  end
21
-
22
19
  end
23
20
  end
@@ -9,7 +9,7 @@ module Vend
9
9
  @attrs = attrs
10
10
  end
11
11
 
12
- def method_missing(method_name, *args, &block)
12
+ def method_missing(method_name, *_args, &_block)
13
13
  if attrs.keys.include? method_name.to_s
14
14
  attrs[method_name.to_s]
15
15
  else
@@ -0,0 +1,23 @@
1
+ module Vend
2
+ module Resource
3
+ class Supplier < Vend::Base
4
+ # Returns the endpoint name for a collection of this resource
5
+ def self.collection_name
6
+ endpoint_name
7
+ end
8
+
9
+ # Builds a collection of instances from a JSON response
10
+ def self.build_from_json(client, json)
11
+ json[collection_name.pluralize].map do |attrs|
12
+ self.build(client, attrs)
13
+ end
14
+ end
15
+
16
+ # Initializes a single object from a JSON response.
17
+ # Assumes the response is a JSON array with a single item.
18
+ def self.initialize_singular(client, json)
19
+ self.build(client, json[collection_name.pluralize].first)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,7 +1,5 @@
1
1
  module Vend
2
2
  module Resource
3
-
4
3
  class Tax < Vend::Base; end
5
-
6
4
  end
7
5
  end
@@ -1,7 +1,5 @@
1
1
  module Vend
2
2
  module Resource
3
-
4
3
  class User < Vend::Base; end
5
-
6
4
  end
7
5
  end
@@ -4,10 +4,9 @@ module Vend
4
4
  # resources. This class will automatically fetch paginated results if the
5
5
  # target_class supports it.
6
6
  class ResourceCollection
7
-
8
- class PageOutOfBoundsError < StandardError ; end
9
- class AlreadyScopedError < StandardError ; end
10
- class ScopeNotFoundError < StandardError ; end
7
+ class PageOutOfBoundsError < StandardError; end
8
+ class AlreadyScopedError < StandardError; end
9
+ class ScopeNotFoundError < StandardError; end
11
10
 
12
11
  include Enumerable
13
12
  extend Forwardable
@@ -39,9 +38,7 @@ module Vend
39
38
  end
40
39
 
41
40
  def pagination
42
- if response.instance_of? Hash
43
- PaginationInfo.new(response)
44
- end
41
+ PaginationInfo.new(response) if response.instance_of? Hash
45
42
  end
46
43
 
47
44
  def last_page?
@@ -66,7 +63,7 @@ module Vend
66
63
  end
67
64
 
68
65
  def has_scope?(name)
69
- scopes.any? {|s| s.name == name }
66
+ scopes.any? { |s| s.name == name }
70
67
  end
71
68
 
72
69
  def method_missing(method_name, *args, &block)
@@ -90,20 +87,17 @@ module Vend
90
87
  end
91
88
 
92
89
  def get_scope(name)
93
-
94
90
  result = scopes.find { |scope| scope.name == name }
95
91
  if result.nil?
96
92
  raise ScopeNotFoundError.new(
97
93
  "Scope: #{name} was not found in #{scopes}."
98
94
  )
99
95
  end
100
- return result
96
+ result
101
97
  end
102
98
 
103
99
  def get_or_create_page_scope
104
- unless has_scope? :page
105
- scope(:page, page)
106
- end
100
+ scope(:page, page) unless has_scope? :page
107
101
  get_scope :page
108
102
  end
109
103
 
@@ -116,10 +110,12 @@ module Vend
116
110
  endpoint + scopes.join
117
111
  end
118
112
 
119
- protected
113
+ protected
114
+
120
115
  attr_accessor :response
121
116
 
122
- protected
117
+ protected
118
+
123
119
  def get_next_page
124
120
  if last_page?
125
121
  raise PageOutOfBoundsError.new(
@@ -1,6 +1,5 @@
1
1
  module Vend
2
2
  class Scope
3
-
4
3
  attr_reader :name
5
4
  attr_accessor :value
6
5
 
@@ -17,7 +16,7 @@ module Vend
17
16
  else
18
17
  result = value.to_s
19
18
  end
20
- CGI::escape(result)
19
+ CGI.escape(result)
21
20
  end
22
21
 
23
22
  def to_s
@@ -1,3 +1,3 @@
1
1
  module Vend #:nodoc:
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -1,7 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Vend::Resource::Customer do
4
-
5
4
  let(:expected_attributes) do
6
5
  {
7
6
  'id' => '6cbfbc20-3d5f-11e0-8697-4040f540b50a',
@@ -16,18 +15,22 @@ describe Vend::Resource::Customer do
16
15
 
17
16
  let(:expected_collection_length) { 1 }
18
17
 
19
- specify { client.Customer.should respond_to(:find_by_email) }
20
- specify { client.Customer.should respond_to(:find_by_name) }
18
+ specify :find_by_email do
19
+ expect(client.Customer).to respond_to(:find_by_email)
20
+ end
21
+
22
+ specify :find_by_name do
23
+ expect(client.Customer).to respond_to(:find_by_name)
24
+ end
21
25
 
22
26
  it "returns a collection of customers from a search" do
23
- stub_request(:get, "https://bar:baz@foo.vendhq.com/api/customers?email=foo@example.com").
24
- to_return(:status => 200, :body => get_mock_response('customers.find_by_email.json'))
27
+ stub_request(:get, "https://bar:baz@foo.vendhq.com/api/customers?email=foo@example.com")
28
+ .to_return(status: 200, body: get_mock_response('customers.find_by_email.json'))
25
29
 
26
30
  collection = client.Customer.find_by_email('foo@example.com')
27
- collection.first.should be_a Vend::Resource::Customer
28
- collection.first.email.should == 'foo@example.com'
31
+ expect(collection.first).to be_a Vend::Resource::Customer
32
+ expect(collection.first.email).to eq 'foo@example.com'
29
33
  end
30
34
 
31
- it_should_behave_like "a resource with a collection GET endpoint"
32
-
35
+ it_behaves_like "a resource with a collection GET endpoint"
33
36
  end
@@ -1,15 +1,14 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Vend::Resource::Outlet do
4
-
5
4
  let(:expected_attributes) do
6
5
  {
7
6
  'id' => '6cb5c88c-3d5f-11e0-8697-4040f540b50a',
8
- 'name' => 'Main Outlet',
7
+ 'name' => 'Main Outlet'
9
8
  }
10
9
  end
11
10
 
12
11
  let(:expected_collection_length) { 1 }
13
12
 
14
- it_should_behave_like "a resource with a collection GET endpoint"
13
+ it_behaves_like "a resource with a collection GET endpoint"
15
14
  end
@@ -1,7 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Vend::Resource::PaymentType do
4
-
5
4
  let(:expected_attributes) do
6
5
  {
7
6
  'id' => '6cde3ba0-3d5f-11e0-8697-4040f540b50a',
@@ -11,5 +10,5 @@ describe Vend::Resource::PaymentType do
11
10
 
12
11
  let(:expected_collection_length) { 1 }
13
12
 
14
- it_should_behave_like "a resource with a collection GET endpoint"
13
+ it_behaves_like "a resource with a collection GET endpoint"
15
14
  end
@@ -12,34 +12,33 @@ describe Vend::Resource::Product do
12
12
 
13
13
  let(:expected_collection_length) { 2 }
14
14
 
15
- it_should_behave_like "a resource with a collection GET endpoint"
16
- it_should_behave_like "a resource with a DELETE endpoint"
15
+ it_behaves_like "a resource with a collection GET endpoint"
16
+ it_behaves_like "a resource with a DELETE endpoint"
17
17
 
18
18
  describe "pagination" do
19
-
20
- let(:username) {"foo"}
21
- let(:password) {"bar"}
22
- let(:store) {"baz"}
19
+ let(:username) { "foo" }
20
+ let(:password) { "bar" }
21
+ let(:store) { "baz" }
23
22
 
24
23
  let(:client) do
25
24
  Vend::Client.new(store, username, password)
26
25
  end
27
26
 
28
27
  before do
29
- stub_request( :get,
30
- "https://#{username}:#{password}@#{store}.vendhq.com/api/products"
31
- ).to_return(
32
- :status => 200, :body => get_mock_response('products/page/1.json')
33
- )
34
- stub_request( :get,
35
- "https://#{username}:#{password}@#{store}.vendhq.com/api/products/page/2"
36
- ).to_return(
37
- :status => 200, :body => get_mock_response('products/page/2.json')
38
- )
28
+ stub_request(:get,
29
+ "https://#{username}:#{password}@#{store}.vendhq.com/api/products"
30
+ ).to_return(
31
+ status: 200, body: get_mock_response('products/page/1.json')
32
+ )
33
+ stub_request(:get,
34
+ "https://#{username}:#{password}@#{store}.vendhq.com/api/products/page/2"
35
+ ).to_return(
36
+ status: 200, body: get_mock_response('products/page/2.json')
37
+ )
39
38
  end
40
39
 
41
40
  it "returns paginated results" do
42
- client.Product.all.count.should == 4
41
+ expect(client.Product.all.count).to eq 4
43
42
  end
44
43
 
45
44
  it "returns the first result" do
@@ -48,28 +47,26 @@ describe Vend::Resource::Product do
48
47
  end
49
48
 
50
49
  describe "chaining" do
51
- let(:username) {"foo"}
52
- let(:password) {"bar"}
53
- let(:store) {"baz"}
50
+ let(:username) { "foo" }
51
+ let(:password) { "bar" }
52
+ let(:store) { "baz" }
54
53
 
55
- let(:timestamp) { Time.new(2012,7,5,11,12,13) }
54
+ let(:timestamp) { Time.new(2012, 7, 5, 11, 12, 13) }
56
55
 
57
56
  let(:client) do
58
57
  Vend::Client.new(store, username, password)
59
58
  end
60
59
 
61
60
  before do
62
- stub_request( :get,
63
- "https://#{username}:#{password}@#{store}.vendhq.com/api/products/active/1/since/2012-07-05+11:12:13"
64
- ).to_return(
65
- :status => 200, :body => get_mock_response('products.active.since.json')
66
- )
61
+ stub_request(:get,
62
+ "https://#{username}:#{password}@#{store}.vendhq.com/api/products/active/1/since/2012-07-05+11:12:13"
63
+ ).to_return(
64
+ status: 200, body: get_mock_response('products.active.since.json')
65
+ )
67
66
  end
68
67
 
69
68
  it "allows scope chaining" do
70
- client.Product.active(1).since(timestamp).count.should == 3
69
+ expect(client.Product.active(1).since(timestamp).count).to eq 3
71
70
  end
72
-
73
-
74
71
  end
75
72
  end