dooca_commerce 1.0.0 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 847b94bb2b5eeccc601e7ea0c809f686a42142212b6ddd27cc35cf09dc42c5fa
4
- data.tar.gz: 415a7f99123aa96c5c42568e62a0c43ccbb2771f58263339667a320bcccd4579
3
+ metadata.gz: 69ac8fe0db3c29bd4dd00306455b950acb6fdd4d9e904c7c15c70dd9d25609ce
4
+ data.tar.gz: 2a71165d41f191332a2135496711c758e9b3df6011b504d22c00d162549141e3
5
5
  SHA512:
6
- metadata.gz: 1127f0083c39c595efd2de8791de642fe04adfee9cc2778fc44e70a1269ca34d0c05023fccfdd9002fad55cfa2149fe76863cd55c93e52ac6c0e250d64b57b78
7
- data.tar.gz: 9fcc3ebc6c6af3b9fa0002f7e80d61482067998b2a16f3857e8b2b15fc0a98f1794a95510b2609470bf4c9575c7feb844d401698795073aaab009ad0264c9f5a
6
+ metadata.gz: a7320cb8afa53bc1b9b268a98f9e5c583cc9743151af3fb36f2f1c589e222cf8e2fbf52998e960634f6e63ff49b8e72b6555b7cd58017316c99896123fff8ea5
7
+ data.tar.gz: 837449914984dfe36ed627e2ea3a790204550341a0847472cf5b7560b57d0e7c0e34ea92ff27e59fa85130621a345cfcf8eb63e3104051f370be410143c05ca9
@@ -1,5 +1,177 @@
1
+ require 'faraday'
2
+ require 'response_handler'
3
+
1
4
  class DoocaCommerce
