fera-api 0.1.2 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 340f111ef9ae910374f8b8556b80a4c7cea4a982be22c807e1330b52f5a222a8
4
- data.tar.gz: 758fe26ac14a9a4012039d14800e657690d821862eda47ea8e0866ea714a16d1
3
+ metadata.gz: 350de7865e484558dd53c2c4e4635b679baf32d9fbf11619e19995e2add14eae
4
+ data.tar.gz: c758c1e080ef27a9ece0f88edb515f9ece137b1158986a216f98444ea38f3e7c
5
5
  SHA512:
6
- metadata.gz: db7db0bd5a14e64dce22219b385d21db5675ddc5dc07196756c2a88dc20cb628786a48e911b5ad638b741c2124be96e7008dff341366d4a7ab83e827b767d5b5
7
- data.tar.gz: 53ea2d445404901fc1377edd5720f186e3cccad97eafd420f4e13959fedb55d8778ca39847e3eb2622226b06b9261e5fbc38417f3d23a523acfe083701f84303
6
+ metadata.gz: b833c34953d53e50618cdbaebfd217b6532dd6adbe4f0947ac26a00e95ddf8483ea09f6abb557b91f06c014411f264cc7fecb8d4c9d64cfd757c3161f1291d0b
7
+ data.tar.gz: 8c00efeddbdf2280b524f0ae536e5d4c76814dda70f7b0bda5d47abe62029e4ad2bd3b3dd2ae4873f04f9d8a692fb1509e36fcae3b9ab4ece486eeb31d740bcd
data/.rubocop.yml CHANGED
@@ -1,173 +1,10 @@
1
- # Base was copied from https://github.com/sider/meowcop/blob/master/config/rubocop.yml which is what Sider uses by default
1
+ inherit_from:
2
+ - 'https://raw.githubusercontent.com/feracommerce/code-standards/master/.rubocop.yml'
3
+ - 'https://raw.githubusercontent.com/feracommerce/code-standards/master/.rubocop-rspec.yml'
4
+ inherit_mode:
5
+ merge:
6
+ - Exclude
2
7
  AllCops:
3
8
  TargetRubyVersion: 2.3
