ecommerce-client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +31 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +29 -0
  7. data/Rakefile +5 -0
  8. data/ecommerce-client.gemspec +29 -0
  9. data/lib/ecommerce.rb +25 -0
  10. data/lib/ecommerce/attribute_handler.rb +51 -0
  11. data/lib/ecommerce/client.rb +45 -0
  12. data/lib/ecommerce/configuration.rb +14 -0
  13. data/lib/ecommerce/exception.rb +11 -0
  14. data/lib/ecommerce/request.rb +49 -0
  15. data/lib/ecommerce/resources/base.rb +28 -0
  16. data/lib/ecommerce/resources/order.rb +91 -0
  17. data/lib/ecommerce/resources/order_collection.rb +62 -0
  18. data/lib/ecommerce/response.rb +34 -0
  19. data/lib/ecommerce/version.rb +3 -0
  20. data/spec/ecommerce/attribute_handler_spec.rb +54 -0
  21. data/spec/ecommerce/client_spec.rb +26 -0
  22. data/spec/ecommerce/configuration_spec.rb +21 -0
  23. data/spec/ecommerce/request_spec.rb +23 -0
  24. data/spec/ecommerce/resources/order_collection_spec.rb +82 -0
  25. data/spec/ecommerce/resources/order_spec.rb +155 -0
  26. data/spec/ecommerce/response_spec.rb +35 -0
  27. data/spec/ecommerce_spec.rb +48 -0
  28. data/spec/spec_helper.rb +24 -0
  29. data/spec/support/vcr.rb +7 -0
  30. data/spec/vcr_cassettes/Ecommerce/Resources_Order_create_when_success_returns_body.yml +64 -0
  31. data/spec/vcr_cassettes/Ecommerce/Resources_Order_create_when_success_when_sending_not_all_required_parameters_raises_.yml +46 -0
  32. data/spec/vcr_cassettes/Ecommerce/Resources_Order_destroy_when_not_found_raises_NotFound.yml +46 -0
  33. data/spec/vcr_cassettes/Ecommerce/Resources_Order_destroy_when_success_returns_empty_body.yml +48 -0
  34. data/spec/vcr_cassettes/Ecommerce/Resources_Order_find_all_when_not_found_raises_NotFound.yml +46 -0
  35. data/spec/vcr_cassettes/Ecommerce/Resources_Order_find_all_when_success_returns_a_find_all_of_orders.yml +99 -0
  36. data/spec/vcr_cassettes/Ecommerce/Resources_Order_find_when_not_found_raises_NotFound.yml +46 -0
  37. data/spec/vcr_cassettes/Ecommerce/Resources_Order_find_when_success_returns_order_object.yml +58 -0
  38. data/spec/vcr_cassettes/client/authenticated/false.yml +47 -0
  39. data/spec/vcr_cassettes/client/authenticated/true.yml +56 -0
  40. data/tasks/rspec.rake +2 -0
  41. metadata +206 -0
