help_scout-sdk 1.0.0 → 1.0.1

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: db597946131f89598f48083d3352273b5adde0f23dfe608a9f42f4a0d836be83
4
- data.tar.gz: d7e837bf6257fda9b10f21e74948068df19f50033885e70b30120004e98b6717
3
+ metadata.gz: fde4d41133056de55106a9908c925633544fe8a0f888794ed88dd3eff5502985
4
+ data.tar.gz: 25e7ad5dcdb45e4d24744bcb72a06a46588cc0712d4eed9e2990246ad3bb625a
5
5
  SHA512:
6
- metadata.gz: d9b1bec06d8ab4ee6715054ad7a96b3013532c2e62fd76eb5a68e0b6425b8a0f62f7b395dc3c60ab607a76926b3ae1ebeb928fbd9757977064b9c20a643ddc7c
7
- data.tar.gz: ea66c98c374e78172b7cfa5daa7b236357bd4a74f624a86803725bddfb2b6503d4da95b88f1220d4d5aef1a48dfb7e481c63897e35fff7bba26d71df387a0ca3
6
+ metadata.gz: f25c0927c1bdde7e99f7b9be29734978a99d39dba10cc3143a2ef6968fe4456713670709805888c69cb44a2b961deb8e3f8c5d5218f0afbb27d4357cacee80a8
7
+ data.tar.gz: b94698d0faf4d31672d5434b26ed4e2a3025a55b846db3512d458ab1176bcf45b0f5adbfcc68030e6428472bc5ef9cbf864a5923350d160d81b42c1c9d28d0be
data/.rubocop.yml CHANGED
@@ -29,5 +29,8 @@ Naming/FileName:
29
29
  - 'lib/help_scout-sdk.rb'
30
30
  - 'spec/**/*_spec.rb'
31
31
 
32
+ Naming/MemoizedInstanceVariableName:
33
+ EnforcedStyleForLeadingUnderscores: required
34
+
32
35
  Style/Documentation:
33
36
  Enabled: False
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  # Help Scout API Wrapper
2
2
 
