ecommerce-client 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/README.md +25 -2
  4. data/ecommerce-client.gemspec +1 -0
  5. data/lib/ecommerce.rb +5 -0
  6. data/lib/ecommerce/attribute_handler.rb +1 -1
  7. data/lib/ecommerce/resources/account_order.rb +26 -0
  8. data/lib/ecommerce/resources/collection.rb +57 -0
  9. data/lib/ecommerce/resources/invoice_order.rb +52 -0
  10. data/lib/ecommerce/resources/invoice_order_collection.rb +20 -0
  11. data/lib/ecommerce/resources/invoice_plan.rb +27 -0
  12. data/lib/ecommerce/resources/order.rb +18 -13
  13. data/lib/ecommerce/resources/order_collection.rb +6 -48
  14. data/lib/ecommerce/version.rb +1 -1
  15. data/spec/ecommerce/attribute_handler_spec.rb +24 -0
  16. data/spec/ecommerce/resources/account_order_spec.rb +24 -0
  17. data/spec/ecommerce/resources/collection_spec.rb +70 -0
  18. data/spec/ecommerce/resources/invoice_order_collection_spec.rb +20 -0
  19. data/spec/ecommerce/resources/invoice_order_spec.rb +54 -0
  20. data/spec/ecommerce/resources/invoice_plan_spec.rb +23 -0
  21. data/spec/ecommerce/resources/order_collection_spec.rb +7 -69
  22. data/spec/ecommerce/resources/order_spec.rb +46 -5
  23. data/spec/spec_helper.rb +3 -0
  24. data/spec/vcr_cassettes/Ecommerce/Resources_AccountOrder_find_all_when_not_found_raises_NotFound.yml +219 -0
  25. data/spec/vcr_cassettes/Ecommerce/Resources_AccountOrder_find_all_when_success_returns_a_find_all_of_orders.yml +297 -0
  26. data/spec/vcr_cassettes/Ecommerce/Resources_InvoiceOrder_find_all_when_not_found_raises_NotFound.yml +30 -0
  27. data/spec/vcr_cassettes/Ecommerce/Resources_InvoiceOrder_find_all_when_success_returns_a_find_all_of_orders.yml +73 -0
  28. data/spec/vcr_cassettes/Ecommerce/Resources_InvoiceOrder_find_when_not_found_raises_NotFound.yml +46 -0
  29. data/spec/vcr_cassettes/Ecommerce/Resources_InvoiceOrder_find_when_success_returns_order_object.yml +59 -0
  30. data/spec/vcr_cassettes/Ecommerce/Resources_InvoicePlan_find_all_when_not_found_raises_NotFound.yml +46 -0
  31. data/spec/vcr_cassettes/Ecommerce/Resources_InvoicePlan_find_all_when_success_returns_a_find_all_of_orders.yml +63 -0
  32. data/spec/vcr_cassettes/Ecommerce/Resources_Order_update_when_sending_not_all_required_parameters_raises_error_BarRequ.yml +49 -0
  33. data/spec/vcr_cassettes/Ecommerce/Resources_Order_update_with_success_returns_order_information_on_body.yml +61 -0
  34. metadata +52 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7687281e6e8112b50c562dc4124e6ac17b10f083
4
- data.tar.gz: fa8ef5444600d9d6be649eb8ca5742b8f6a983fa
3
+ metadata.gz: 372faf95c34b3244ee4aa37429bf146c59df0541
4
+ data.tar.gz: a86671860634547190e26e59d38935ec23949cad
5
5
  SHA512:
6
- metadata.gz: ed884b916edad83b57c22eef2da3068c711f3cf158f7d4920b86ccef36175442545ed916c0cd407734a65f6d7d40f0a8ff27fd7f82434aef15c90c8cf433de1d
7
- data.tar.gz: 96a8edfad2126ba901e4364948de669e0753f5d315992c20f3103efb79c3c7c90d16fa243a81913dc33f4267937b184a5dff9a595b0c5820e7cceb32d5e6f72b
6
+ metadata.gz: 5af1bcf38deac81ff229e0e654446d9d4c658ca849b47a096c1c5fe8826bd9b843b9c300cfb71eac6f5a471853c4141191628cca22078bc525869534d5a58ead
7
+ data.tar.gz: f32b025ac5266fd9873bf7512ea7d05541ef6fae88810ea6645f291d63aa28ec61968f6ad598e9c69e4be761a2336e963dec921f8310f9fa182e7ca5f5241e93
data/.gitignore CHANGED
@@ -28,4 +28,5 @@ mkmf.log
28
28
  *~