@@ -0,0 +1,62 @@
1
+ module Ecommerce
2
+ module Resources
3
+
4
+ #
5
+ # A wrapper to Ecommerce orders API. This wrapper represents a collection of orders and it's responsible for processing pagination information as well.
6
+ #
7
+
8
+ class OrderCollection < Base
9
+
10
+ PAGE_REGEX = /page=(\d+)/
11
+
12
+ attr_reader :response, :orders, :headers
13
+
14
+ def initialize(response)
15
+ @response = response
16
+ @orders = []
17
+ @headers = response.headers['Link'].split(',')
18
+ end
19
+
20
+ def self.build(response)
21
+ self.new(response).build
22
+ end
23
+
24
+ def build
25
+ build_orders
26
+ self
27
+ end
28
+
29
+ def next_page
30
+ page_for(:next)
31
+ end
32
+
33
+ def last_page
34
+ page_for(:last)
35
+ end
36
+
37
+ def previous_page
38
+ page_for(:prev)
39
+ end
40
+
41
+ def first_page
42
+ page_for(:first)
43
+ end
44
+
45
+ private
46
+
47
+ def page_for(page_rel)
48
+ header_link_for(page_rel).match(PAGE_REGEX)[1].to_i rescue nil
49
+ end
50
+
51
+ def header_link_for(rel)
52
+ headers.select{|n| n =~ /rel=#{rel}/}.first
53
+ end
54
+
55
+ def build_orders
56
+ Ecommerce::Resources::Base.parsed_body(response).each do |order_attributes|
57
+ orders.push(Ecommerce::Resources::Order.new(order_attributes))
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,34 @@
1
+ require "ecommerce/exception"
2
+
3
+ module Ecommerce
4
+ RequestTimeout = Class.new(Exception)
5
+ RequestError = Class.new(Exception)
6
+
7
+ class Response < SimpleDelegator
8
+
9
+ def resolve!(&block)
10
+ if success?
11
+ block_given? ? yield(self) : self
12
+ elsif timed_out?
13
+ timeout!
14
+ else
15
+ error!
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def timeout!
22
+ raise RequestTimeout
23
+ end
24
+
25
+ def error!
26
+ raise RequestError.new(
27
+ code: code,
28
+ message: status_message,
29
+ body: body
30
+ )
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module Ecommerce
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,54 @@
1
+ require "spec_helper"
2
+
3
+ describe Ecommerce::AttributeHandler do
4
+ describe '.handle' do
5
+ it 'instantiates a new Ecommerce::AttributeHandler' do
6
+ expect(Ecommerce::AttributeHandler).to receive(:new).and_return(double(handle: ''))
7
+ Ecommerce::AttributeHandler.handle('')
8
+ end
9
+ end
10
+
11
+ describe '#handle' do
12
+ context 'when attribute is a decimal' do
13
+ subject { Ecommerce::AttributeHandler.new('1366.99') }
14
+
15
+ it 'returns a BigDecimal object' do
16
+ expect(subject.handle).to eq(BigDecimal.new('1366.99'))
17
+ end
18
+ end
19
+
20
+ context 'when attribute is a date in ISO8601 format' do
21
+ subject { Ecommerce::AttributeHandler.new('2015-03-26') }
22
+
23
+ it 'returns a Date object' do
24
+ expect(subject.handle).to eq(Date.new(2015, 3, 26))
25
+ end
26
+ end
27
+
28
+ context 'when attribute is a datetime in ISO8601 format' do
29
+ context 'when datetime has the format with date and hours separated' do
30
+ subject { Ecommerce::AttributeHandler.new('2015-03-26 11:11:46') }
31
+
32
+ it 'returns a Date object' do
33
+ expect(subject.handle).to eq(DateTime.new(2015, 3, 26, 11, 11, 46))
34
+ end
35
+ end
36
+
37
+ context 'when datetime has the format with date and hours separated, in UTC' do
38
+ subject { Ecommerce::AttributeHandler.new('2015-03-26 11:11:46Z') }
39
+
40
+ it 'returns a Date object' do
41
+ expect(subject.handle).to eq(DateTime.new(2015, 3, 26, 11, 11, 46))
42
+ end
43
+ end
44
+
45
+ context 'when datetime has the format with date and hours combined, in UTC ' do
46
+ subject { Ecommerce::AttributeHandler.new('2014-06-01T14:17:56Z') }
47
+
48
+ it 'returns a Date object' do
49
+ expect(subject.handle).to eq(DateTime.new(2014, 6, 1, 14, 17, 56))
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,26 @@
1
+ require "spec_helper"
2
+
3
+ describe Ecommerce::Client do
4
+
5
+ describe "#authenticated?" do
6
+ context "with a valid token" do
7
+ subject { described_class.new(Ecommerce.configuration.token, Ecommerce.configuration.secret) }
8
+
9
+ it "returns true" do
10
+ VCR.use_cassette("client/authenticated/true") do
11
+ expect(subject.authenticated?('rexpense-custom-monthly-brl-5250')).to be_truthy
12
+ end
13
+ end
14
+ end
15
+
16
+ context "with an invalid token" do
17
+ subject { described_class.new("FAKE-TOKEN", "FAKE-SECRET") }
18
+
19
+ it "returns false" do
20
+ VCR.use_cassette("client/authenticated/false") do
21
+ expect(subject.authenticated?('rexpense-custom-monthly-brl-5250')).to be_falsey
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,21 @@
1
+ require "spec_helper"
2
+
3
+ describe Ecommerce::Configuration do
4
+
5
+ it "uses the production Ecommerce URL by default" do
6
+ expect(subject.url).to eq "https://ecommerce.myfreecomm.com.br"
7
+ end
8
+
9
+ it "uses a default user agent" do
10
+ expect(subject.user_agent).to eq "Ecommerce Ruby Client v#{Ecommerce::VERSION}"
11
+ end
12
+
13
+ it "uses ecommerce token as nil by default" do
14
+ expect(subject.token).to be_nil
15
+ end
16
+
17
+ it "uses ecommerce secret as nil by default" do
18
+ expect(subject.secret).to be_nil
19
+ end
20
+
21
+ end
@@ -0,0 +1,23 @@
1
+ require "spec_helper"
2
+
3
+ describe Ecommerce::Request do
4
+ describe "#run" do
5
+ subject { described_class.new({ method: 'post', params: {}, url: Ecommerce.configuration.url, user_agent: 'My Test User-Agent', authorization_hash: 'my-auth-hash' }) }
6
+
7
+ it "does a request using Typhoeus::Request" do
8
+ expect(Typhoeus::Request).to receive(:new).with("http://sandbox.ecommerce.myfreecomm.com.br", { method: "post", params: {}, headers: { "Accept" => "application/json", "Content-Type" => "application/json", "User-Agent" => "My Test User-Agent", "Authorization" => "Basic my-auth-hash" }, accept_encoding: "gzip" }).and_return(double(run: true, response: true))
9
+ subject.run
10
+ end
11
+
12
+ it "invokes Typhoeus::Request#run" do
13
+ expect_any_instance_of(Typhoeus::Request).to receive(:run)
14
+ subject.run
15
+ end
16
+
17
+ it "invokes Typhoeus::Request#response" do
18
+ allow_any_instance_of(Typhoeus::Request).to receive(:run)
19
+ expect_any_instance_of(Typhoeus::Request).to receive(:response)
20
+ subject.run
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,82 @@
1
+ require "spec_helper"
2
+
3
+ describe Ecommerce::Resources::OrderCollection do
4
+ let(:response) { double(headers: {"Link" => "<http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/?page=3>; rel=next, <http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/?page=1>; rel=prev, <http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/?page=3>; rel=last, <http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/?page=1>; rel=first"},
5
+ body: "[{\"is_paid\": false, \"address_number\": null, \"plan_change_urls\": {}, \"activation_expired\": false, \"number\": 2608, \"address_state\": null, \"global_account\": \"e5732007-7989-4372-8e72-9ec8cf6ee046\", \"api_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2608/\", \"is_trial\": false, \"document_number\": null, \"checkout_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/rexpense-custom-monthly-brl-5250/2608/checkout/\", \"active_until\": null, \"charge_day\": 25, \"address_quarter\": null, \"activated_at\": null, \"is_active\": false, \"user_code\": \"\", \"address\": null, \"is_recurring\": true, \"address_city\": null, \"plan_slug\": \"rexpense-custom-monthly-brl-5250\", \"address_complement\": null, \"discounts_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2608/discounts/\", \"created_at\": \"2015-03-25 17:54:42\", \"adjustments_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2608/adjustments/\", \"next_payment_at\": null, \"amount\": \"52.50\", \"client_email\": \"jaime.lannister@mailinator.com\", \"zip_code\": null, \"client_name\": \"\", \"invoices_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2608/invoices/\"}, {\"is_paid\": false, \"address_number\": null, \"plan_change_urls\": {}, \"activation_expired\": false, \"number\": 2609, \"address_state\": null, \"global_account\": \"e5732007-7989-4372-8e72-9ec8cf6ee046\", \"api_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2609/\", \"is_trial\": false, \"document_number\": null, \"checkout_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/rexpense-custom-monthly-brl-5250/2609/checkout/\", \"active_until\": null, \"charge_day\": 25, \"address_quarter\": null, \"activated_at\": null, \"is_active\": false, \"user_code\": \"\", \"address\": null, \"is_recurring\": true, \"address_city\": null, \"plan_slug\": \"rexpense-custom-monthly-brl-5250\", \"address_complement\": null, \"discounts_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2609/discounts/\", \"created_at\": \"2015-03-25 17:55:32\", \"adjustments_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2609/adjustments/\", \"next_payment_at\": null, \"amount\": \"52.50\", \"client_email\": \"jaime.lannister@mailinator.com\", \"zip_code\": null, \"client_name\": \"\", \"invoices_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2609/invoices/\"}, {\"is_paid\": false, \"address_number\": null, \"plan_change_urls\": {}, \"activation_expired\": false, \"number\": 2610, \"address_state\": null, \"global_account\": \"e5732007-7989-4372-8e72-9ec8cf6ee046\", \"api_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2610/\", \"is_trial\": false, \"document_number\": null, \"checkout_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/rexpense-custom-monthly-brl-5250/2610/checkout/\", \"active_until\": null, \"charge_day\": 25, \"address_quarter\": null, \"activated_at\": null, \"is_active\": false, \"user_code\": \"\", \"address\": null, \"is_recurring\": true, \"address_city\": null, \"plan_slug\": \"rexpense-custom-monthly-brl-5250\", \"address_complement\": null, \"discounts_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2610/discounts/\", \"created_at\": \"2015-03-25 17:59:15\", \"adjustments_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2610/adjustments/\", \"next_payment_at\": null, \"amount\": \"52.50\", \"client_email\": \"jaime.lannister@mailinator.com\", \"zip_code\": null, \"client_name\": \"\", \"invoices_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2610/invoices/\"}, {\"is_paid\": false, \"address_number\": null, \"plan_change_urls\": {}, \"activation_expired\": false, \"number\": 2611, \"address_state\": null, \"global_account\": \"e5732007-7989-4372-8e72-9ec8cf6ee046\", \"api_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2611/\", \"is_trial\": false, \"document_number\": null, \"checkout_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/rexpense-custom-monthly-brl-5250/2611/checkout/\", \"active_until\": null, \"charge_day\": 25, \"address_quarter\": null, \"activated_at\": null, \"is_active\": false, \"user_code\": \"\", \"address\": null, \"is_recurring\": true, \"address_city\": null, \"plan_slug\": \"rexpense-custom-monthly-brl-5250\", \"address_complement\": null, \"discounts_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2611/discounts/\", \"created_at\": \"2015-03-25 18:03:33\", \"adjustments_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2611/adjustments/\", \"next_payment_at\": null, \"amount\": \"52.50\", \"client_email\": \"jaime.lannister@mailinator.com\", \"zip_code\": null, \"client_name\": \"\", \"invoices_url\": \"http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2611/invoices/\"}]") }
6
+
7
+ subject { described_class.new(response) }
8
+
9
+ describe '#build' do
10
+ it 'builds orders' do
11
+ expect(subject).to receive(:build_orders)
12
+ subject.build
13
+ end
14
+
15
+ it 'returns Ecommerce::Resources::OrderCollection object' do
16
+ expect(subject.build.class).to eq(Ecommerce::Resources::OrderCollection)
17
+ end
18
+ end
19
+
20
+ describe '#build_orders' do
21
+ it 'builds an array of Ecommerce::Resources::Order' do
22
+ expect(Ecommerce::Resources::Order).to receive(:new).exactly(4).times
23
+ subject.build
24
+ end
25
+ end
26
+
27
+ describe '#next_page' do
28
+ it 'returns next page (3)' do
29
+ expect(subject.next_page).to eq(3)
30
+ end
31
+
32
+ context 'when there is no next page' do
33
+ let(:response) { double(headers: {"Link" => ""}, body: "") }
34
+
35
+ it 'returns nil' do
36
+ expect(subject.next_page).to be_nil
37
+ end
38
+ end
39
+ end
40
+
41
+ describe '#last_page' do
42
+ it 'returns last page (3)' do
43
+ expect(subject.last_page).to eq(3)
44
+ end
45
+
46
+ context 'when there is no last page' do
47
+ let(:response) { double(headers: {"Link" => ""}, body: "") }
48
+
49
+ it 'returns nil' do
50
+ expect(subject.last_page).to be_nil
51
+ end
52
+ end
53
+ end
54
+
55
+ describe '#previous_page' do
56
+ it 'returns previous page (3)' do
57
+ expect(subject.previous_page).to eq(1)
58
+ end
59
+
60
+ context 'when there is no previous page' do
61
+ let(:response) { double(headers: {"Link" => ""}, body: "") }
62
+
63
+ it 'returns nil' do
64
+ expect(subject.previous_page).to be_nil
65
+ end
66
+ end
67
+ end
68
+
69
+ describe '#first_page' do
70
+ it 'returns first page (3)' do
71
+ expect(subject.first_page).to eq(1)
72
+ end
73
+
74
+ context 'when there is no first page' do
75
+ let(:response) { double(headers: {"Link" => ""}, body: "") }
76
+
77
+ it 'returns nil' do
78
+ expect(subject.first_page).to be_nil
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,155 @@
1
+ require "spec_helper"
2
+
3
+ describe Ecommerce::Resources::Order do
4
+ describe '.create', vcr: true do
5
+ let(:params) { { global_account: 'e5732007-7989-4372-8e72-9ec8cf6ee046',
6
+ client_email: 'jaime.lannister@mailinator.com',
7
+ identity: '8923199e-6c43-415a-bbd1-2e302fdf8d96' } }
8
+
9
+ subject { described_class.create('rexpense-custom-monthly-brl-5250', params) }
10
+
11
+ context 'when success' do
12
+ it 'returns body' do
13
+ expect(subject.is_paid).to be_falsy
14
+ expect(subject.address_number).to be_nil
15
+ expect(subject.plan_change_urls).to eq({})
16
+ expect(subject.activation_expired).to be_falsy
17
+ expect(subject.number).to eq(2609)
18
+ expect(subject.address_state).to be_nil
19
+ expect(subject.global_account).to eq('e5732007-7989-4372-8e72-9ec8cf6ee046')
20
+ expect(subject.api_url).to eq("http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2609/")
21
+ expect(subject.is_trial).to be_falsy
22
+ expect(subject.document_number).to be_nil
23
+ expect(subject.checkout_url).to eq("http://sandbox.ecommerce.myfreecomm.com.br/rexpense-custom-monthly-brl-5250/2609/checkout/")
24
+ expect(subject.active_until).to be_nil
25
+ expect(subject.charge_day).to eq(25)
26
+ expect(subject.address_quarter).to be_nil
27
+ expect(subject.activated_at).to be_nil
28
+ expect(subject.is_active).to be_falsy
29
+ expect(subject.user_code).to be_empty
30
+ expect(subject.address).to be_nil
31
+ expect(subject.is_recurring).to be_truthy
32
+ expect(subject.address_city).to be_nil
33
+ expect(subject.plan_slug).to eq("rexpense-custom-monthly-brl-5250")
34
+ expect(subject.address_complement).to be_nil
35
+ expect(subject.discounts_url).to eq("http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2609/discounts/")
36
+ expect(subject.created_at).to eq(DateTime.new(2015, 3, 25, 17, 55, 32))
37
+ expect(subject.adjustments_url).to eq("http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2609/adjustments/")
38
+ expect(subject.next_payment_at).to be_nil
39
+ expect(subject.amount).to eq(52.5)
40
+ expect(subject.client_email).to eq("jaime.lannister@mailinator.com")
41
+ expect(subject.zip_code).to be_nil
42
+ expect(subject.client_name).to be_empty
43
+ expect(subject.invoices_url).to eq("http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2609/invoices/")
44
+ end
45
+
46
+ context 'when sending not all required parameters' do
47
+ before { params.delete(:global_account) }
48
+
49
+ it 'raises error BarRequest' do
50
+ expect{subject}.to raise_error(Ecommerce::RequestError)
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ describe '.find_all', vcr: true do
57
+ let(:params) { { global_account: 'e5732007-7989-4372-8e72-9ec8cf6ee046',
58
+ client_email: 'jaime.lannister@mailinator.com',
59
+ identity: '8923199e-6c43-415a-bbd1-2e302fdf8d96' } }
60
+
61
+ context 'when success' do
62
+ subject { described_class.find_all('rexpense-custom-monthly-brl-5250') }
63
+
64
+ it 'returns a find_all of orders' do
65
+ expect(subject.class).to eq(Ecommerce::Resources::OrderCollection)
66
+ expect(subject.orders.first.plan_slug).to eq("rexpense-custom-monthly-brl-5250")
67
+ expect(subject.orders.first.class).to eq(Ecommerce::Resources::Order)
68
+ expect(subject.orders.count).to eq(4)
69
+ end
70
+ end
71
+
72
+ context 'when not found' do
73
+ subject { described_class.find_all('wrong-slug') }
74
+
75
+ it 'raises NotFound' do
76
+ expect{ subject }.to raise_error(Ecommerce::RequestError)
77
+ end
78
+ end
79
+ end
80
+
81
+ describe '.find', vcr: true do
82
+ context 'when success' do
83
+ subject { described_class.find(2609, 'rexpense-custom-monthly-brl-5250') }
84
+
85
+ it 'returns order object' do
86
+ expect(subject.is_paid).to be_falsy
87
+ expect(subject.address_number).to be_nil
88
+ expect(subject.plan_change_urls).to eq({})
89
+ expect(subject.activation_expired).to be_falsy
90
+ expect(subject.number).to eq(2609)
91
+ expect(subject.address_state).to be_nil
92
+ expect(subject.global_account).to eq('e5732007-7989-4372-8e72-9ec8cf6ee046')
93
+ expect(subject.api_url).to eq("http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2609/")
94
+ expect(subject.is_trial).to be_falsy
95
+ expect(subject.document_number).to be_nil
96
+ expect(subject.checkout_url).to eq("http://sandbox.ecommerce.myfreecomm.com.br/rexpense-custom-monthly-brl-5250/2609/checkout/")
97
+ expect(subject.active_until).to be_nil
98
+ expect(subject.charge_day).to eq(25)
99
+ expect(subject.address_quarter).to be_nil
100
+ expect(subject.activated_at).to be_nil
101
+ expect(subject.is_active).to be_falsy
102
+ expect(subject.user_code).to be_empty
103
+ expect(subject.address).to be_nil
104
+ expect(subject.is_recurring).to be_truthy
105
+ expect(subject.address_city).to be_nil
106
+ expect(subject.plan_slug).to eq("rexpense-custom-monthly-brl-5250")
107
+ expect(subject.address_complement).to be_nil
108
+ expect(subject.discounts_url).to eq("http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2609/discounts/")
109
+ expect(subject.created_at).to eq(DateTime.new(2015, 3, 25, 17, 55, 32))
110
+ expect(subject.adjustments_url).to eq("http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2609/adjustments/")
111
+ expect(subject.next_payment_at).to be_nil
112
+ expect(subject.amount).to eq(52.5)
113
+ expect(subject.client_email).to eq("jaime.lannister@mailinator.com")
114
+ expect(subject.zip_code).to be_nil
115
+ expect(subject.client_name).to be_empty
116
+ expect(subject.invoices_url).to eq("http://sandbox.ecommerce.myfreecomm.com.br/api/orders/rexpense-custom-monthly-brl-5250/2609/invoices/")
117
+ end
118
+ end
119
+
120
+ context 'when not found' do
121
+ subject { described_class.find(2609, 'wrong-slug') }
122
+
123
+ it 'raises NotFound' do
124
+ expect{ subject }.to raise_error(Ecommerce::RequestError)
125
+ end
126
+ end
127
+ end
128
+
129
+ describe '.destroy', vcr: true do
130
+ context 'when success' do
131
+ subject { described_class.destroy(2609, 'rexpense-custom-monthly-brl-5250') }
132
+
133
+ it 'returns empty body' do
134
+ expect(subject).to be_empty
135
+ end
136
+ end
137
+
138
+ context 'when not found' do
139
+ subject { described_class.destroy(2609, 'wrong-slug') }
140
+
141
+ it 'raises NotFound' do
142
+ expect{ subject }.to raise_error(Ecommerce::RequestError)
143
+ end
144
+ end
145
+ end
146
+
147
+ describe '#destroy' do
148
+ subject { described_class.new({ number: 2609, plan_slug: 'rexpense-custom-monthly-brl-5250' }) }
149
+
150
+ it 'invokes Ecommerce::Resources::Order.destroy' do
151
+ expect(Ecommerce::Resources::Order).to receive(:destroy).with(2609, 'rexpense-custom-monthly-brl-5250')
152
+ subject.destroy
153
+ end
154
+ end
155
+ end