3
3
  [![Build Status](https://travis-ci.org/taxjar/help_scout-sdk.svg?branch=master)](https://travis-ci.org/taxjar/help_scout-sdk)
4
-
5
- 🚨 WORK IN PROGRESS 🚨
4
+ [![Gem Version](https://badge.fury.io/rb/help_scout-sdk.svg)](https://badge.fury.io/rb/help_scout-sdk)
6
5
 
7
6
  This gem is a wrapper around the Help Scout API. The current version is targeting the [Mailbox 2.0 API](https://developer.helpscout.com/mailbox-api/).
8
7
 
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
  spec.email = ['support@taxjar.com']
12
12
 
13
13
  spec.summary = 'Ruby Help Scout SDK'
14
- spec.description = 'Ruby Help Scout SDK.'
14
+ spec.description = 'Ruby Help Scout (aka HelpScout) SDK.'
15
15
  spec.homepage = 'https://github.com/taxjar/help_scout-sdk'
16
16
  spec.license = 'MIT'
17
17
 
@@ -5,15 +5,14 @@ module HelpScout
5
5
  class AccessToken
6
6
  class << self
7
7
  def create
8
- connection = HelpScout::API::Client.new.unauthorized_connection
8
+ connection = HelpScout::API::Client.new(authorize: false).connection
9
9
  response = connection.post('oauth2/token', token_request_params)
10
10
 
11
11
  case response.status
12
- when 429 then raise ThrottleLimitReached, response.body&.dig('error')
13
- when 500, 501, 503 then raise InternalError, response.body&.dig('error')
12
+ when 200 then new HelpScout::Response.new(response).body
13
+ when 429 then raise HelpScout::API::ThrottleLimitReached, response.body&.dig('error')
14
+ else raise HelpScout::API::InternalError, "unexpected response (status #{response.status})"
14
15
  end
15
-
16
- new HelpScout::Response.new(response).body
17
16
  end
18
17
 
19
18
  def refresh!
@@ -23,7 +22,7 @@ module HelpScout
23
22
  private
24
23
 
25
24
  def token_request_params
26
- @token_request_params ||= {
25
+ @_token_request_params ||= {
27
26
  grant_type: 'client_credentials',
28
27
  client_id: HelpScout.app_id,
29
28
  client_secret: HelpScout.app_secret
@@ -31,21 +30,33 @@ module HelpScout
31
30
  end
32
31
  end
33
32
 
34
- attr_reader :expires_at, :expires_in, :value
33
+ attr_accessor :invalid
34
+ attr_reader :expires_in, :value
35
35
 
36
36
  def initialize(params)
37
37
  @value = params[:access_token]
38
- expires_in = params[:expires_in]
39
-
40
- return unless expires_in
38
+ @expires_in = params[:expires_in]
39
+ end
41
40
 
42
- @expires_in = expires_in
43
- @expires_at = Time.now.utc + expires_in
41
+ def expires_at
42
+ @_expires_at ||= Time.now.utc + expires_in
44
43
  end
45
44
 
46
45
  def expired?
47
46
  Time.now.utc > expires_at
48
47
  end
48
+
49
+ def invalid?
50
+ invalid
51
+ end
52
+
53
+ def invalidate!
54
+ self.invalid = true
55
+ end
56
+
57
+ def stale?
58
+ invalid? || expired?
59
+ end
49
60
  end
50
61
  end
51
62
  end
@@ -3,29 +3,37 @@
3
3
  module HelpScout
4
4
  class API
5
5
  class Client
6
- def authorized_connection
7
- @authorized_connection ||= begin
8
- HelpScout::API::AccessToken.refresh! if HelpScout.access_token.nil?
9
- build_connection
10
- end
6
+ attr_reader :authorize
7
+
8
+ def initialize(authorize: true)
9
+ @authorize = authorize
11
10
  end
12
11
 
13
- def unauthorized_connection
14
- @unauthorized_connection ||= begin
15
- build_connection(authorize: false)
12
+ def connection
13
+ @_connection ||= begin
14
+ HelpScout::API::AccessToken.refresh! if authorize? && token_needs_refresh?
15
+ build_connection
16
16
  end
17
17
  end
18
18
 
19
19
  private
20
20
 
21
- def build_connection(authorize: true)
21
+ def authorize?
22
+ authorize
23
+ end
24
+
25
+ def build_connection
22
26
  Faraday.new(url: BASE_URL) do |conn|
23
27
  conn.request :json
24
- conn.authorization(:Bearer, HelpScout.access_token.value) if authorize && HelpScout.access_token&.value
28
+ conn.authorization(:Bearer, HelpScout.access_token.value) if authorize? && HelpScout.access_token&.value
25
29
  conn.response(:json, content_type: /\bjson$/)
26
30
  conn.adapter(Faraday.default_adapter)
27
31
  end
28
32
  end
33
+
34
+ def token_needs_refresh?
35
+ HelpScout.access_token.nil? || HelpScout.access_token.stale?
36
+ end
29
37
  end
30
38
  end
31
39
  end
@@ -30,29 +30,30 @@ module HelpScout
30
30
 
31
31
  private
32
32
 
33
- def handle_response(result) # rubocop:disable AbcSize
34
- case result.status
35
- when 400 then raise BadRequest, result.body&.dig('validationErrors')
36
- when 401 then raise NotAuthorized, result.body&.dig('error_description')
37
- when 404 then raise NotFound, 'Resource Not Found'
38
- when 429 then raise ThrottleLimitReached, result.body&.dig('error')
39
- when 500, 501, 503 then raise InternalError, result.body&.dig('error')
33
+ def handle_response(result) # rubocop:disable AbcSize, Metrics/MethodLength
34
+ if (200...300).include? result.status
35
+ HelpScout::Response.new(result)
36
+ else
37
+ case result.status
38
+ when 400 then raise BadRequest, result.body&.dig('validationErrors')
39
+ when 401 then raise NotAuthorized, result.body&.dig('error_description')
40
+ when 404 then raise NotFound, 'Resource Not Found'
41
+ when 429 then raise ThrottleLimitReached, result.body&.dig('error')
42
+ else raise InternalError, result.body
43
+ end
40
44
  end
41
-
42
- HelpScout::Response.new(result)
43
45
  end
44
46
 
45
47
  def new_connection
46
- HelpScout::API::Client.new.authorized_connection
48
+ HelpScout::API::Client.new.connection
47
49
  end
48
50
 
49
51
  def send_request(action, path, params)
50
- connection = new_connection
51
- response = connection.send(action, path, params.compact)
52
+ response = new_connection.send(action, path, params.compact)
52
53
 
53
54
  if response.status == 401
54
- HelpScout::API::AccessToken.refresh!
55
- response = connection.send(action, path, params.compact)
55
+ access_token.invalidate!
56
+ response = new_connection.send(action, path, params.compact)
56
57
  end
57
58
 
58
59
  handle_response(response)
@@ -6,6 +6,8 @@ module HelpScout
6
6
  attr_reader :access_token
7
7
 
8
8
  def access_token=(token_value)
9
+ return unless token_value
10
+
9
11
  @access_token = HelpScout::API::AccessToken.new(access_token: token_value)
10
12
  end
11
13
  end
@@ -61,7 +61,7 @@ module HelpScout
61
61
  end
62
62
 
63
63
  def populated_threads
64
- @populated_threads ||= HelpScout::Thread.list(id)
64
+ @_populated_threads ||= HelpScout::Thread.list(id)
65
65
  end
66
66
 
67
67
  def update(operation, path, value = nil)
@@ -27,11 +27,11 @@ module HelpScout
27
27
  end
28
28
 
29
29
  def fields
30
- @fields ||= HelpScout.api.get(fields_path).embedded[:fields]
30
+ @_fields ||= HelpScout.api.get(fields_path).embedded[:fields]
31
31
  end
32
32
 
33
33
  def folders
34
- @folders ||= HelpScout::Folder.list(id: id)
34
+ @_folders ||= HelpScout::Folder.list(id: id)
35
35
  end
36
36
 
37
37
  private
@@ -10,7 +10,7 @@ module HelpScout
10
10
  end
11
11
 
12
12
  def body
13
- @body ||= response.body.deep_transform_keys { |key| key.to_s.underscore.to_sym }
13
+ @_body ||= response.body.deep_transform_keys { |key| key.to_s.underscore.to_sym }
14
14
  end
15
15
 
16
16
  def embedded
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HelpScout
4
- VERSION = '1.0.0'
4
+ VERSION = '1.0.1'
5
5
  end
@@ -35,7 +35,7 @@ module HelpScout
35
35
  end
36
36
 
37
37
  def api
38
- @api ||= HelpScout::API.new
38
+ @_api ||= HelpScout::API.new
39
39
  end
40
40
 
41
41
  def app_id
@@ -47,7 +47,7 @@ module HelpScout
47
47
  end
48
48
 
49
49
  def configuration
50
- @configuration ||= Configuration.new
50
+ @_configuration ||= Configuration.new
51
51
  end
52
52
 
53
53
  def configure
@@ -59,8 +59,8 @@ module HelpScout
59
59
  configuration.default_mailbox
60
60
  end
61
61
 
62
- def refresh!
63
- @api = nil
62
+ def reset!
63
+ @_api = HelpScout::API.new
64
64
  end
65
65
  end
66
66
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: help_scout-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - TaxJar
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-05 00:00:00.000000000 Z
11
+ date: 2019-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -212,7 +212,7 @@ dependencies:
212
212
  - - "~>"
213
213
  - !ruby/object:Gem::Version
214
214
  version: '3.3'
215
- description: Ruby Help Scout SDK.
215
+ description: Ruby Help Scout (aka HelpScout) SDK.
216
216
  email:
217
217
  - support@taxjar.com
218
218
  executables: []