4
- Exclude:
5
- - 'vendor/**/*'
6
- - 'db/schema.rb'
7
- - 'node_modules/**/*'
8
- - 'tmp/**/*'
9
- - 'lib/tasks/one_offs/**/*' # We don't really care about linting one-off tasks
10
- - 'app/migrators/**/*' # Migrators are one-off tasks so there isn't much need to lint them
11
- DisplayCopNames: true
12
- DisplayStyleGuide: true
13
- NewCops: enable
14
-
15
- Lint/NonLocalExitFromIterator:
16
- Enabled: false # We actually believe that it is more readable with premature returns
17
- Layout/BeginEndAlignment:
18
- Enabled: false # We believe it's more readable to have begin blocks aligned to the end blocks (and also helps with code smells)
19
- Lint/AmbiguousRegexpLiteral:
20
- Enabled: false
21
- Lint/AssignmentInCondition:
22
- Enabled: false
23
- Metrics/AbcSize:
24
- Max: 60
25
- # Use the default setting
26
- Metrics/BlockLength:
27
- Max: 200
28
- Exclude:
29
- # RuboCop's default
30
- - 'Rakefile'
31
- - '**/*.rake'
32
- # In many cases, config/routes.rb has very long block
33
- - 'config/routes.rb'
34
- Metrics/ClassLength:
35
- Enabled: false
36
- Metrics/CyclomaticComplexity:
37
- Max: 60
38
- Metrics/MethodLength:
39
- Enabled: false
40
- Metrics/ModuleLength:
41
- Enabled: false
42
- Metrics/ParameterLists:
43
- Max: 5
44
- Metrics/PerceivedComplexity:
45
- Enabled: false
46
- Layout/ArgumentAlignment:
47
- Enabled: false
48
- Layout/HashAlignment:
49
- Enabled: false
50
- Layout/ParameterAlignment:
51
- Enabled: false
52
- Layout/IndentationWidth:
53
- Enabled: false
54
- Layout/FirstHashElementIndentation:
55
- Enabled: false
56
- Layout/HeredocIndentation:
57
- Enabled: false
58
- Layout/LineLength:
59
- Max: 200
60
- AllowedPatterns: ['^\s*#', '^\s*([a-zA-Z]*[lL]og[a-zA-Z]*)[ (]', '^\s*[''"]', '.*#.{30,300}', '^\s*[a-z_]:\s*".+",?[}\s]*$']
61
- Layout/MultilineArrayBraceLayout:
62
- Enabled: false
63
- Layout/MultilineAssignmentLayout:
64
- Enabled: false
65
- Layout/MultilineBlockLayout:
66
- Enabled: false
67
- Layout/MultilineHashBraceLayout:
68
- Enabled: false
69
- Layout/MultilineMethodCallBraceLayout:
70
- Enabled: false
71
- Layout/MultilineMethodCallIndentation:
72
- Enabled: false
73
- Layout/MultilineMethodDefinitionBraceLayout:
74
- Enabled: false
75
- Layout/MultilineOperationIndentation:
76
- Enabled: false
77
- Layout/RescueEnsureAlignment:
78
- Enabled: false
79
- Layout/SpaceAfterNot:
80
- Enabled: false
81
- Layout/SpaceBeforeBlockBraces:
82
- Enabled: false
83
- Layout/SpaceInLambdaLiteral:
84
- Enabled: false
85
- Layout/SpaceInsideStringInterpolation:
86
- EnforcedStyle: space
87
- Naming/PredicateName:
88
- Enabled: false # As of February 2022 we now want some methods to have is_ prefix so it is congruent with JavaScript
89
- Naming/VariableNumber:
90
- Enabled: false
91
- Style/AsciiComments:
92
- Enabled: false # It's 2022, emojis are everywhere
93
- Style/Alias:
94
- EnforcedStyle: prefer_alias_method
95
- Style/CaseLikeIf:
96
- Enabled: false
97
- Style/CommentAnnotation:
98
- Enabled: false # Don't really care about being picky on comments like this. Not worth slowing down the developer.
99
- Style/CommentedKeyword:
100
- Enabled: false # We want to allow comments on single-line method definitions like `# @alias`
101
- Style/ConstantVisibility:
102
- Enabled: false
103
- Style/Copyright:
104
- Enabled: false
105
- Style/DisableCopsWithinSourceCodeDirective:
106
- Enabled: false # Allow disabling cops and we will use PRs to determine whether it should be enforced
107
- Style/Documentation:
108
- Enabled: false # We will decide in PR whether public method should be documented or not.
109
- Style/DocumentationMethod:
110
- Enabled: false # We will decide in PR whether public method should be documented or not.
111
- Style/EmptyElse:
112
- AllowComments: true # When a comment is there then it means the programmer thought about the case so we can ignore it.
113
- EnforcedStyle: empty # When a comment or nil is there it means the programmer thought about the case, so there is no need for concern
114
- Style/FloatDivision:
115
- Enabled: false # Sometimes we're trying to force the number to become a float and it could be nil
116
- Style/FrozenStringLiteralComment:
117
- Enabled: false
118
- Style/GlobalVars:
119
- Enabled: false # If we're using global variables, we have a darn good reason or it should be blocked in the PR
120
- Style/IfUnlessModifier:
121
- Enabled: false # This isn't always easier to read. Sometimes you want to put the IF in front of the block of code to make it more clear.
122
- Style/ImplicitRuntimeError:
123
- Enabled: false # These are actually readable as long as we all know that they represent a RuntimeError
124
- Style/InlineComment:
125
- Enabled: false # Trailing inline comments are pretty compact and more readable
126
- Style/Lambda:
127
- Enabled: false
128
- Style/LambdaCall:
129
- Enabled: false
130
- Style/MethodCallWithArgsParentheses:
131
- Enabled: false
132
- Style/MethodCallWithoutArgsParentheses:
133
- Enabled: true # We use whatever is more readable
134
- Style/MethodCalledOnDoEndBlock:
135
- Enabled: false # Actually don't mind these - they look more readable
136
- Style/MissingElse:
137
- Enabled: false # Sometimes it's obvious we don't care about the else case (e.g. if we're prematurely returning from method)
138
- Style/MultilineBlockChain:
139
- Enabled: false # Sometimes these are more readable
140
- Style/MutableConstant:
141
- Enabled: false
142
- Style/MapToHash:
143
- Enabled: false # We believe it's more readable to use map vs hash with block syntax
144
- Style/NumericPredicate:
145
- Enabled: false # .zero? and .positive? are a bit less readable than == 0 IMO
146
- Style/OptionHash:
147
- Enabled: false # It is more intuitive to pass option hashes to other objects in many times
148
- Style/OpenStructUse:
149
- Enabled: false
150
- Style/RedundantRegexpCharacterClass:
151
- Enabled: false # No need to make regexp less readable than they already are by reducing redundancy
152
- Style/RedundantRegexpEscape:
153
- Enabled: false # No need to make regexp less readable than they already are by reducing redundancy
154
- Style/RedundantSelf:
155
- Enabled: false # Sometimes it's actually more readable to explicitly reference self
156
- Style/RegexpLiteral:
157
- Enabled: false
158
- Style/ReturnNil:
159
- Enabled: false # Returning nil is more clear what is happening and can prevent issues with returning mixed types
160
- Style/Send:
161
- Enabled: false
162
- Style/SingleLineMethods:
163
- Enabled: false # Sometimes these are more compact and easier to read. No need to spam our file with simple method definitions.
164
- Style/StringLiterals:
165
- Enabled: false
166
- Style/SymbolArray:
167
- Enabled: false
168
- Style/TernaryParentheses:
169
- Enabled: false
170
- Style/TrailingCommaInArrayLiteral:
171
- EnforcedStyleForMultiline: consistent_comma # It makes managing arrays much easier when we have commas at the end
172
- Style/TrailingCommaInHashLiteral:
173
- EnforcedStyleForMultiline: consistent_comma # It makes managing hashes much easier when we have commas at the end
9
+ require:
10
+ - rubocop-rspec
data/Gemfile CHANGED
@@ -6,11 +6,10 @@ source "https://rubygems.org"
6
6
  gemspec
