help_scout-sdk 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HelpScout
4
+ class Conversation < HelpScout::Base
5
+ extend Getable
6
+ extend Listable
7
+
8
+ class << self
9
+ def create(params)
10
+ response = HelpScout.api.post(create_path, HelpScout::Util.camelize_keys(params))
11
+ response.location
12
+ end
13
+
14
+ private
15
+
16
+ def create_path
17
+ base_path
18
+ end
19
+
20
+ def list_path(mailbox_id)
21
+ "#{base_path}?mailbox=#{mailbox_id}"
22
+ end
23
+ end
24
+
25
+ BASIC_ATTRIBUTES = %i[
26
+ id
27
+ number
28
+ type
29
+ folder_id
30
+ status
31
+ state
32
+ subject
33
+ preview
34
+ mailbox_id
35
+ assignee
36
+ created_by
37
+ created_at
38
+ closed_by
39
+ closed_at
40
+ user_updated_at
41
+ customer_waiting_since
42
+ source
43
+ tags
44
+ cc
45
+ bcc
46
+ primary_customer
47
+ custom_fields
48
+ ].freeze
49
+
50
+ attr_accessor(*BASIC_ATTRIBUTES)
51
+ attr_reader :hrefs
52
+
53
+ def initialize(params)
54
+ BASIC_ATTRIBUTES.each do |attribute|
55
+ next unless params[attribute]
56
+
57
+ instance_variable_set("@#{attribute}", params[attribute])
58
+ end
59
+
60
+ @hrefs = HelpScout::Util.map_links(params.fetch(:_links, []))
61
+ end
62
+
63
+ def populated_threads
64
+ @populated_threads ||= HelpScout::Thread.list(id)
65
+ end
66
+
67
+ def update(operation, path, value = nil)
68
+ update_path = URI.parse(hrefs[:self]).path
69
+ HelpScout.api.patch(update_path, op: operation, path: path, value: value)
70
+ true
71
+ end
72
+
73
+ def update_tags(new_tags = nil)
74
+ new_tags ||= []
75
+ tags_path = URI.parse(hrefs[:self]).path + '/tags'
76
+ HelpScout.api.put(tags_path, tags: new_tags)
77
+ true
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HelpScout
4
+ class Customer < HelpScout::Base
5
+ extend Getable
6
+ extend Listable
7
+
8
+ BASIC_ATTRIBUTES = %i[
9
+ first_name
10
+ last_name
11
+ photo_url
12
+ job_title
13
+ photo_type
14
+ background
15
+ location
16
+ created_at
17
+ updated_at
18
+ organization
19
+ gender
20
+ age
21
+ id
22
+ ].freeze
23
+ EMBEDDED_ATTRIBUTES = %i[
24
+ addresses
25
+ chats
26
+ emails
27
+ phones
28
+ social_profiles
29
+ websites
30
+ ].freeze
31
+ attr_reader(*(BASIC_ATTRIBUTES + EMBEDDED_ATTRIBUTES))
32
+ attr_reader :hrefs
33
+
34
+ def initialize(params = {})
35
+ BASIC_ATTRIBUTES.each do |attribute|
36
+ next unless params[attribute]
37
+
38
+ instance_variable_set("@#{attribute}", params[attribute])
39
+ end
40
+
41
+ embedded_params = params.fetch(:_embedded, {})
42
+ EMBEDDED_ATTRIBUTES.each do |attribute|
43
+ next unless embedded_params[attribute]
44
+
45
+ instance_variable_set("@#{attribute}", embedded_params[attribute])
46
+ end
47
+
48
+ @hrefs = HelpScout::Util.map_links(params[:_links])
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HelpScout
4
+ class Folder < HelpScout::Base
5
+ extend Listable
6
+
7
+ class << self
8
+ private
9
+
10
+ def base_path
11
+ 'mailboxes/%<MAILBOX_ID>/folders/'
12
+ end
13
+
14
+ def list_path(mailbox_id)
15
+ replacements = {
16
+ '%<MAILBOX_ID>' => mailbox_id
17
+ }
18
+
19
+ HelpScout::Util.parse_path(base_path, replacements)
20
+ end
21
+ end
22
+
23
+ BASIC_ATTRIBUTES = %i[
24
+ id
25
+ name
26
+ type
27
+ user_id
28
+ total_count
29
+ active_count
30
+ updated_at
31
+ ].freeze
32
+ attr_reader(*BASIC_ATTRIBUTES)
33
+
34
+ def initialize(params)
35
+ BASIC_ATTRIBUTES.each do |attribute|
36
+ next unless params[attribute]
37
+
38
+ instance_variable_set("@#{attribute}", params[attribute])
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HelpScout
4
+ class Mailbox < HelpScout::Base
5
+ extend Getable
6
+ extend Listable
7
+
8
+ BASIC_ATTRIBUTES = %i[
9
+ id
10
+ name
11
+ slug
12
+ email
13
+ created_at
14
+ updated_at
15
+ ].freeze
16
+ attr_reader(*BASIC_ATTRIBUTES)
17
+ attr_reader :hrefs
18
+
19
+ def initialize(params)
20
+ BASIC_ATTRIBUTES.each do |attribute|
21
+ next unless params[attribute]
22
+
23
+ instance_variable_set("@#{attribute}", params[attribute])
24
+ end
25
+
26
+ @hrefs = HelpScout::Util.map_links(params[:_links])
27
+ end
28
+
29
+ def fields
30
+ @fields ||= HelpScout.api.get(fields_path).embedded[:fields]
31
+ end
32
+
33
+ def folders
34
+ @folders ||= HelpScout::Folder.list(id: id)
35
+ end
36
+
37
+ private
38
+
39
+ def fields_path
40
+ hrefs[:fields]
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HelpScout
4
+ module Getable
5
+ def get(id)
6
+ new parse_item(HelpScout.api.get(get_path(id)))
7
+ end
8
+
9
+ private
10
+
11
+ def get_path(id)
12
+ "#{base_path}/#{id}"
13
+ end
14
+
15
+ def parse_item(response)
16
+ response.body
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HelpScout
4
+ module Listable
5
+ def list(id: HelpScout.default_mailbox, page: nil)
6
+ HelpScout.api.get(list_path(id), page: page).embedded_list.map { |e| new e }
7
+ end
8
+
9
+ def list_path(_)
10
+ base_path
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HelpScout
4
+ class Response
5
+ delegate :headers, :status, :success?, to: :response
6
+
7
+ attr_reader :response
8
+ def initialize(response)
9
+ @response = response
10
+ end
11
+
12
+ def body
13
+ @body ||= response.body.deep_transform_keys { |key| key.to_s.underscore.to_sym }
14
+ end
15
+
16
+ def embedded
17
+ body[:_embedded]
18
+ end
19
+
20
+ def embedded_list
21
+ embedded.values.first
22
+ end
23
+
24
+ def item
25
+ body[:item]
26
+ end
27
+
28
+ def items
29
+ body[:items]
30
+ end
31
+
32
+ def location
33
+ headers['location']
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HelpScout
4
+ class Thread < HelpScout::Base
5
+ class << self
6
+ def list(conversation_id, page: nil)
7
+ HelpScout.api.get(list_path(conversation_id), page: page).embedded_list.map { |details| new details }
8
+ end
9
+
10
+ private
11
+
12
+ def list_path(conversation_id)
13
+ "conversations/#{conversation_id}/threads"
14
+ end
15
+ end
16
+
17
+ BASIC_ATTRIBUTES = %i[
18
+ id
19
+ assigned_to
20
+ status
21
+ state
22
+ action
23
+ body
24
+ source
25
+ customer
26
+ created_by
27
+ saved_reply_id
28
+ type
29
+ to
30
+ cc
31
+ bcc
32
+ created_at
33
+ opened_at
34
+ attachments
35
+ ].freeze
36
+
37
+ attr_accessor(*BASIC_ATTRIBUTES)
38
+ attr_reader :hrefs
39
+
40
+ def initialize(params)
41
+ BASIC_ATTRIBUTES.each do |attribute|
42
+ next unless params[attribute]
43
+
44
+ instance_variable_set("@#{attribute}", params[attribute])
45
+ end
46
+
47
+ @hrefs = HelpScout::Util.map_links(params.fetch(:_links, []))
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HelpScout
4
+ class User < HelpScout::Base
5
+ extend Getable
6
+ extend Listable
7
+
8
+ BASIC_ATTRIBUTES = %i[
9
+ id
10
+ first_name
11
+ last_name
12
+ email
13
+ created_at
14
+ updated_at
15
+ role
16
+ timezone
17
+ type
18
+ photoUrl
19
+ ].freeze
20
+
21
+ attr_reader(*BASIC_ATTRIBUTES)
22
+ attr_reader :hrefs
23
+
24
+ def initialize(params = {})
25
+ BASIC_ATTRIBUTES.each do |attribute|
26
+ next unless params[attribute]
27
+
28
+ instance_variable_set("@#{attribute}", params[attribute])
29
+ end
30
+
31
+ @hrefs = HelpScout::Util.map_links(params[:_links])
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HelpScout
4
+ module Util
5
+ # TODO: Consider including in Base
6
+ class << self
7
+ def camelize(term)
8
+ term = term.to_s.split('_').collect(&:capitalize).join
9
+ term[0] = term[0].downcase
10
+ term
11
+ end
12
+
13
+ def camelize_keys(source)
14
+ source.each_with_object({}) do |(key, value), results|
15
+ results[camelize(key)] = if value.is_a? Hash
16
+ camelize_keys(value)
17
+ else
18
+ value
19
+ end
20
+ end
21
+ end
22
+
23
+ def jsonify(term)
24
+ camelize(keyify(term))
25
+ end
26
+
27
+ def keyify(term)
28
+ term.to_s.delete('@')
29
+ end
30
+
31
+ def map_links(links)
32
+ links.map { |k, v| [k, v[:href]] }.to_h
33
+ end
34
+
35
+ def parse_path(path_template, replacements)
36
+ placeholders = Regexp.union(replacements.keys)
37
+ path_template.gsub(placeholders) { |match_text| replacements[match_text] }
38
+ end
39
+
40
+ def serialized_value(value, type)
41
+ if value.is_a? Array
42
+ value.map { |v| serialized_value(v, type) }
43
+ else
44
+ value.class < HelpScout::Base ? value.send(type) : value
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HelpScout
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support'
4
+ require 'active_support/core_ext'
5
+ require 'json'
6
+ require 'faraday'
7
+ require 'faraday_middleware'
8
+
9
+ require 'help_scout/version'
10
+
11
+ require 'help_scout/base'
12
+ require 'help_scout/modules/getable'
13
+ require 'help_scout/modules/listable'
14
+
15
+ require 'help_scout/api'
16
+ require 'help_scout/api/access_token'
17
+ require 'help_scout/api/client'
18
+ require 'help_scout/attachment'
19
+ require 'help_scout/configuration'
20
+ require 'help_scout/conversation'
21
+ require 'help_scout/customer'
22
+ require 'help_scout/folder'
23
+ require 'help_scout/mailbox'
24
+ require 'help_scout/response'
25
+ require 'help_scout/thread'
26
+ require 'help_scout/user'
27
+ require 'help_scout/util'
28
+
29
+ module HelpScout
30
+ class << self
31
+ attr_writer :configuration
32
+
33
+ def access_token
34
+ api.access_token
35
+ end
36
+
37
+ def api
38
+ @api ||= HelpScout::API.new
39
+ end
40
+
41
+ def app_id
42
+ configuration.app_id
43
+ end
44
+
45
+ def app_secret
46
+ configuration.app_secret
47
+ end
48
+
49
+ def configuration
50
+ @configuration ||= Configuration.new
51
+ end
52
+
53
+ def configure
54
+ yield(configuration)
55
+ api.access_token = HelpScout.configuration.access_token
56
+ end
57
+
58
+ def default_mailbox
59
+ configuration.default_mailbox
60
+ end
61
+
62
+ def refresh!
63
+ @api = nil
64
+ end
65
+ end
66
+ end