whiplash-app 0.6.2 → 0.8.1

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: 3d70f904d63e4b816748c23f7a3287763420f8c222b2e7e4c63519c63614613f
4
- data.tar.gz: 3a267c71d211814b6e85bfd8fa8075d1f048a90f936187b4353a0d36bc40b4d2
3
+ metadata.gz: f264f95fe30e26b24988d6f8db597a349259a79ab9fb446402a3528569112fed
4
+ data.tar.gz: a07d6c73760ec2d98107b0510c7f7d9b3566a217afe364b2b53e9044eebe940f
5
5
  SHA512:
6
- metadata.gz: 27d80f78a200d4d202b1236941ec7b7c5b3b2e185bdcf3c51ba98ce7304010cc258c350f2cf524d684343eae2c8111f837ced586b10fda93439ed2f1e608a556
7
- data.tar.gz: 92bcf7a2e0bc197d7b97aba7d4748fe4759b9ede63cf60ec9027645b44ee4155057b2e6b63ed2177b2a6d208fb8563409cf2d7dae0bb68a04cca0e8c0eed8e8c
6
+ metadata.gz: 34d77d907ea7d03b60483b3fd7f6177e1a9c1d502cef48e27bccc77c8694849ded0a47e9f1155025602c4dcadb78af0753502e0ab46305ad7f411caa32a992ca
7
+ data.tar.gz: 356479891eb004b6beb00437452ed0b55190176618aff587fb3d5d9370c8689673a2388d1c6ae08e69f4c6472f8c0e134f910e152706aa8e04ac0c6e36143dc3
data/.travis.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.3
4
- before_install: gem install bundler -v 1.11.2
3
+ - 2.6.7
4
+ before_install: gem install bundler -v 2.2.25
@@ -2,6 +2,8 @@ module Whiplash
2
2
  class App
3
3
  module Connections
4
4
 
5
+ PER_PAGE_REQUEST_LIMIT = 50
6
+
5
7
  def base_app_request(options={})
6
8
  if options[:params][:id]
7
9
  endpoint = [options[:endpoint], options[:params].delete(:id)].join('/')
@@ -11,6 +13,7 @@ module Whiplash
11
13
  options[:headers] ||= {}
12
14
  options[:headers][:customer_id] ||= customer_id if customer_id
13
15
  options[:headers][:shop_id] ||= shop_id if shop_id
16
+ options[:headers][:version] ||= 2
14
17
 