7
7
 
8
8
  group :development, :test do
9
- gem "pry"
10
- gem "pry-byebug"
11
9
  gem "pry-rails"
12
10
  gem "pry-remote"
13
11
  gem "pry-rescue"
14
- gem "pry-stack_explorer"
15
- gem "rubocop"
12
+ gem "rubocop", "~> 1.35"
13
+ gem "rubocop-rspec", "~> 2.12"
14
+ gem "spellr", "~> 0.11"
16
15
  end
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fera-api (0.1.2)
4
+ fera-api (0.2.2)
5
5
  activemodel (>= 4)
6
6
  activeresource (>= 6)
7
7
  activesupport (>= 4.0)
@@ -11,8 +11,8 @@ PATH
11
11
  GEM
12
12
  remote: https://rubygems.org/
13
13
  specs:
14
- activemodel (7.0.3.1)
15
- activesupport (= 7.0.3.1)
14
+ activemodel (7.0.4)
15
+ activesupport (= 7.0.4)
16
16
  activemodel-serializers-xml (1.0.2)
17
17
  activemodel (> 5.x)
18
18
  activesupport (> 5.x)
@@ -21,7 +21,7 @@ GEM
21
21
  activemodel (>= 6.0)
22
22
  activemodel-serializers-xml (~> 1.0)
23
23
  activesupport (>= 6.0)
24
- activesupport (7.0.3.1)
24
+ activesupport (7.0.4)
25
25
  concurrent-ruby (~> 1.0, >= 1.0.2)
