fera-api 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ clearfix
@@ -0,0 +1,3 @@
1
+ concat
2
+ intl
3
+ swal
@@ -0,0 +1,22 @@
1
+ cattr
2
+ endcapture
3
+ endfor
4
+ endform
5
+ endif
6
+ evalsha
7
+ hdel
8
+ hget
9
+ hgetall
10
+ hkeys
11
+ hmset
12
+ sadd
13
+ sismember
14
+ smembers
15
+ spop
16
+ srem
17
+ zadd
18
+ zcount
19
+ zrangebyscore
20
+ zrank
21
+ zrem
22
+ zrevrange
data/Gemfile CHANGED
@@ -5,10 +5,12 @@ source "https://rubygems.org"
5
5
  # Specify your gem's dependencies in fera.gemspec
6
6
  gemspec
7
7
 
8
- gem "rake", "~> 13.0"
9
-
10
8
  group :development, :test do
11
- gem "rspec", "~> 3.0"
12
-
9
+ gem "pry"
10
+ gem "pry-byebug"
11
+ gem "pry-rails"
12
+ gem "pry-remote"
13
+ gem "pry-rescue"
14
+ gem "pry-stack_explorer"
13
15
  gem "rubocop"
14
16
  end
data/Gemfile.lock CHANGED
@@ -1,18 +1,18 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fera-api (0.1.1)
4
+ fera-api (0.1.2)
5
5
  activemodel (>= 4)
6
6
  activeresource (>= 6)
7
- activesupport (>= 5.0)
7
+ activesupport (>= 4.0)
8
8
  json-jwt (>= 1)
9
9
  require_all (>= 2)
10
10
 
11
11
  GEM
12
12
  remote: https://rubygems.org/
13
13
  specs:
14
- activemodel (6.1.6.1)
15
- activesupport (= 6.1.6.1)
14
+ activemodel (7.0.3.1)
15
+ activesupport (= 7.0.3.1)
16
16
  activemodel-serializers-xml (1.0.2)
17
17
  activemodel (> 5.x)
18
18
  activesupport (> 5.x)
@@ -21,31 +21,62 @@ GEM
21
21
  activemodel (>= 6.0)
22
22
  activemodel-serializers-xml (~> 1.0)
23
23
  activesupport (>= 6.0)
24
- activesupport (6.1.6.1)
24
+ activesupport (7.0.3.1)
25
25
  concurrent-ruby (~> 1.0, >= 1.0.2)
26
26
  i18n (>= 1.6, < 2)
27
27
  minitest (>= 5.1)
28
28
  tzinfo (~> 2.0)
29
- zeitwerk (~> 2.3)
29
+ addressable (2.8.0)
30
+ public_suffix (>= 2.0.2, < 5.0)
30
31
  aes_key_wrap (1.1.0)
31
32
  ast (2.4.2)
32
33
  bindata (2.4.10)
34
+ binding_of_caller (1.0.0)
35
+ debug_inspector (>= 0.0.1)
33
36
  builder (3.2.4)
37
+ byebug (11.1.3)
38
+ coderay (1.1.3)
34
39
  concurrent-ruby (1.1.10)
40
+ crack (0.4.5)
41
+ rexml
42
+ debug_inspector (1.1.0)
35
43
  diff-lcs (1.5.0)
44
+ hashdiff (1.0.1)
45
+ httpclient (2.8.3)
36
46
  i18n (1.12.0)
37
47
  concurrent-ruby (~> 1.0)
38
- json-jwt (1.14.0)
48
+ interception (0.5)
49
+ json (2.6.2)
50
+ json-jwt (1.15.2)
39
51
  activesupport (>= 4.2)
40
52
  aes_key_wrap
41
53
  bindata
42
- minitest (5.16.2)
54
+ httpclient
55
+ method_source (1.0.0)
56
+ minitest (5.16.3)
43
57
  parallel (1.22.1)
44
- parser (3.1.2.0)
58
+ parser (3.1.2.1)
45
59
  ast (~> 2.4.1)