2
- def self.hi
3
- puts "Hello world!"
5
+
6
+ def initialize(token:)
7
+ @token = token
8
+ @handler = ResponseHandler
9
+ end
10
+
11
+ attr_reader :token, :handler
12
+
13
+ def fetch_products(query = {})
14
+ response = connection.get("/products", query)
15
+
16
+ handler.new(response).parse!
17
+ end
18
+
19
+ def fetch_product(query = {})
20
+ response = connection.get("/products", query)
21
+
22
+ handler.new(response, single_from_list: true).parse!
23
+ end
24
+
25
+ def fetch_variations(query = {})
26
+ response = connection.get("/variations", query)
27
+
28
+ handler.new(response).parse!
29
+ end
30
+
31
+ def fetch_variation(query = {})
32
+ response = connection.get("/variations", query)
33
+
34
+ handler.new(response, single_from_list: true).parse!
35
+ end
36
+
37
+ def fetch_categories(query = {})
38
+ response = connection.get("/categories", query)
39
+
40
+ handler.new(response).parse!
41
+ end
42
+
43
+ def fetch_category(category_id:)
44
+ response = connection.get("/categories/#{category_id}")
45
+
46
+ handler.new(response).parse!
47
+ end
48
+
49
+ def fetch_orders(query = {})
50
+ response = connection.get("/orders", query)
51
+
52
+ handler.new(response).parse!
53
+ end
54
+
55
+ def fetch_order(order_id:)
56
+ response = connection.get("/orders/#{order_id}")
57
+
58
+ handler.new(response).parse!
59
+ end
60
+
61
+ def fetch_colors(query = {})
62
+ response = connection.get("/colors", query)
63
+
64
+ handler.new(response).parse!
65
+ end
66
+
67
+ def fetch_brands(query = {})
68
+ response = connection.get("/brands", query)
69
+
70
+ handler.new(response).parse!
71
+ end
72
+
73
+ def fetch_attributes(query = {})
74
+ response = connection.get("/attributes", query)
75
+
76
+ handler.new(response).parse!
77
+ end
78
+
79
+ def create_product(body)
80
+ response = connection.post("/products", body.to_json)
81
+
82
+ handler.new(response).parse!
83
+ end
84
+
85
+ def update_product(product_id:, body:)
86
+ response = connection.put("/products/#{product_id}", body.to_json)
87
+
88
+ handler.new(response).parse!
89
+ end
90
+
91
+ def create_variation(body)
92
+ response = connection.post("/variations", body.to_json)
93
+
94
+ handler.new(response).parse!
95
+ end
96
+
97
+ def update_variation(variation_id:, body:)
98
+ response = connection.put("/variations/#{variation_id}", body.to_json)
99
+
100
+ handler.new(response).parse!
101
+ end
102
+
103
+ def create_category(body)
104
+ response = connection.post("/categories", body.to_json)
105
+
106
+ handler.new(response).parse!
107
+ end
108
+
109
+ def update_category(category_id:, body:)
110
+ response = connection.put("/categories/#{category_id}", body.to_json)
111
+
112
+ handler.new(response).parse!
113
+ end
114
+
115
+ def create_brand(body)
116
+ response = connection.post("/brands", body.to_json)
117
+
118
+ handler.new(response).parse!
119
+ end
120
+
121
+ def update_brand(brand_id:, body:)
122
+ response = connection.put("/brands/#{brand_id}", body.to_json)
123
+
124
+ handler.new(response).parse!
125
+ end
126
+
127
+ def create_color(body)
128
+ response = connection.post("/colors", body.to_json)
129
+
130
+ handler.new(response).parse!
131
+ end
132
+
133
+ def update_color(color_id:, body:)
134
+ response = connection.put("/colors/#{color_id}", body.to_json)
135
+
136
+ handler.new(response).parse!
137
+ end
138
+
139
+ def create_attribute(body)
140
+ response = connection.post("/attributes", body.to_json)
141
+
142
+ handler.new(response).parse!
143
+ end
144
+
145
+ def update_attribute(attribute_id:, body:)
146
+ response = connection.put("/attributes/#{attribute_id}", body.to_json)
147
+
148
+ handler.new(response).parse!
149
+ end
150
+
151
+ def create_attribute_value(body)
152
+ response = connection.post("/attributes/values", body.to_json)
153
+
154
+ handler.new(response).parse!
155
+ end
156
+
157
+ def update_attribute_value(attribute_value_id:, body:)
158
+ response = connection.put("/attributes/values/#{attribute_value_id}", body.to_json)
159
+
160
+ handler.new(response).parse!
161
+ end
162
+
163
+ private
164
+
165
+ URL = "https://api.dooca.store"
166
+
167
+ def headers
168
+ {
169
+ "Content-Type" => "application/json",
170
+ "Authorization": "Bearer #{token}"
171
+ }
172
+ end
173
+
174
+ def connection
175
+ @connection ||= Faraday.new(url: URL, headers: headers)
4
176
  end
5
177
  end
@@ -0,0 +1,37 @@
1
+ class ResponseHandler
2
+
3
+ def initialize(response, single_from_list: false)
4
+ @response = response
5
+ @single_from_list = single_from_list
6
+ end
7
+
8
+ attr_reader :response, :single_from_list
9
+
10
+ def parse!
11
+ success? ? handle_success : handle_error
12
+ end
13
+
14
+ private
15
+
16
+ def handle_success
17
+ @payload = payload.data.first if single_from_list
18
+
19
+ payload.success = true
20
+
21
+ payload
22
+ end
23
+
24
+ def handle_error
25
+ payload.success = false
26
+
27
+ payload
28
+ end
29
+
30
+ def payload
31
+ @payload ||= JSON.parse(response.body, object_class: OpenStruct)
32
+ end
33
+
34
+ def success?
35
+ single_from_list ? payload.data&.first.present? : (200..204).include?(response.status)
36
+ end
37
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dooca_commerce
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luciano Cesar Cordeiro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-17 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2022-01-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: A Ruby implementation of Dooca Commerce API
14
28
  email: lucianocordeiro1991@hotmail.com
15
29
  executables: []
@@ -17,6 +31,7 @@ extensions: []
17
31
  extra_rdoc_files: []
18
32
  files:
19
33
  - lib/dooca_commerce.rb
34
+ - lib/response_handler.rb
20
35
  homepage: https://rubygems.org/gems/doca_commerce
21
36
  licenses:
22
37
  - MIT