26
26
  i18n (>= 1.6, < 2)
27
27
  minitest (>= 5.1)
@@ -30,7 +30,7 @@ GEM
30
30
  public_suffix (>= 2.0.2, < 5.0)
31
31
  aes_key_wrap (1.1.0)
32
32
  ast (2.4.2)
33
- bindata (2.4.10)
33
+ bindata (2.4.14)
34
34
  binding_of_caller (1.0.0)
35
35
  debug_inspector (>= 0.0.1)
36
36
  builder (3.2.4)
@@ -41,17 +41,25 @@ GEM
41
41
  rexml
42
42
  debug_inspector (1.1.0)
43
43
  diff-lcs (1.5.0)
44
+ faraday (2.5.2)
45
+ faraday-net_http (>= 2.0, < 3.1)
46
+ ruby2_keywords (>= 0.0.4)
47
+ faraday-follow_redirects (0.3.0)
48
+ faraday (>= 1, < 3)
49
+ faraday-net_http (3.0.0)
50
+ fast_ignore (0.17.4)
44
51
  hashdiff (1.0.1)
45
- httpclient (2.8.3)
46
52
  i18n (1.12.0)
47
53
  concurrent-ruby (~> 1.0)
48
54
  interception (0.5)
55
+ jaro_winkler (1.5.4)
49
56
  json (2.6.2)
50
- json-jwt (1.15.2)
57
+ json-jwt (1.16.1)
51
58
  activesupport (>= 4.2)
52
59
  aes_key_wrap
53
60
  bindata
54
- httpclient
61
+ faraday (~> 2.0)
62
+ faraday-follow_redirects
55
63
  method_source (1.0.0)
56
64
  minitest (5.16.3)
57
65
  parallel (1.22.1)
@@ -92,7 +100,7 @@ GEM
92
100
  diff-lcs (>= 1.2.0, < 2.0)
93
101
  rspec-support (~> 3.11.0)
94
102
  rspec-support (3.11.0)
95
- rubocop (1.35.0)
103
+ rubocop (1.35.1)
96
104
  json (~> 2.3)
97
105
  parallel (~> 1.10)
98
106
  parser (>= 3.1.2.1)
@@ -104,20 +112,29 @@ GEM
104
112
  unicode-display_width (>= 1.4.0, < 3.0)
105
113
  rubocop-ast (1.21.0)
106
114
  parser (>= 3.1.1.0)
115
+ rubocop-rspec (2.13.1)
116
+ rubocop (~> 1.33)
107
117
  ruby-progressbar (1.11.0)
118
+ ruby2_keywords (0.0.5)
108
119
  slop (3.6.0)
120
+ spellr (0.11.0)
121
+ fast_ignore (>= 0.11.0)
122
+ jaro_winkler
123
+ parallel (~> 1.0)
109
124
  to_bool (2.0.0)
110
125
  tzinfo (2.0.5)
111
126
  concurrent-ruby (~> 1.0)
112
- unicode-display_width (2.2.0)
127
+ unicode-display_width (2.3.0)
113
128
  webmock (3.18.0)
114
129
  addressable (>= 2.8.0)
115
130
  crack (>= 0.3.2)
116
131
  hashdiff (>= 0.4.0, < 2.0.0)
117
132
 
118
133
  PLATFORMS
134
+ ruby
119
135
  x86_64-darwin-18
120
136
  x86_64-darwin-20
137
+ x86_64-darwin-21
121
138
  x86_64-linux
122
139
 
123
140
  DEPENDENCIES
@@ -129,9 +146,11 @@ DEPENDENCIES
129
146
  pry-rescue
130
147
  pry-stack_explorer
131
148
  rspec (>= 3.0)
132
- rubocop
149
+ rubocop (~> 1.35)
150
+ rubocop-rspec (~> 2.12)
151
+ spellr (~> 0.11)
133
152
  to_bool