60
+ pry (0.14.1)
61
+ coderay (~> 1.1)
62
+ method_source (~> 1.0)
63
+ pry-byebug (3.10.1)
64
+ byebug (~> 11.0)
65
+ pry (>= 0.13, < 0.15)
66
+ pry-rails (0.3.9)
67
+ pry (>= 0.10.4)
68
+ pry-remote (0.1.8)
69
+ pry (~> 0.9)
70
+ slop (~> 3.0)
71
+ pry-rescue (1.5.2)
72
+ interception (>= 0.5)
73
+ pry (>= 0.12.0)
74
+ pry-stack_explorer (0.6.1)
75
+ binding_of_caller (~> 1.0)
76
+ pry (~> 0.13)
77
+ public_suffix (4.0.7)
46
78
  rainbow (3.1.1)
47
- rake (13.0.6)
48
- regexp_parser (2.4.0)
79
+ regexp_parser (2.5.0)
49
80
  require_all (3.0.0)
50
81
  rexml (3.2.5)
51
82
  rspec (3.11.0)
@@ -61,32 +92,46 @@ GEM
61
92
  diff-lcs (>= 1.2.0, < 2.0)
62
93
  rspec-support (~> 3.11.0)
63
94
  rspec-support (3.11.0)
64
- rubocop (1.29.0)
95
+ rubocop (1.35.0)
96
+ json (~> 2.3)
65
97
  parallel (~> 1.10)
66
- parser (>= 3.1.0.0)
98
+ parser (>= 3.1.2.1)
67
99
  rainbow (>= 2.2.2, < 4.0)
68
100
  regexp_parser (>= 1.8, < 3.0)
69
101
  rexml (>= 3.2.5, < 4.0)
70
- rubocop-ast (>= 1.17.0, < 2.0)
102
+ rubocop-ast (>= 1.20.1, < 2.0)
71
103
  ruby-progressbar (~> 1.7)
72
104
  unicode-display_width (>= 1.4.0, < 3.0)
73
- rubocop-ast (1.17.0)
105
+ rubocop-ast (1.21.0)
74
106
  parser (>= 3.1.1.0)
75
107
  ruby-progressbar (1.11.0)
108
+ slop (3.6.0)
109
+ to_bool (2.0.0)
76
110
  tzinfo (2.0.5)
77
111
  concurrent-ruby (~> 1.0)
78
- unicode-display_width (2.1.0)
79
- zeitwerk (2.6.0)
112
+ unicode-display_width (2.2.0)
113
+ webmock (3.18.0)
114
+ addressable (>= 2.8.0)
115
+ crack (>= 0.3.2)
116
+ hashdiff (>= 0.4.0, < 2.0.0)
80
117
 
81
118
  PLATFORMS
82
119
  x86_64-darwin-18
83
120
  x86_64-darwin-20
121
+ x86_64-linux
84
122
 
85
123
  DEPENDENCIES
86
124
  fera-api!
87
- rake (~> 13.0)
88
- rspec (~> 3.0)
125
+ pry
126
+ pry-byebug
127
+ pry-rails
128
+ pry-remote
129
+ pry-rescue
130
+ pry-stack_explorer
131
+ rspec (>= 3.0)
89
132
  rubocop
133
+ to_bool
134
+ webmock (>= 3.0)
90
135
 
91
136
  BUNDLED WITH