15
18
  args = [
16
19
  options[:method],
@@ -56,6 +59,47 @@ module Whiplash
56
59
  end
57
60
  end
58
61
 
62
+ def multi_page_get!(endpoint, params = {}, headers = nil)
63
+ results = []
64
+ page = 1
65
+ params[:per_page] = PER_PAGE_REQUEST_LIMIT
66
+ response = nil
67
+
68
+ loop do
69
+ partial_results_request = app_request!(
70
+ method: :get,
71
+ endpoint: endpoint,
72
+ params: params,
73
+ headers: headers
74
+ )
75
+
76
+ if !partial_results_request.success?
77
+ response = Faraday::Response.new(
78
+ body: partial_results_request.body,
79
+ status: partial_results_request.status,
80
+ method: :get
81
+ )
82
+ break
83
+ end
84
+
85
+ results << partial_results_request.body
86
+ results.flatten!
87
+
88
+ page += 1
89
+ params[:page] = page
90
+
91
+ break if partial_results_request.body.size == 0
92
+ break if partial_results_request.body.size < PER_PAGE_REQUEST_LIMIT
93
+ end
94
+
95
+ response ||= Faraday::Response.new(
96
+ body: results,
97
+ status: 200,
98
+ method: :get
99
+ )
100
+
101
+ end
102
+
59
103
  def delete(endpoint, params = {}, headers = nil)
60
104
  app_request(method: :delete,
61
105
  endpoint: endpoint,
@@ -138,15 +182,18 @@ module Whiplash
138
182
 
139
183
  def get_context(endpoint)
140
184
  parts = endpoint.split('/').compact
141
- return 'member' if (parts.last =~ /\d+/).present?
185
+ return 'member' unless (parts.last =~ /\d+/).nil?
142
186
  return 'aggregate' if parts.include?('aggregate')
143
187
  'collection'
144
188
  end
145
189
 
146
190
  def sanitize_headers(headers)
147
- if headers
148
- {}.tap do |hash|
149
- headers.each do |k,v|
191
+ return if headers.nil? || headers.empty?
192
+ out = {}.tap do |hash|
193
+ headers.each do |k,v|
194
+ if k.to_s == 'version'
195
+ hash['Accept-Version'] = "v#{v}"
196
+ else
150
197
  hash["X-#{k.to_s.upcase.gsub('_','-')}"] = v.to_s
151
198
  end
152
199
  end
@@ -155,7 +202,7 @@ module Whiplash
155
202
 
156
203
  def store_whiplash_error!(error, options={})
157
204
  return unless defined?(Appsignal)
158
- options = options.with_indifferent_access
205
+ options.transform_keys!(&:to_sym)
159
206
  Appsignal.increment_counter(
160
207
  "whiplash_error",
161
208
  1.0,
@@ -15,7 +15,11 @@ module Whiplash
15
15
  private
16
16
 
17
17
  def request_body(body)
18
- body.blank? ? ENV["WHIPLASH_CLIENT_ID"] : body
18
+ begin
19
+ (body.nil? || body.empty?) ? ENV["WHIPLASH_CLIENT_ID"] : body
20
+ rescue NoMethodError => e
21
+ ENV["WHIPLASH_CLIENT_ID"]
22
+ end
19
23
  end
20
24
  end
21
25
  end
@@ -1,5 +1,5 @@
1
1
  module Whiplash
2
2
  class App
3
- VERSION = "0.6.2"
3
+ VERSION = "0.8.1"
4
4
  end
5
5
  end
data/lib/whiplash/app.rb CHANGED
@@ -19,11 +19,11 @@ module Whiplash
19
19
  attr_accessor :customer_id, :shop_id, :token
20
20
 
21
21
  def initialize(token=nil, options={})
22
- opts = options.with_indifferent_access
23
22
  token ||= cache_store["whiplash_api_token"]
24
23
  @token = format_token(token) unless token.nil?
25
24
  @customer_id = options[:customer_id]
26
25
  @shop_id = options[:shop_id]
26
+ @api_version = options[:api_version] || 2 # can be 2_1
27
27
  end
28
28
 
29
29
  def self.whiplash_api_token
@@ -36,8 +36,12 @@ module Whiplash
36
36
  OAuth2::Client.new(ENV["WHIPLASH_CLIENT_ID"], ENV["WHIPLASH_CLIENT_SECRET"], site: api_url)
37
37
  end
38
38
 
39
+ def versioned_api_url
40
+ "api/v#{@api_version}"
41
+ end
42
+
39
43
  def connection
40
- out = Faraday.new [api_url, "api/v2"].join("/") do |conn|
44
+ out = Faraday.new [api_url, versioned_api_url].join("/") do |conn|
41
45
  conn.request :oauth2, token.token, token_type: "bearer"
42
46
  conn.request :json
43
47
  conn.response :json, :content_type => /\bjson$/
@@ -52,34 +56,41 @@ module Whiplash
52
56
  end
53
57
 
54
58
  def refresh_token!
55
- if token.blank? # If we're storing locally, grab a new token and cache it
56
- access_token = client.client_credentials.get_token(scope: ENV["WHIPLASH_CLIENT_SCOPE"])
57
- new_token = access_token.to_hash
58
- cache_store["whiplash_api_token"] = new_token
59
+ case ENV["WHIPLASH_CLIENT_SCOPE"]
60
+ when /app_(manage|read)/
61
+ begin
62
+ access_token = client.client_credentials.get_token(scope: ENV["WHIPLASH_CLIENT_SCOPE"])
63
+ new_token = access_token.to_hash
64
+ cache_store["whiplash_api_token"] = new_token
65
+ rescue URI::InvalidURIError => e
66
+ raise StandardError, "The provide URL (#{ENV["WHIPLASH_API_URL"]}) is not valid"
67
+ end
59
68
  else
69
+ raise StandardError, "You must request an access token before you can refresh it" if token.nil?
70
+ raise StandardError, "Token must either be a Hash or an OAuth2::AccessToken" unless token.is_a?(OAuth2::AccessToken)
60
71
  access_token = token.refresh!
61
72
  end
62
73
  self.token = access_token
63
74
  end
64
75
 
65
76
  def token_expired?
66
- return token.expired? unless token.blank?
77
+ return token.expired? unless token.nil?
67
78
  return true unless cache_store.key?("whiplash_api_token")
68
- return true if cache_store["whiplash_api_token"].blank?
79
+ return true if cache_store["whiplash_api_token"].nil?
80
+ return true if cache_store["whiplash_api_token"].empty?
69
81
  false
70
82
  end
71
83
 
72
84
  private
73
85
  def format_token(oauth_token)
74
- unless oauth_token.is_a?(OAuth2::AccessToken)
75
- raise StandardError, "Token must either be a Hash or an OAuth2::AccessToken" unless oauth_token.is_a?(Hash)
76
- oauth_token['expires'] = oauth_token['expires'].to_s # from_hash expects 'true'
77
- if oauth_token.has_key?('token')
78
- oauth_token['access_token'] = oauth_token['token']
79
- oauth_token.delete('token')
80
- end
81
- oauth_token = OAuth2::AccessToken.from_hash(client, oauth_token)
86
+ return oauth_token if oauth_token.is_a?(OAuth2::AccessToken)
87
+ raise StandardError, "Token must either be a Hash or an OAuth2::AccessToken" unless oauth_token.is_a?(Hash)
88
+ oauth_token['expires'] = oauth_token['expires'].to_s # from_hash expects 'true'
89
+ if oauth_token.has_key?('token')
90
+ oauth_token['access_token'] = oauth_token['token']
91
+ oauth_token.delete('token')
82
92
  end
93
+ oauth_token = OAuth2::AccessToken.from_hash(client, oauth_token)
83
94
  end
84
95
 
85
96
  end
data/whiplash-app.gemspec CHANGED
@@ -18,11 +18,11 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "oauth2", "~> 1.2.0"
22
- spec.add_dependency "faraday_middleware", "~> 0.11.0"
21
+ spec.add_dependency "oauth2", "~> 2.0.4"
22
+ spec.add_dependency "faraday_middleware", "~> 1.2.0"
23
23
  spec.add_dependency "moneta", "~> 0.8.0"
24
24
 
25
- spec.add_development_dependency "bundler", "~> 1.11"
25
+ spec.add_development_dependency "bundler", ">= 2.2"
26
26
  spec.add_development_dependency "rake", ">= 12.3.3"
27
27
  spec.add_development_dependency "rspec", "~> 3.0"
28
28
  spec.add_development_dependency "pry" , '~> 0.12.2'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whiplash-app
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Don Sullivan, Mark Dickson
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-13 00:00:00.000000000 Z
11
+ date: 2022-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oauth2
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 1.2.0
19
+ version: 2.0.4
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 1.2.0
26
+ version: 2.0.4
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: faraday_middleware
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.11.0
33
+ version: 1.2.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.11.0
40
+ version: 1.2.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: moneta
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -56,16 +56,16 @@ dependencies:
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '1.11'
61
+ version: '2.2'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '1.11'
68
+ version: '2.2'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -138,7 +138,7 @@ files:
138
138
  homepage: https://github.com/whiplashmerch/whiplash-app
139
139
  licenses: []
140
140
  metadata: {}
141
- post_install_message:
141
+ post_install_message:
142
142
  rdoc_options: []
143
143
  require_paths:
144
144
  - lib
@@ -153,9 +153,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
153
  - !ruby/object:Gem::Version
154
154
  version: '0'
155
155
  requirements: []
156
- rubyforge_project:
157
- rubygems_version: 2.7.6.2
158
- signing_key:
156
+ rubygems_version: 3.0.9
157
+ signing_key:
159
158
  specification_version: 4
160
159
  summary: this gem provides connectivity to the Whiplash API for authentication and
161
160
  REST requests.