134
153
  webmock (>= 3.0)
135
154
 
136
155
  BUNDLED WITH
137
- 2.3.13
156
+ 2.3.15
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Fera
4
4
  module API
5
- VERSION = "0.1.2"
5
+ VERSION = "0.2.2"
6
6
  end
7
7
  end
data/lib/fera/api.rb CHANGED
@@ -18,23 +18,19 @@ 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, debug_mode: false)
22
- previous_base_site = Base.site
23
- previous_base_headers = Base.headers
21
+ # @return [Object, ::Fera::API] Result of the block operation if given, otherwise self
22
+ def self.configure(api_key, api_url: nil, strict_mode: false, debug_mode: false, api_type: nil)
23
+ previous_base_site = Base.site.dup
24
+ previous_base_headers = Base.headers.dup
25
+ previous_debug_mode = @debug_mode
24
26
 
25
27
  api_url ||= 'https://api.fera.ai'
26
- Base.site = "#{ api_url.chomp('/') }/v3/private"
28
+ api_type ||= api_key.include?('sk_') ? 'private' : 'public'
29
+ Base.site = "#{ api_url.chomp('/') }/v3/#{ api_type }"
27
30
 
28
31
  @debug_mode = debug_mode
29
32
 
30
- if api_key =~ /^sk_/
31
- Base.headers['Secret-Key'] = api_key
32
- elsif api_key =~ /^pk_/
33
- Base.headers['Public-Key'] = api_key
34
- else
35
- Base.headers['Authorization'] = "Bearer #{ api_key }"
36
- end
37
-
33
+ Base.api_key = api_key
38
34
  Base.headers['Strict-Mode'] = strict_mode if strict_mode
39
35
 
40
36
  if block_given?
@@ -42,9 +38,8 @@ module Fera
42
38
  result = yield
43
39
  ensure
44
40
  Base.site = previous_base_site
45
- previous_base_headers.each do |key, value|
46
- Base.headers[key] = value
47
- end
41
+ Base.headers = previous_base_headers
42
+ @debug_mode = previous_debug_mode
48
43
  end
49
44
 
50
45
  result
@@ -55,6 +50,10 @@ module Fera
55
50
 
56
51
  def self.debug_mode?; @debug_mode; end
57
52
 
53
+ ##
54
+ # @option client_id [String] Fera app Client ID
55
+ # @option client_secret [String] Fera app Client secret
56
+ # @option auth_token [String] Auth token you wish to revoke access for.
58
57
  def self.revoke_token!(client_id:, client_secret:, auth_token:)
59
58
  previous_site = Base.site
60
59
 
@@ -8,6 +8,52 @@ module Fera
8
8
  self.site ||= "https://api.fera.ai/v3/private"
9
9
 
10
10
  class << self
11
+ ##
12
+ # Sets the header API key for subsequent requests
13
+ # @param api_key [String] Secret Key, Public Key or OAuth Access Token.
14
+ def api_key=(api_key)
15
+ if api_key.blank?
16
+ self.headers.delete('Secret-Key')
17
+ self.headers.delete('Public-Key')
18
+ self.headers.delete('Authorization')
19
+ elsif api_key =~ /^sk_/
20
+ self.headers['Secret-Key'] = api_key
21
+ elsif api_key =~ /^pk_/
22
+ self.headers['Public-Key'] = api_key
23
+ else
24
+ self.headers['Authorization'] = "Bearer #{ api_key }"
25
+ end
26
+ end
27
+
28
+ ##
29
+ # Sets all the headers for subsequent requests
30
+ # @param new_headers [Hash] Hash of new headers to set
31
+ def headers=(new_headers)
32
+ new_headers.to_h.each do |key, value|
33
+ self.headers[key] = value
34
+ end
35
+
36
+ self.headers.to_h.each do |key, _|
37
+ self.headers.delete(key) unless new_headers.key?(key)
38
+ end
39
+ end
40
+
41
+ def api_key
42
+ self.headers['Secret-Key'] || self.headers['Public-Key'] || self.headers['Authorization'].to_s.split.last.presence
43
+ end
44
+
45
+ ##
46
+ # Returns sorted results.
47
+ # @note This only works from the root, not with scoped results (`Fera::Review.order(created_at: :asc)`)
48
+ # @param sorts [Hash, String, Symbol]
49
+ def order(sorts)
50
+ sorts = { sorts.to_s => :desc.to_s } if sorts.is_a?(String) || sorts.is_a?(Symbol)
51
+ sort_by = sorts.map do |column, direction|
52
+ "#{ column }:#{ direction.presence || :desc }"
53
+ end.join(",")
54
+ where(sort_by: sort_by)
55
+ end
56
+
11
57
  def belongs_to(name, options = {})
