whiplash-app 0.6.2 → 0.7.0

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: 216d093d7f7c804c1f6b7563a429e7d0ed70cf31e90eb56266e03c086107863a
4
+ data.tar.gz: bd62a9c2da9f0a603c13c7c40e99a0bfd1e6b48b958cd20f9c74f02613d3dd70
5
5
  SHA512:
6
- metadata.gz: 27d80f78a200d4d202b1236941ec7b7c5b3b2e185bdcf3c51ba98ce7304010cc258c350f2cf524d684343eae2c8111f837ced586b10fda93439ed2f1e608a556
7
- data.tar.gz: 92bcf7a2e0bc197d7b97aba7d4748fe4759b9ede63cf60ec9027645b44ee4155057b2e6b63ed2177b2a6d208fb8563409cf2d7dae0bb68a04cca0e8c0eed8e8c
6
+ metadata.gz: f3ee8b0c5c9817ddbc3231889abe2d4edd95873234674c8a2da66e418cc262810a5424813f8144439a43fdc40765bc6e0fab10dfcb540cf9d6e2a1896393ae58
7
+ data.tar.gz: a2281f059a7d4c227d7d6a89cedeb75e082dcdaa2fb2799e9a3f36f205881f07813530ac120718035d702476c176a11e1f58a0a084e736790ede55f7991ef00c
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,32 @@ 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
+
67
+ loop do
68
+ partial_results_request = app_request!(
69
+ method: :get,
70
+ endpoint: endpoint,
71
+ params: params,
72
+ headers: headers
73
+ ).body
74
+
75
+ results << partial_results_request
76
+ results.flatten!
77
+
78
+ page += 1
79
+ params[:page] = page
80
+
81
+ break if partial_results_request.size == 0
82
+ break if partial_results_request.size < PER_PAGE_REQUEST_LIMIT
83
+ end
84
+
85
+ results
86
+ end
87
+
59
88
  def delete(endpoint, params = {}, headers = nil)
60
89
  app_request(method: :delete,
61
90
  endpoint: endpoint,
@@ -138,15 +167,18 @@ module Whiplash
138
167
 
139
168
  def get_context(endpoint)
140
169
  parts = endpoint.split('/').compact
141
- return 'member' if (parts.last =~ /\d+/).present?
170
+ return 'member' unless (parts.last =~ /\d+/).nil?
142
171
  return 'aggregate' if parts.include?('aggregate')
143
172
  'collection'
144
173
  end
145
174
 
146
175
  def sanitize_headers(headers)
147
- if headers
148
- {}.tap do |hash|
149
- headers.each do |k,v|
176
+ return if headers.nil? || headers.empty?
177
+ out = {}.tap do |hash|
178
+ headers.each do |k,v|
179
+ if k.to_s == 'version'
180
+ hash['Accept-Version'] = "v#{v}"
181
+ else
150
182
  hash["X-#{k.to_s.upcase.gsub('_','-')}"] = v.to_s
151
183
  end
152
184
  end
@@ -155,7 +187,7 @@ module Whiplash
155
187
 
156
188
  def store_whiplash_error!(error, options={})
157
189
  return unless defined?(Appsignal)
158
- options = options.with_indifferent_access
190
+ options.transform_keys!(&:to_sym)
159
191
  Appsignal.increment_counter(
160
192
  "whiplash_error",
161
193
  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.7.0"
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
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.add_dependency "faraday_middleware", "~> 0.11.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.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Don Sullivan, Mark Dickson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-13 00:00:00.000000000 Z
11
+ date: 2021-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oauth2
@@ -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
@@ -153,8 +153,7 @@ 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
156
+ rubygems_version: 3.0.3.1
158
157
  signing_key:
159
158
  specification_version: 4
160
159
  summary: this gem provides connectivity to the Whiplash API for authentication and