29
29
  .ruby-gemset
30
30
  .ruby-version
31
- doc
31
+ doc
32
+ .rvmrc
data/README.md CHANGED
@@ -1,6 +1,12 @@
1
+ [![Gem Version](https://badge.fury.io/rb/ecommerce-client.svg)](http://badge.fury.io/rb/ecommerce-client)
2
+ [![Build Status](https://travis-ci.org/myfreecomm/ecommerce-client-ruby.svg?branch=master)](https://travis-ci.org/myfreecomm/ecommerce-client-ruby)
3
+ [![Code Climate](https://codeclimate.com/github/myfreecomm/ecommerce-client-ruby/badges/gpa.svg)](https://codeclimate.com/github/myfreecomm/ecommerce-client-ruby)
4
+ [![Test Coverage](https://codeclimate.com/github/myfreecomm/ecommerce-client-ruby/badges/coverage.svg)](https://codeclimate.com/github/myfreecomm/ecommerce-client-ruby)
5
+ [![Inline docs](http://inch-ci.org/github/myfreecomm/ecommerce-client-ruby.svg)](http://inch-ci.org/github/myfreecomm/ecommerce-client-ruby)
6
+
1
7
  # Ecommerce Client
2
8
 
3
- A Ruby client for Myfreecomm's Ecommerce REST API
9
+ A Ruby client for [Myfreecomm's Ecommerce REST API](http://myfreecomm.github.io/passaporte-web/ecommerce/api/index.html).
4
10
 
5
11
  ## Installation
6
12
 
@@ -16,9 +22,26 @@ Or install it yourself as:
16
22
 
17
23
  $ gem install ecommerce-client
18
24
 
25
+ ## Configuration
26
+
27
+ To access the Ecommerce API, you must provide the *token* and the *secret*:
28
+
29
+ ```ruby
30
+ Ecommerce.configuration do |config|
31
+ config.token = 'YOUR-TOKEN'
32
+ config.secret = 'YOUR-SECRET'
33
+ end
34
+ ```
35
+
19
36
  ## Usage
20
37
 
21
- TODO: Write usage instructions here
38
+ Ecommerce endpoints are represented as Resources. To check the list of all available Resources, please visit [Ecommerce::Resources](https://github.com/myfreecomm/ecommerce-client-ruby/tree/master/lib/ecommerce/resources) module folder.
39
+
40
+ ```ruby
41
+ # listing orders of a plan
42
+ Ecommerce::Resources::Order.find_all('specific-plan')
43
+ => #<Ecommerce::Resources::OrderCollection:0x000000028739b0...>
44
+ ```
22
45
 
23
46
  ## Contributing
24
47
 
@@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency 'rspec', "~> 3.2"
27
27
  spec.add_development_dependency "pry", "~> 0.10"
28
28
  spec.add_development_dependency "vcr", "~> 2.9"
29
+ spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4"
29
30
  end
@@ -7,8 +7,13 @@ require "ecommerce/client"
7
7
  require "ecommerce/attribute_handler"
8
8
 
9
9
  require "ecommerce/resources/base"
10
+ require "ecommerce/resources/collection"
11
+ require "ecommerce/resources/account_order"
10
12
  require "ecommerce/resources/order"
11
13
  require "ecommerce/resources/order_collection"
14
+ require "ecommerce/resources/invoice_order"
15
+ require "ecommerce/resources/invoice_order_collection"
16
+ require "ecommerce/resources/invoice_plan"
12
17
 
13
18
  module Ecommerce
14
19
  def self.configuration
@@ -48,4 +48,4 @@ module Ecommerce
48
48
  DateTime.parse(attribute)
49
49
  end
50
50
  end
51
- end
51
+ end
@@ -0,0 +1,26 @@
1
+ module Ecommerce
2
+ module Resources
3
+ #
4
+ # A wrapper to Ecommerce orders API
5
+ #
6
+ # [API]
7
+ # Documentation: http://myfreecomm.github.io/passaporte-web/ecommerce/api/index.html
8
+ #
9
+ class AccountOrder < Base
10
+ #
11
+ # Lists all Orders of an account and return a collection with orders and pagination information (represented by Ecommerce::Resources::Collection)
12
+ #
13
+ # [API]
14
+ # Method: <tt>GET /api/accounts/:account_uuid/orders/</tt>
15
+ #
16
+ # Documentation: http://myfreecomm.github.io/passaporte-web/ecommerce/api/orders.html#listagem-das-ordens-de-compra-de-uma-conta
17
+ #
18
+ def self.find_all(account_uuid, page = 1, limit = 20)
19
+ body = { page: page, limit: limit }
20
+ client.get("/api/accounts/#{account_uuid}/orders/", body: body) do |response|
21
+ Ecommerce::Resources::OrderCollection.build(response)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,57 @@
1
+ module Ecommerce
2
+ module Resources
3
+ #
4
+ # A wrapper to Ecommerce collection returns from API. This wrapper represents a collection and it's responsible for processing pagination information as well.
5
+ #
6
+ class Collection < Base
7
+ PAGE_REGEX = /page=(\d+)/
8
+
9
+ attr_reader :response, :collection, :headers
10
+
11
+ def initialize(response)
12
+ @response = response
13
+ @collection = []
14
+ @headers = response.headers['Link'].split(',')
15
+ end
16
+
17
+ def self.build(response)
18
+ self.new(response).build
19
+ end
20
+
21
+ def build
22
+ build_collection
23
+ self
24
+ end
25
+
26
+ def next_page
27
+ page_for(:next)
28
+ end
29
+
30
+ def last_page
31
+ page_for(:last)
32
+ end
33
+
34
+ def previous_page
35
+ page_for(:prev)
36
+ end
37
+
38
+ def first_page
39
+ page_for(:first)
40
+ end
41
+
42
+ private
43
+
44
+ def page_for(page_rel)
45
+ header_link_for(page_rel).match(PAGE_REGEX)[1].to_i rescue nil
46
+ end
47
+
48
+ def header_link_for(rel)
49
+ headers.select{|n| n =~ /rel=#{rel}/}.first
50
+ end
51
+
52
+ def build_collection
53
+ raise NotImplementedError
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,52 @@
1
+ module Ecommerce
2
+ module Resources
3
+ #
4
+ # A wrapper to Ecommerce orders invoice API
5
+ #
6
+ # [API]
7
+ # Documentation: http://myfreecomm.github.io/passaporte-web/ecommerce/api/index.html
8
+ #
9
+ class InvoiceOrder < Base
10
+ attr_reader :redeemed_adjustments, :order_url, :url, :created_at, :activated_at, :number, :user_data,
11
+ :amount_with_adjustments, :amount, :paid, :payment_method, :plan_data, :charge_date,
12
+ :next_payment_date
13
+
14
+ #
15
+ # Lists all invoice of an orders and return a collection of invoice with
16
+ # pagination information (represented by Ecommerce::Resources::InvoiceOrderCollection)
17
+ #
18
+ # [API]
19
+ # Method: <tt>GET /api/orders/:slug/:id/invoices/ </tt>
20
+ #
21
+ # Documentation: http://myfreecomm.github.io/passaporte-web/ecommerce/api/orders.html#listagem-das-ordens-de-compra-de-uma-conta
22
+ #
23
+ def self.find_all(order_id, slug, page = 1, limit = 20)
24
+ body = { page: page, limit: limit }
25
+ client.get("/api/orders/#{slug}/#{order_id}/invoices/", body: body) do |response|
26
+ Ecommerce::Resources::InvoiceOrderCollection.build(response)
27
+ end
28
+ end
29
+
30
+ #
31
+ # Finds an invoice of an order
32
+ #
33
+ # [API]
34
+ # Method: <tt>GET /api/orders/:slug/:order_id/invoices/:id/</tt>
35
+ #
36
+ # Documentation: http://myfreecomm.github.io/passaporte-web/ecommerce/api/orders.html#listagem-das-ordens-de-compra-de-uma-conta
37
+ #
38
+ def self.find(invoice_order_id, order_id, slug)
39
+ client.get("/api/orders/#{slug}/#{order_id}/invoices/#{invoice_order_id}/") do |response|
40
+ build_invoice(response)
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def self.build_invoice(response)
47
+ invoice_attributes = parsed_body(response)
48
+ invoice_attributes.empty? ? {} : new(invoice_attributes)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,20 @@
1
+ module Ecommerce
2
+ module Resources
3
+ #
4
+ # A wrapper to Ecommerce orders API.
5
+ #
6
+ class InvoiceOrderCollection < Collection
7
+ def orders
8
+ collection
9
+ end
10
+
11
+ private
12
+
13
+ def build_collection
14
+ Ecommerce::Resources::Base.parsed_body(response).each do |invoice_attributes|
15
+ collection.push(Ecommerce::Resources::InvoiceOrder.new(invoice_attributes))
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ module Ecommerce
2
+ module Resources
3
+ #
4
+ # A wrapper to Ecommerce invoice per plan API
5
+ #
6
+ # [API]
7
+ # Documentation: http://myfreecomm.github.io/passaporte-web/ecommerce/api/index.html
8
+ #
9
+ class InvoicePlan < Base
10
+ #
11
+ # Lists all paid invoices of a plan and return a collection of invoice with
12
+ # pagination information (represented by Ecommerce::Resources::InvoiceOrderCollection)
13
+ #
14
+ # [API]
15
+ # Method: <tt>GET /api/:slug/invoices/paid/
16
+ #
17
+ # Documentation: http://myfreecomm.github.io/passaporte-web/ecommerce/api/orders.html#listagem-das-faturas-pagas-para-um-plano
18
+ #
19
+ def self.find_all(slug, page = 1, limit = 20)
20
+ body = { page: page, limit: limit }
21
+ client.get("/api/#{slug}/invoices/paid/", body: body) do |response|
22
+ Ecommerce::Resources::InvoiceOrderCollection.build(response)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,15 +1,12 @@
1
1
  module Ecommerce
2
2
  module Resources
3
-
4
3
  #
5
4
  # A wrapper to Ecommerce orders API
6
5
  #
7
6
  # [API]
8
7
  # Documentation: http://myfreecomm.github.io/passaporte-web/ecommerce/api/index.html
9
8
  #
10
-
11
9
  class Order < Base
12
-
13
10
  attr_reader :is_paid, :address_number, :plan_change_urls, :activation_expired, :number, :is_trial,
14
11
  :address_state, :global_account, :api_url, :document_number, :checkout_url, :active_until,
15
12
  :charge_day, :address_quarter, :activated_at, :is_active, :user_code, :address, :is_recurring,
@@ -24,24 +21,24 @@ module Ecommerce
24
21
  #
25
22
  # Documentation: http://myfreecomm.github.io/passaporte-web/ecommerce/api/orders.html#criacao-de-ordem-de-compra
26
23
  #
27
-
28
24
  def self.create(slug, params)
29
- client.post("/api/orders/#{slug}/", { body: params }) do |response|
25
+ client.post("/api/orders/#{slug}/", body: params) do |response|
30
26
  build_order(response)
31
27
  end
32
28
  end
33
29
 
34
30
  #
35
- # Lists all Orders of a slug and return a collection with orders and pagination information (represented by Ecommerce::Resources::OrderCollection)
31
+ # Lists all Orders of a slug and return a collection with orders
32
+ # and pagination information (represented by Ecommerce::Resources::OrderCollection)
36
33
  #
37
34
  # [API]
38
35
  # Method: <tt>GET /api/orders/:slug/</tt>
39
36
  #
40
37
  # Documentation: http://myfreecomm.github.io/passaporte-web/ecommerce/api/orders.html#listagem-de-ordens-de-compra
41
38
  #
42
-
43
39
  def self.find_all(slug, page = 1, limit = 20)
44
- client.get("/api/orders/#{slug}/", { body: {page: page, limit: limit} }) do |response|
40
+ body = { page: page, limit: limit }
41
+ client.get("/api/orders/#{slug}/", body: body) do |response|
45
42
  Ecommerce::Resources::OrderCollection.build(response)
46
43
  end
47
44
  end
@@ -54,7 +51,6 @@ module Ecommerce
54
51
  #
55
52
  # Documentation: http://myfreecomm.github.io/passaporte-web/ecommerce/api/orders.html#detalhes-de-uma-ordem-de-compra
56
53
  #
57
-
58
54
  def self.find(order_id, slug)
59
55
  client.get("/api/orders/#{slug}/#{order_id}/") do |response|
60
56
  build_order(response)
@@ -69,22 +65,31 @@ module Ecommerce
69
65
  #
70
66
  # Documentation: http://myfreecomm.github.io/passaporte-web/ecommerce/api/orders.html#delete-api-orders-slug-id
71
67
  #
72
-
73
68
  def self.destroy(order_id, slug)
74
69
  client.delete("/api/orders/#{slug}/#{order_id}/") do |response|
75
70
  build_order(response)
76
71
  end
77
72
  end
78
73
 
79
- def destroy
80
- self.class.destroy(number, plan_slug)
74
+ #
75
+ # Updates an Order client information
76
+ #
77
+ # [API]
78
+ # Method: <tt>PUT /api/orders/:slug/:order_id/</tt>
79
+ #
80
+ # Documentation for available and required fields: http://myfreecomm.github.io/passaporte-web/ecommerce/api/orders.html#put-api-orders-slug-id
81
+ #
82
+ def self.update(order_id, slug, order_params={})
83
+ client.put("/api/orders/#{slug}/#{order_id}/", body: order_params) do |response|
84
+ build_order(response)
85
+ end
81
86
  end
82
87
 
83
88
  private
84
89
 
85
90
  def self.build_order(response)
86
91
  order_attributes = parsed_body(response)
87
- order_attributes.empty? ? {} : self.new(order_attributes)
92
+ order_attributes.empty? ? {} : new(order_attributes)
88
93
  end
89
94
  end
90
95
  end
@@ -1,60 +1,18 @@
1
1
  module Ecommerce
2
2
  module Resources
3
-
4
3
  #
5
- # A wrapper to Ecommerce orders API. This wrapper represents a collection of orders and it's responsible for processing pagination information as well.
4
+ # A wrapper to Ecommerce orders API.
6
5
  #
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)
6
+ class OrderCollection < Collection
7
+ def orders
8
+ collection
43
9
  end
44
10
 
45
11
  private
46
12
 
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
13
+ def build_collection
56
14
  Ecommerce::Resources::Base.parsed_body(response).each do |order_attributes|
57
- orders.push(Ecommerce::Resources::Order.new(order_attributes))
15
+ collection.push(Ecommerce::Resources::Order.new(order_attributes))
58
16
  end
59
17
  end
60
18
  end
@@ -1,3 +1,3 @@
1
1
  module Ecommerce
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -49,6 +49,30 @@ describe Ecommerce::AttributeHandler do
49
49
  expect(subject.handle).to eq(DateTime.new(2014, 6, 1, 14, 17, 56))
50
50
  end
51
51
  end
52
+
53
+ context 'when parameter is nil' do
54
+ subject { Ecommerce::AttributeHandler.new(nil) }
55
+
56
+ it 'returns parameter' do
57
+ expect(subject.handle).to be_nil
58
+ end
59
+ end
60
+
61
+ context 'when parameter is blank' do
62
+ subject { Ecommerce::AttributeHandler.new('') }
63
+
64
+ it 'returns parameter' do
65
+ expect(subject.handle).to eq('')
66
+ end
67
+ end
68
+
69
+ context 'when parameter is different from String' do
70
+ subject { Ecommerce::AttributeHandler.new([]) }
71
+
72
+ it 'returns parameter' do
73
+ expect(subject.handle).to eq([])
74
+ end
75
+ end
52
76
  end
53
77
  end
54
78
  end