12
58
  @belongs_tos = @belongs_tos.to_h.merge(name => options)
13
59
  end
@@ -209,7 +255,7 @@ module Fera
209
255
  end
210
256
  end
211
257
 
212
- connection.post(collection_path(nil, extra_params), data.to_json, self.class.headers).tap do |response|
258
+ connection.post(collection_path(nil, extra_params), { data: data }.to_json, self.class.headers).tap do |response|
213
259
  self.id = id_from_response(response)
214
260
  load_attributes_from_response(response)
215
261
  end
@@ -285,7 +331,7 @@ module Fera
285
331
  end
286
332
 
287
333
  #
288
- # Method missing adapters to define public_*_id
334
+ # Method missing adapters to define is_* methods for boolean attributes
289
335
  #
290
336
 
291
337
  def method_missing(method_name, *args, &block)
@@ -0,0 +1,52 @@
1
+ require 'active_support/concern'
2
+
3
+ module Fera
4
+ module HasTimestampAction
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ # define API actions that set a timestamp on the model
9
+ # @example
10
+ # class Order < Fera::Base
11
+ # include HasTimestampAction
12
+ #
13
+ # timestamp_action deliver: :delivered
14
+ # end
15
+ #
16
+ # routes to `PUT /orders/1/deliver`
17
+ #
18
+ # order = Order.find(1)
19
+ # order.delivered? # => false
20
+ # order.deliver!(Time.now)
21
+ # order.delivered? # => true
22
+ # order.delivered_at # => 2018-01-01 00:00:00 UTC
23
+ #
24
+ # or
25
+ #
26
+ # Fera::Order.deliver!(1, Time.now.utc)
27
+ #
28
+ # @param [Array<Symbol>] actions
29
+ def timestamp_action(args)
30
+ args.each do |action, state|
31
+ define_method("#{ action }!") do |at = nil|
32
+ changed_attributes = { "#{ state }_at": at.presence || Time.now.utc }
33
+
34
+ put(action, changed_attributes)
35
+
36
+ load(changed_attributes)
37
+
38
+ true
39
+ end
40
+
41
+ define_method("#{ state }?") do
42
+ send("#{ state }_at?")
43
+ end
44
+
45
+ define_singleton_method("#{ action }!") do |id, at = nil|
46
+ find(id).send("#{ action }!", at)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -1,5 +1,8 @@
1
1
  module Fera
2
2
  class Order < Base
3
3
  include BelongsToCustomer
4
+ include HasTimestampAction
5
+
6
+ timestamp_action pay: :paid, deliver: :delivered, fulfill: :fulfilled
4
7
  end
5
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fera-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fera Commerce Inc
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-17 00:00:00.000000000 Z
11
+ date: 2022-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -201,6 +201,7 @@ files:
201
201
  - lib/fera/models/concerns/has_many_submissions.rb
202
202
  - lib/fera/models/concerns/has_media.rb
203
203
  - lib/fera/models/concerns/has_subject.rb
204
+ - lib/fera/models/concerns/has_timestamp_action.rb
204
205
  - lib/fera/models/customer.rb
205
206
  - lib/fera/models/media.rb
206
207
  - lib/fera/models/order.rb