92
137
  2.3.13
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  [![Checks](https://github.com/feracommerce/fera-api-ruby/actions/workflows/checks.yml/badge.svg)](https://github.com/feracommerce/fera-api-ruby/actions/workflows/checks.yml)
2
+ [![Tests](https://github.com/feracommerce/fera-api-ruby/actions/workflows/tests.yml/badge.svg)](https://github.com/feracommerce/fera-api-ruby/actions/workflows/tests.yml)
2
3
 
3
4
  # Fera API Ruby Client
4
5
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Fera
4
4
  module API
5
- VERSION = "0.1.1"
5
+ VERSION = "0.1.2"
6
6
  end
7
7
  end
data/lib/fera/api.rb CHANGED
@@ -18,11 +18,14 @@ module Fera
18
18
 
19
19
  ##
20
20
  # @param api_key [String] Public API key, Secret API key or Auth Token (if app)
21
- def self.configure(api_key, api_url: nil, strict_mode: false)
21
+ def self.configure(api_key, api_url: nil, strict_mode: false, debug_mode: false)
22
22
  previous_base_site = Base.site
23
23
  previous_base_headers = Base.headers
24
24
 
25
- Base.site = api_url || 'https://api.fera.ai/v3/private'
25
+ api_url ||= 'https://api.fera.ai'
26
+ Base.site = "#{ api_url.chomp('/') }/v3/private"
27
+
28
+ @debug_mode = debug_mode
26
29
 
27
30
  if api_key =~ /^sk_/
28
31
  Base.headers['Secret-Key'] = api_key
@@ -48,9 +51,10 @@ module Fera
48
51
  else
49
52
  self
50
53
  end
51
-
52
54
  end
53
55
 
56
+ def self.debug_mode?; @debug_mode; end
57
+
54
58
  def self.revoke_token!(client_id:, client_secret:, auth_token:)
55
59
  previous_site = Base.site
56
60
 
data/lib/fera/app.rb CHANGED
@@ -29,5 +29,8 @@ module Fera
29
29
  nil
30
30
  end
31
31
 
32
+ def encode_jwt(payload)
33
+ JWT.encode(payload, @client_secret)
34
+ end
32
35
  end
33
36
  end
@@ -2,11 +2,10 @@ require_relative './collection'
2
2
 
3
3
  module Fera
4
4
  class Base < ActiveResource::Base
5
- # include ActiveModel::Dirty
6
-
7
5
  attr_reader :last_response, :last_response_body, :last_response_message, :last_response_exception, :options
8
6
 
9
7
  self.collection_parser = ::Fera::Collection
8
+ self.site ||= "https://api.fera.ai/v3/private"
10
9
 
11
10
  class << self
12
11
  def belongs_to(name, options = {})
@@ -37,9 +36,7 @@ module Fera
37
36
  end
38
37
  end
39
38
 
40
- def default_params=(default_params)
41
- @default_params = default_params
42
- end
39
+ attr_writer :default_params
43
40
 
44
41
  ##
45
42
  # @override to support extra_params
@@ -80,7 +77,7 @@ module Fera
80
77
  end
81
78
 
82
79
  def new_element_path(prefix_options = {}, extra_params = {})
83
- url = "#{prefix(prefix_options)}#{collection_name}/new#{format_extension}"
80
+ url = "#{ prefix(prefix_options) }#{ collection_name }/new#{ format_extension }"
84
81
  url += "?#{ extra_params.to_param }" if extra_params.present?
85
82
  url
86
83
  end
@@ -108,12 +105,14 @@ module Fera
108
105
 
109
106
  super(dynamic_attributes, persisted)
110
107
 
108
+ return unless attributes.present?
109
+
111
110
  association_keys.each do |name, _opts|
112
111
  if attributes.key?(name.to_s) || attributes.key?(name.to_sym)
113
112
  val = attributes.to_h[name.to_s] || attributes.to_h[name.to_sym]
114
113
  self.send("#{ name }=", val) if respond_to?("#{ name }=")
115
114
  end
116
- end if attributes.present?
115
+ end
117
116
  end
118
117
 
119
118
  def load(attributes, *args)
@@ -150,7 +149,7 @@ module Fera
150
149
  end
151
150
  end
152
151
 
153
- def update(changed_attributes, extra_params = {}, raise = false)
152
+ def update(changed_attributes, extra_params = {}, raise: false)
154
153
  run_callbacks(:update) do
155
154
  connection.put(element_path(prefix_options, extra_params), changed_attributes.to_json, self.class.headers).tap do |response|
156
155
  load_attributes_from_response(response)
@@ -171,7 +170,7 @@ module Fera
171
170
  end
172
171
 
173
172
  def update!(changed_attributes, extra_params = {})
174
- update(changed_attributes, extra_params, true)
173
+ update(changed_attributes, extra_params, raise: true)
175
174
  end
176
175
 
177
176
  def valid?(_context = nil)
@@ -179,13 +178,13 @@ module Fera
179
178
  end
180
179
 
181
180
  ##
182
- # @override to add exgtra params
183
- def create(extra_params = {}, raise = false)
181
+ # @override to add extra params
182
+ def create(extra_params = {}, raise: false)
184
183
  run_callbacks :create do
185
-
186
184
  data = as_json
187
- (self.class.belongs_tos.merge(self.class.has_ones)).each do |name, _opts|
185
+ self.class.belongs_tos.merge(self.class.has_ones).each do |name, _opts|
188
186
  next unless instance_variable_defined?(:"@#{ name }")
187
+
189
188
  nested_resource = self.send(name)
190
189
  if nested_resource.present? && !nested_resource.persisted?
191
190
  nested_resource.validate!
@@ -195,6 +194,7 @@ module Fera
195
194
 
196
195
  self.class.has_manys.each do |name, _opts|
197
196
  next unless instance_variable_defined?(:"@#{ name }")
197
+
198
198
  nested_resource = self.send(name)
199
199
 
200
200
  next if nested_resource.nil?
@@ -227,13 +227,13 @@ module Fera
227
227
  end
228
228
 
229
229
  def create!(extra_params = {})
230
- create(extra_params, true)
230
+ create(extra_params, raise: true)
231
231
  end
232
232
 
233
- def save(extra_params = {}, raise = false)
233
+ def save(extra_params = {}, raise: false)
234
234
  run_callbacks :save do
235
235
  if new?
236
- create(extra_params, raise) # We'll raise the error below
236
+ create(extra_params, raise: raise) # We'll raise the error below
237
237
  else
238
238
  # find changes
239
239
  changed_attributes = attributes.filter { |key, value| !@clean_copy.attributes.key?(key) || (@clean_copy.attributes[key] != value) || (key == self.class.primary_key) }
@@ -241,23 +241,22 @@ module Fera
241
241
  return false unless changed_attributes.keys.any?
242
242
 
243
243
  # save
244
- update(changed_attributes, extra_params, raise)
244
+ update(changed_attributes, extra_params, raise: raise)
245
245
  end
246
246
 
247
247
  @clean_copy = clone_with_nil # Clear changes
248
248
 
249
249
  self
250
250
  end
251
-
252
251
  end
253
252
 
254
253
  def save!(extra_params = {})
255
- save(extra_params, true)
254
+ save(extra_params, raise: true)
256
255
  end
257
256
 
258
257
  def clone_with_nil
259
258
  # Clone all attributes except the pk and any nested ARes
260
- cloned = Hash[attributes.reject { |k, v| k == self.class.primary_key || v.is_a?(ActiveResource::Base) }.map { |k, v| [k, v.clone] }]
259
+ cloned = attributes.reject { |k, v| k == self.class.primary_key || v.is_a?(ActiveResource::Base) }.map { |k, v| [k, v.clone] }.to_h
261
260
  # Form the new resource - bypass initialize of resource with 'new' as that will call 'load' which
262
261
  # attempts to convert hashes into member objects and arrays into collections of objects. We want
263
262
  # the raw objects to be cloned so we bypass load by directly setting the attributes hash.
@@ -294,9 +293,11 @@ module Fera
294
293
  if matcher.present?
295
294
  attribute_name = matcher[1]
296
295
  return super if attribute_name.blank?
296
+
297
297
  attribute_name = "is_#{ attribute_name }" unless attribute_name =~ /^is_/
298
298
  return super unless known_attribute?(attribute_name.to_s)
299
- return !!(send(attribute_name.to_sym).presence)
299
+
300
+ return !!send(attribute_name.to_sym).presence
300
301
  end
301
302
 
302
303
  super
@@ -307,6 +308,7 @@ module Fera
307
308
  if matcher.present?
308
309
  attribute_name = matcher[1]
309
310
  return super if attribute_name.blank?
311
+
310
312
  attribute_name = "is_#{ attribute_name }" unless attribute_name =~ /^is_/
311
313
  return true if known_attribute?(attribute_name)
312
314
  end
@@ -318,7 +320,7 @@ module Fera
318
320
  known_attributes.map(&:to_s).include?(attribute_name.to_s)
319
321
  end
320
322
 
321
- def set_last_response(result)
323
+ def set_last_response(result) # rubocop:disable Naming/AccessorMethodName
322
324
  response = if result.is_a?(StandardError)
323
325
  @last_response_exception = result
324
326
  @last_response_exception.response
@@ -376,5 +378,9 @@ module Fera
376
378
  sub_fields = sub_fields.drop(1)
377
379
  changed_attributes[sub_key] = values.map { |value| clone_selected_fields(value, sub_fields) }
378
380
  end
381
+
382
+ def debug(message)
383
+ puts "[Fera-Api] #{ message }" if ::Fera::Api.debug_mode?
384
+ end
379
385
  end
380
386
  end
@@ -10,8 +10,8 @@ module Fera
10
10
 
11
11
  def customer=(customer)
12
12
  if customer.is_a?(Customer)
13
- @customer = customer
14
- self.attributes['customer_id'] = customer.id
13
+ @customer = customer
14
+ self.attributes['customer_id'] = customer.id
15
15
  self.attributes['external_customer_id'] = customer.try(:external_id)
16
16
  self.attributes.delete('customer')
17
17
  elsif customer.is_a?(Hash)
@@ -59,8 +59,6 @@ module Fera
59
59
  Customer.find(customer_id)
60
60
  elsif attributes.key?('external_customer_id') && external_customer_id.present?
61
61
  Customer.find(external_customer_id)
62
- else
63
- nil
64
62
  end
65
63
  end
66
64
  end
@@ -9,21 +9,21 @@ module Fera
9
9
  end
10
10
 
11
11
  def order=(order)
12
- order_id = if order.is_a?(Order)
13
- order.id
14
- else
15
- order.try(:with_indifferent_access).try(:[], :id)
16
- end
12
+ order_id = if order.is_a?(Order)
13
+ order.id
14
+ else
15
+ order.try(:with_indifferent_access).try(:[], :id)
16
+ end
17
17
  external_order_id = if order.is_a?(Order)
18
- order.external_id
19
- else
20
- order.try(:with_indifferent_access).try(:[], :external_id)
21
- end
22
- @order = if order.is_a?(Order)
23
- order
24
- else
25
- Order.new(order, order_id.present?)
26
- end
18
+ order.external_id
19
+ else
20
+ order.try(:with_indifferent_access).try(:[], :external_id)
21
+ end
22
+ @order = if order.is_a?(Order)
23
+ order
24
+ else
25
+ Order.new(order, order_id.present?)
26
+ end
27
27
  self.attributes['order_id'] = order_id
28
28
  self.attributes['external_order_id'] = external_order_id
29
29
  self.attributes.delete('order')
@@ -77,10 +77,7 @@ module Fera
77
77
  Order.find(order_id)
78
78
  elsif attributes.key?('external_order_id') && external_order_id.present?
79
79
  Order.find(external_order_id)
80
- else
81
- nil
82
80
  end
83
81
  end
84
-
85
82
  end
86
83
  end
@@ -9,21 +9,21 @@ module Fera
9
9
  end
10
10
 
11
11
  def product=(product)
12
- product_id = if product.is_a?(Product)
13
- product.id
14
- else
15
- product.try(:with_indifferent_access).try(:[], :id)
16
- end
12
+ product_id = if product.is_a?(Product)
13
+ product.id
14
+ else
15
+ product.try(:with_indifferent_access).try(:[], :id)
16
+ end
17
17
  external_product_id = if product.is_a?(Product)
18
- product.external_id
19
- else
20
- product.try(:with_indifferent_access).try(:[], :external_id)
21
- end
22
- @product = if product.is_a?(Product)
23
- product
24
- else
25
- Product.new(product, product_id.present?)
26
- end
18
+ product.external_id
19
+ else
20
+ product.try(:with_indifferent_access).try(:[], :external_id)
21
+ end
22
+ @product = if product.is_a?(Product)
23
+ product
24
+ else
25
+ Product.new(product, product_id.present?)
26
+ end
27
27
  self.attributes['product_id'] = product_id
28
28
  self.attributes['external_product_id'] = external_product_id
29
29
  self.attributes.delete('product')
@@ -38,10 +38,7 @@ module Fera
38
38
  Product.find(product_id)
39
39
  elsif attributes.key?('external_product_id') && external_product_id.present?
40
40
  Product.find(external_product_id)
41
- else
42
- nil
43
41
  end
44
42
  end
45
-
46
43
  end
47
44
  end
@@ -10,15 +10,15 @@ module Fera
10
10
 
11
11
  def review=(review)
12
12
  review_id = if review.is_a?(Review)
13
- review.id
14
- else
15
- review.try(:with_indifferent_access).try(:[], :id)
16
- end
17
- @review = if review.is_a?(Review)
18
- review
19
- else
20
- Review.new(review, review_id.present?)
21
- end
13
+ review.id
14
+ else
15
+ review.try(:with_indifferent_access).try(:[], :id)
16
+ end
17
+ @review = if review.is_a?(Review)
18
+ review
19
+ else
20
+ Review.new(review, review_id.present?)
21
+ end
22
22
  self.attributes['review_id'] = review_id
23
23
  self.attributes.delete('review')
24
24
  end
@@ -30,10 +30,7 @@ module Fera
30
30
  Review.new(attributes['review'], true)
31
31
  elsif attributes.key?('review_id') && review_id.present?
32
32
  Review.find(review_id)
33
- else
34
- nil
35
33
  end
36
34
  end
37
-
38
35
  end
39
36
  end
@@ -30,8 +30,6 @@ module Fera
30
30
  Submission.new(attributes['submission'], true)
31
31
  elsif attributes.key?('submission_id') && submission_id.present?
32
32
  Submission.find(submission_id)
33
- else
34
- nil
35
33
  end
36
34
  end
37
35
  end
@@ -26,11 +26,9 @@ module Fera
26
26
  # Returns the associated product model if it was preloaded in the original request and if the object is for a product.
27
27
  # @return [::Fera::Product, NilClass]
28
28
  def product
29
- if attributes['subject'] =~ /^product/i
30
- super
31
- else
32
- nil
33
- end
29
+ return nil if attributes['subject'] !~ /^product/i
30
+
31
+ super
34
32
  end
35
33
 
36
34
  def for_product?
@@ -31,25 +31,25 @@ module Fera
31
31
  end
32
32
  alias_method :photo?, :is_photo?
33
33
 
34
- def file=(f)
35
- if f.is_a?(File)
36
- file_name = File.basename(f.path)
34
+ def file=(val)
35
+ if val.is_a?(File)
36
+ file_name = File.basename(val.path)
37
37
  mime_type_group = type == 'video' ? 'video' : 'image'
38
38
  self.attributes['file'] = {
39
39
  'name' => File.basename(file_name),
40
- 'data' => "data:#{ mime_type_group }/#{ file_name.split('.').last };base64,#{ Base64.encode64(f.read) }"
40
+ 'data' => "data:#{ mime_type_group }/#{ file_name.split('.').last };base64,#{ Base64.encode64(val.read) }",
41
41
  }
42
42
  else
43
- self.attributes['file'] = f
43
+ self.attributes['file'] = val
44
44
  end
45
45
  end
46
46
 
47
47
  private
48
48
 
49
49
  def remove_file_param
50
- if self.attributes.key?('file')
51
- self.attributes.delete('file')
52
- end
50
+ return unless self.attributes.key?('file')
51
+
52
+ self.attributes.delete('file')
53
53
  end
54
54
  end
55
55
  end