vend 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +57 -0
- data/README.rdoc +1 -1
- data/Rakefile +1 -1
- data/example.rb +3 -3
- data/lib/vend.rb +2 -1
- data/lib/vend/base.rb +12 -12
- data/lib/vend/base_factory.rb +2 -6
- data/lib/vend/client.rb +5 -5
- data/lib/vend/http_client.rb +30 -31
- data/lib/vend/null_logger.rb +9 -5
- data/lib/vend/oauth2/auth_code.rb +7 -10
- data/lib/vend/oauth2/client.rb +1 -5
- data/lib/vend/pagination_info.rb +2 -1
- data/lib/vend/resource/customer.rb +1 -3
- data/lib/vend/resource/outlet.rb +0 -2
- data/lib/vend/resource/payment_type.rb +0 -2
- data/lib/vend/resource/product.rb +0 -2
- data/lib/vend/resource/register.rb +0 -2
- data/lib/vend/resource/register_sale.rb +2 -5
- data/lib/vend/resource/register_sale_product.rb +1 -1
- data/lib/vend/resource/supplier.rb +23 -0
- data/lib/vend/resource/tax.rb +0 -2
- data/lib/vend/resource/user.rb +0 -2
- data/lib/vend/resource_collection.rb +11 -15
- data/lib/vend/scope.rb +1 -2
- data/lib/vend/version.rb +1 -1
- data/spec/integration/customer_spec.rb +12 -9
- data/spec/integration/outlet_spec.rb +2 -3
- data/spec/integration/payment_types_spec.rb +1 -2
- data/spec/integration/product_spec.rb +26 -29
- data/spec/integration/register_sale_spec.rb +9 -10
- data/spec/integration/register_spec.rb +1 -2
- data/spec/integration/supplier_spec.rb +14 -0
- data/spec/integration/tax_spec.rb +1 -2
- data/spec/integration/user_spec.rb +1 -2
- data/spec/mock_responses/suppliers.json +36 -0
- data/spec/spec_helper.rb +1 -2
- data/spec/support/matchers/have_attributes.rb +3 -3
- data/spec/support/shared_examples/integration.rb +7 -9
- data/spec/support/shared_examples/logger.rb +21 -10
- data/spec/vend/base_factory_spec.rb +11 -12
- data/spec/vend/base_spec.rb +22 -23
- data/spec/vend/client_spec.rb +40 -39
- data/spec/vend/http_client_spec.rb +70 -62
- data/spec/vend/null_logger_spec.rb +1 -1
- data/spec/vend/oauth2/auth_code_spec.rb +18 -17
- data/spec/vend/oauth2/client_spec.rb +21 -21
- data/spec/vend/pagination_info_spec.rb +40 -17
- data/spec/vend/resource/register_sale_product_spec.rb +8 -6
- data/spec/vend/resource/register_sale_spec.rb +7 -8
- data/spec/vend/resource_collection_spec.rb +108 -95
- data/spec/vend/scope_spec.rb +20 -12
- data/vend.gemspec +5 -6
- 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(:
|
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, :
|
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
|
-
|
34
|
+
protected
|
35
|
+
|
37
36
|
def get_oauth2_client(domain_prefix = 'secure')
|
38
37
|
OAuth2::Client.new(client_id, secret, {
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
data/lib/vend/oauth2/client.rb
CHANGED
@@ -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
|
-
|
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
|
data/lib/vend/pagination_info.rb
CHANGED
data/lib/vend/resource/outlet.rb
CHANGED
@@ -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, :
|
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(:
|
16
|
+
super.merge(url_params: {page_size: 200})
|
19
17
|
end
|
20
18
|
end
|
21
|
-
|
22
19
|
end
|
23
20
|
end
|
@@ -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
|
data/lib/vend/resource/tax.rb
CHANGED
data/lib/vend/resource/user.rb
CHANGED
@@ -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
|
9
|
-
class
|
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
|
-
|
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
|
-
|
113
|
+
protected
|
114
|
+
|
120
115
|
attr_accessor :response
|
121
116
|
|
122
|
-
|
117
|
+
protected
|
118
|
+
|
123
119
|
def get_next_page
|
124
120
|
if last_page?
|
125
121
|
raise PageOutOfBoundsError.new(
|
data/lib/vend/scope.rb
CHANGED
data/lib/vend/version.rb
CHANGED
@@ -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
|
20
|
-
|
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(:
|
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.
|
28
|
-
collection.first.email.
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
16
|
-
|
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(:
|
21
|
-
let(:
|
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(
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
stub_request(
|
35
|
-
|
36
|
-
|
37
|
-
|
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.
|
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(
|
63
|
-
|
64
|
-
|
65
|
-
|
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.
|
69
|
+
expect(client.Product.active(1).since(timestamp).count).to eq 3
|
71
70
|
end
|
72
|
-
|
73
|
-
|
74
71
|
end
|
75
72
|
end
|