help_scout-sdk 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: db597946131f89598f48083d3352273b5adde0f23dfe608a9f42f4a0d836be83
4
+ data.tar.gz: d7e837bf6257fda9b10f21e74948068df19f50033885e70b30120004e98b6717
5
+ SHA512:
6
+ metadata.gz: d9b1bec06d8ab4ee6715054ad7a96b3013532c2e62fd76eb5a68e0b6425b8a0f62f7b395dc3c60ab607a76926b3ae1ebeb928fbd9757977064b9c20a643ddc7c
7
+ data.tar.gz: ea66c98c374e78172b7cfa5daa7b236357bd4a74f624a86803725bddfb2b6503d4da95b88f1220d4d5aef1a48dfb7e481c63897e35fff7bba26d71df387a0ca3
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ *.gem
11
+ .env*
12
+ .byebug_history
13
+ .rspec_status
14
+ .ruby-gemset
15
+ .ruby-version
16
+
17
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,33 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+
4
+ Layout:
5
+ DotPosition:
6
+ EnforcedStyle: trailing
7
+ MultilineMethodCallIndentation:
8
+ EnforcedStyle: indented
9
+
10
+ Lint/AmbiguousBlockAssociation:
11
+ Exclude:
12
+ - 'spec/**/*_spec.rb'
13
+
14
+ Metrics/BlockLength:
15
+ Exclude:
16
+ - '*.gemspec'
17
+ - 'Rakefile'
18
+ - '**/*.rake'
19
+ - 'spec/**/*_spec.rb'
20
+
21
+ Metrics/ClassLength:
22
+ Max: 125
23
+
24
+ Metrics/LineLength:
25
+ Max: 120
26
+
27
+ Naming/FileName:
28
+ Exclude:
29
+ - 'lib/help_scout-sdk.rb'
30
+ - 'spec/**/*_spec.rb'
31
+
32
+ Style/Documentation:
33
+ Enabled: False
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.3
5
+ - 2.4
6
+ - 2.5
7
+ - 2.6
8
+
9
+ before_install:
10
+ - gem update --system
11
+ - gem install bundler
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at andre@taxjar.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in helpscout.gemspec
8
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Andre LeBlanc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # Help Scout API Wrapper
2
+
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 🚨
6
+
7
+ 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
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem "help_scout-sdk"
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ ## Progress
22
+
23
+ | Models | List | Get | Create | Update | Delete |
24
+ | :------------------------- | :--: | :--: | :-----: | :----: | :-----: |
25
+ | Attachment | ❌ | ❌ | ✅ | ❌ | ❌ |
26
+ | Conversations | ✅ | ✅ | ✅ | ✅ | ❌ |
27
+ | Conversation::Threads | ❌ | ❌ | ❌ | ❌ | ❌ |
28
+ | Conversation::ThreadSource | ❌ | ❌ | ❌ | ❌ | ❌ |
29
+ | Customers | ✅ | ✅ | ❌ | ❌ | ❌ |
30
+ | Notes | ❌ | ❌ | ❌ | ❌ | ❌ |
31
+ | Mailboxes | ✅ | ✅ | ➖ | ➖ | ➖ |
32
+ | Mailbox::Folders | ✅ | ➖ | ➖ | ➖ | ➖ |
33
+ | Mailbox::Workflows | ❌ | ❌ | ❌ | ❌ | ❌ |
34
+ | Tags | ❌ | ❌ | ❌ | ❌ | ❌ |
35
+ | Teams | ❌ | ❌ | ❌ | ❌ | ❌ |
36
+ | Team::Members | ❌ | ❌ | ❌ | ❌ | ❌ |
37
+ | Users | ✅ | ✅ | ❌ | ❌ | ❌ |
38
+ | Workflows | ❌ | ❌ | ❌ | ❌ | ❌ |
39
+
40
+ | Endpoint | Supported |
41
+ | -------- | :-------: |
42
+ | Reports | ❌ |
43
+ | Search | ❌ |
44
+ | Webhooks | ❌ |
45
+
46
+ ## Usage
47
+
48
+ ### Configuration
49
+
50
+ ```ruby
51
+ HelpScout.configure do |config|
52
+ config.app_id = ENV["HELP_SCOUT_APP_ID"]
53
+ config.app_secret = ENV["HELP_SCOUT_APP_SECRET"]
54
+
55
+ config.access_token = HelpScout::API::AccessToken.create
56
+ end
57
+ ```
58
+
59
+ ### Conversations
60
+
61
+ [Documentation Link](https://developer.helpscout.com/mailbox-api/endpoints/conversations/list/)
62
+
63
+ ```ruby
64
+ HelpScout::Conversation.list
65
+ location = HelpScout::Conversation.create(...)
66
+ conversation = HelpScout::Conversation.get(location.split("/").last)
67
+ conversation.update("replace", "/subject", "New Subject")
68
+ ```
69
+
70
+ ### Customers
71
+
72
+ [Documentation Link](https://developer.helpscout.com/mailbox-api/endpoints/customers/list/)
73
+
74
+ ```ruby
75
+ HelpScout::Customer.list
76
+ HelpScout::Customer.get(id)
77
+ ```
78
+
79
+ ### Mailboxes
80
+
81
+ [Documentation Link](https://developer.helpscout.com/mailbox-api/endpoints/mailboxes/list/)
82
+
83
+ ```ruby
84
+ HelpScout::Mailbox.list
85
+ mailbox = HelpScout::Mailbox.get(id)
86
+ mailbox.fields
87
+ mailbox.folders
88
+ ```
89
+
90
+ ### Users
91
+
92
+ [Documentation Link](https://developer.helpscout.com/mailbox-api/endpoints/users/list/)
93
+
94
+ ```ruby
95
+ HelpScout::User.list
96
+ user = HelpScout::User.get(id)
97
+ ```
98
+
99
+ ## Development
100
+
101
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
102
+
103
+ To install this gem onto your local machine, run `bundle exec rake install`.
104
+
105
+ ## Contributing
106
+
107
+ Bug reports and pull requests are welcome on GitHub at https://github.com/taxjar/help_scout-sdk. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
108
+
109
+ ## License
110
+
111
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
112
+
113
+ ## Code of Conduct
114
+
115
+ Everyone interacting in the H2 project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/taxjar/help_scout-sdk/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/audit/task'
4
+ require 'bundler/gem_tasks'
5
+ require 'rspec/core/rake_task'
6
+ require 'rubocop/rake_task'
7
+
8
+ Bundler::Audit::Task.new
9
+ RSpec::Core::RakeTask.new(:spec)
10
+ RuboCop::RakeTask.new
11
+
12
+ desc 'Run CI'
13
+ task :ci do
14
+ Rake::Task['bundle:audit'].invoke
15
+ Rake::Task['rubocop'].invoke
16
+ Rake::Task['spec'].invoke
17
+ end
18
+
19
+ task default: :ci
data/bin/console ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'dotenv/load'
6
+ require 'awesome_print'
7
+ require 'byebug'
8
+ require 'help_scout-sdk'
9
+
10
+ HelpScout.configure do |config|
11
+ config.app_id = ENV.fetch('HELP_SCOUT_APP_ID')
12
+ config.app_secret = ENV.fetch('HELP_SCOUT_APP_SECRET')
13
+ config.default_mailbox = ENV.fetch('HELP_SCOUT_DEFAULT_MAILBOX')
14
+ config.access_token = ENV.fetch('HELP_SCOUT_ACCESS_TOKEN', nil)
15
+ end
16
+
17
+ begin
18
+ require 'pry'
19
+ Pry.start
20
+ rescue LoadError
21
+ require 'irb'
22
+ IRB.start(__FILE__)
23
+ end
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'help_scout/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'help_scout-sdk'
9
+ spec.version = HelpScout::VERSION
10
+ spec.authors = ['TaxJar']
11
+ spec.email = ['support@taxjar.com']
12
+
13
+ spec.summary = 'Ruby Help Scout SDK'
14
+ spec.description = 'Ruby Help Scout SDK.'
15
+ spec.homepage = 'https://github.com/taxjar/help_scout-sdk'
16
+ spec.license = 'MIT'
17
+
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['source_code_uri'] = 'https://github.com/taxjar/help_scout-sdk'
20
+ spec.metadata['bug_tracker_uri'] = 'https://github.com/taxjar/help_scout-sdk/issues'
21
+ else
22
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
23
+ 'public gem pushes.'
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = 'exe'
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ['lib']
32
+
33
+ spec.add_dependency 'activesupport'
34
+ spec.add_dependency 'faraday', '~> 0.10'
35
+ spec.add_dependency 'faraday_middleware', '>= 0.10.1', '< 1.0'
36
+ spec.required_ruby_version = '>= 2.3'
37
+
38
+ spec.add_development_dependency 'awesome_print', '~> 1.8'
39
+ spec.add_development_dependency 'bundler', '~> 2.0'
40
+ spec.add_development_dependency 'bundler-audit', '~> 0.6'
41
+ spec.add_development_dependency 'byebug'
42
+ spec.add_development_dependency 'dotenv', '~> 2.2'
43
+ spec.add_development_dependency 'pry'
44
+ spec.add_development_dependency 'rake', '~> 10.0'
45
+ spec.add_development_dependency 'rspec', '~> 3.0'
46
+ spec.add_development_dependency 'rubocop', '~> 0.70'
47
+ spec.add_development_dependency 'vcr', '~> 5.0'
48
+ spec.add_development_dependency 'webmock', '~> 3.3'
49
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HelpScout
4
+ class API
5
+ class AccessToken
6
+ class << self
7
+ def create
8
+ connection = HelpScout::API::Client.new.unauthorized_connection
9
+ response = connection.post('oauth2/token', token_request_params)
10
+
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')
14
+ end
15
+
16
+ new HelpScout::Response.new(response).body
17
+ end
18
+
19
+ def refresh!
20
+ HelpScout.api.access_token = create
21
+ end
22
+
23
+ private
24
+
25
+ def token_request_params
26
+ @token_request_params ||= {
27
+ grant_type: 'client_credentials',
28
+ client_id: HelpScout.app_id,
29
+ client_secret: HelpScout.app_secret
30
+ }
31
+ end
32
+ end
33
+
34
+ attr_reader :expires_at, :expires_in, :value
35
+
36
+ def initialize(params)
37
+ @value = params[:access_token]
38
+ expires_in = params[:expires_in]
39
+
40
+ return unless expires_in
41
+
42
+ @expires_in = expires_in
43
+ @expires_at = Time.now.utc + expires_in
44
+ end
45
+
46
+ def expired?
47
+ Time.now.utc > expires_at
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HelpScout
4
+ class API
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
11
+ end
12
+
13
+ def unauthorized_connection
14
+ @unauthorized_connection ||= begin
15
+ build_connection(authorize: false)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def build_connection(authorize: true)
22
+ Faraday.new(url: BASE_URL) do |conn|
23
+ conn.request :json
24
+ conn.authorization(:Bearer, HelpScout.access_token.value) if authorize && HelpScout.access_token&.value
25
+ conn.response(:json, content_type: /\bjson$/)
26
+ conn.adapter(Faraday.default_adapter)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HelpScout
4
+ class API
5
+ class BadRequest < StandardError; end
6
+ class NotAuthorized < StandardError; end
7
+ class NotFound < StandardError; end
8
+ class InternalError < StandardError; end
9
+ class ThrottleLimitReached < StandardError; end
10
+
11
+ BASE_URL = 'https://api.helpscout.net/v2/'
12
+
13
+ attr_accessor :access_token
14
+
15
+ def get(path, params = {})
16
+ send_request(:get, path, params)
17
+ end
18
+
19
+ def patch(path, params)
20
+ send_request(:patch, path, params)
21
+ end
22
+
23
+ def post(path, params)
24
+ send_request(:post, path, params)
25
+ end
26
+
27
+ def put(path, params)
28
+ send_request(:put, path, params)
29
+ end
30
+
31
+ private
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')
40
+ end
41
+
42
+ HelpScout::Response.new(result)
43
+ end
44
+
45
+ def new_connection
46
+ HelpScout::API::Client.new.authorized_connection
47
+ end
48
+
49
+ def send_request(action, path, params)
50
+ connection = new_connection
51
+ response = connection.send(action, path, params.compact)
52
+
53
+ if response.status == 401
54
+ HelpScout::API::AccessToken.refresh!
55
+ response = connection.send(action, path, params.compact)
56
+ end
57
+
58
+ handle_response(response)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HelpScout
4
+ class Attachment < HelpScout::Base
5
+ class << self
6
+ def create(conversation_id, thread_id, params)
7
+ HelpScout.api.post(
8
+ create_path(conversation_id, thread_id),
9
+ HelpScout::Util.camelize_keys(params)
10
+ )
11
+ true
12
+ end
13
+
14
+ private
15
+
16
+ def base_path
17
+ 'conversations/%<CONVERSATION_ID>/threads/%<THREAD_ID>/attachments'
18
+ end
19
+
20
+ def create_path(conversation_id, thread_id)
21
+ replacements = {
22
+ '%<CONVERSATION_ID>' => conversation_id,
23
+ '%<THREAD_ID>' => thread_id
24
+ }
25
+
26
+ HelpScout::Util.parse_path(base_path, replacements)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HelpScout
4
+ class Base
5
+ class << self
6
+ private
7
+
8
+ def base_path
9
+ name.split('::').last.downcase.pluralize
10
+ end
11
+ end
12
+
13
+ def to_h(method = :to_h) # rubocop:disable Metrics/MethodLength
14
+ {}.tap do |result|
15
+ instance_variables.each do |var|
16
+ attribute = {
17
+ name: var,
18
+ value: HelpScout::Util.serialized_value(instance_variable_get(var), method)
19
+ }
20
+ if block_given?
21
+ yield(result, attribute)
22
+ else
23
+ result[keyify(attribute[:name])] = attribute[:value]
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ def as_json
30
+ to_h(:as_json) do |result, attribute|
31
+ result[HelpScout::Util.jsonify(attribute[:name])] = attribute[:value]
32
+ end
33
+ end
34
+
35
+ def to_json(*_args)
36
+ as_json.to_json
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HelpScout
4
+ class Configuration
5
+ attr_accessor :app_id, :app_secret, :default_mailbox
6
+ attr_reader :access_token
7
+
8
+ def access_token=(token_value)
9
+ @access_token = HelpScout::API::AccessToken.new(access_token: token_value)
10
+ end
11
+ end
12
+ end