-
Notifications
You must be signed in to change notification settings - Fork 41
/
client.rb
931 lines (817 loc) · 28.5 KB
/
client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
# Help Scout API V1 Client
# http://developer.helpscout.net/
#
# Authentication
# This is an HTTPS-only API. Authentication will be based on API Keys, which can
# be generated the Help Scout UI in the User Profile. Each API Key will map to
# an existing Help Scout user. Results returned from various responses will be
# based upon the role of the user to which the API key is tied.
#
# The API Key will be passed via HTTP Basic Authentication. The API Key will go
# in the username field and a dummy password, such as X, in the password field.
#
# Rate Limiting
# Requests will be limited to 200 requests per minute. Response code 429 will
# be returned when throttle limit has been reached. A "Retry-After" header will
# be returned indicating how many seconds to wait until retry.
#
# Formats
# Each endpoint will specify the response format in the URL. However, the API
# will only support JSON at this time.
#
# Usage
# 1. Follow the instructions at Help Scout's Developers site to generate an API
# key: http://developer.helpscout.net/
# 2. Add your API Key to config/helpscout.yml:
# api_key: XXXXXX
# 3. Initialize your Help Scout client:
# HelpScout::Client.new
# 4. You may now query the Help Scout API:
# mailboxes = HelpScout::Client.mailboxes
#
# You may also initialize a client without using helpscout.yml by passing the
# API Key to new:
# HelpScout::Client.new(XXXXXX)
require "erb"
require "httparty"
require "yaml"
module HelpScout
class Client
include HTTParty
# All API requests will be made to: https://api.helpscout.net/. All
# requests are served over HTTPS. The current version is v1.
base_uri 'https://api.helpscout.net/v1'
@@settings ||= nil
# Returns the current Help Scout Client settings.
# If no settings have been loaded yet, this function will load its
# configuration from helpscout.yml
#
# Settings
# api_key String Help Scout API Key. The API is currently available for
# paying Help Scout accounts (Basic or Standard plan). You
# can generate a key from your User Profile, on the API
# Keys tab.
def self.settings
if @@settings.nil?
path = "config/helpscout.yml"
if File.exist?(path)
@@settings = YAML.load(ERB.new(File.new(path).read).result)
end
end
@@settings
end
# Requests a single item from the Help Scout API. Should return either an
# item from the SingleItemEnvelope, or raise an error with an
# ErrorEnvelope.
#
# url String A string representing the url for the REST endpoint to be
# queried.
# params Hash A hash of GET parameters to use for this particular
# request.
#
# Response
# Name Type Notes
# Header Status Int 200
# Body item
def self.request_item(auth, url, params = {})
item = nil
request_url = ""
request_url << url
if params
query = ""
params.each { |k,v| query += "#{k}=#{v}&" }
request_url << "?" + query
end
begin
response = Client.get(request_url, {:basic_auth => auth})
rescue SocketError => se
raise StandardError, se.message
end
if 200 <= response.code && response.code < 300
envelope = SingleItemEnvelope.new(response)
if envelope.item
item = envelope.item
end
elsif 400 <= response.code && response.code < 500
if response["message"]
envelope = ErrorEnvelope.new(response)
raise StandardError, envelope.message
else
raise StandardError, response["error"]
end
else
raise StandardError, "Server Response: #{response.code}"
end
item
end
# Requests a collections of items from the Help Scout API. Should return
# either an array of items from the CollectionsEnvelope, or raise an error
# with an ErrorEnvelope.
#
# Collections return a maximum of 50 records per page.
#
# url String A string representing the url for the REST endpoint to be
# queried.
# params Hash A hash of GET parameters to use for this particular
# request.
#
# Response
# Name Type Notes
# Header Status Int 200
# Body page Int Current page that was passed in on the request
# pages Int Total number of pages available
# count Int Total number of objects available
# items Array Collection of objects
def self.request_items(auth, url, params = {})
begin
response = Client.get(url, {:basic_auth => auth, :query => params})
rescue SocketError => se
raise StandardError, se.message
end
if 200 <= response.code && response.code < 300
envelope = CollectionsEnvelope.new(response)
elsif 400 <= response.code && response.code < 500
if response["message"]
envelope = ErrorEnvelope.new(response)
raise StandardError, envelope.message
else
raise StandardError, response["error"]
end
else
raise StandardError, "Server Response: #{response.code}"
end
envelope.items || []
end
# Loads paginated list of records
# It starts to load records from page 1 and proceed until number of loaded records will exceed limit or
# all records will be fetched
# auth - authentication info
# url - endpoint to load records from
# klass - type of expected result records
# params - additional get params (will be appended to url). This method will override any page value if provided
# limit - maximal number of records to fetch (set to negative value or 0 to retrieve all)
def self.load_paginated_items_list(auth, url, klass, params, limit)
page = 1
options = params.clone # to avoid argument modification
sz = 0
result = []
begin
options[:page] = page
items = Client.request_items(auth, url, options)
items.each do |item|
result << klass.new(item)
sz += 1
break if limit > 0 && sz == limit
end
page += 1
end while items && items.count > 0 && (limit <= 0 || sz < limit)
result
end
# Requests a collections of items from the Help Scout API. Should return
# the total count for this collection, or raise an error with an
# ErrorEnvelope.
#
# url String A string representing the url for the REST endpoint to be
# queried.
# params Hash A hash of GET parameters to use for this particular
# request.
#
# Response
# Name Type Notes
# Header Status Int 200
# Body page Int Current page that was passed in on the request
# pages Int Total number of pages available
# count Int Total number of objects available
# items Array Collection of objects
def self.request_count(auth, url, params = {})
request_url = ""
request_url << url
if params
query = ""
params.each { |k,v| query += "#{k}=#{v}&" }
request_url << "?" + query
end
begin
response = Client.get(request_url, {:basic_auth => auth})
rescue SocketError => se
raise StandardError, se.message
end
if 200 <= response.code && response.code < 300
envelope = CollectionsEnvelope.new(response)
envelope.count
elsif 400 <= response.code && response.code < 500
if response["message"]
envelope = ErrorEnvelope.new(response)
raise StandardError, envelope.message
else
raise StandardError, response["error"]
end
else
raise StandardError, "Server Response: #{response.code}"
end
end
# Sends a POST request to create a single item from the Help Scout API.
#
# url String A string representing the url to POST.
# params Hash A hash of POST parameters to use for this particular
# request.
#
# Response
# Name Type Notes
# Location String https://api.helpscout.net/v1/conversations/{id}.json
def self.create_item(auth, url, params = {})
begin
response = Client.post(url, {:basic_auth => auth, :headers => { 'Content-Type' => 'application/json' }, :body => params })
rescue SocketError => se
raise StandardError, se.message
end
if response.code == 201
if response["item"]
response["item"]
else
response["Location"]
end
else
raise StandardError.new("Server Response: #{response.code} #{response.message}")
end
end
# HelpScout::Client.new
#
# Initializes the Help Scout Client. Once called, you may use any of the
# HelpScout::Client methods to query the Help Scout API.
#
# key String Help Scout API Key. Optional. If not passed, the key will be
# loaded from @@settings, which defaults to helpscout.yml.
def initialize(key=nil)
Client.settings
if key.nil?
key = @@settings["api_key"]
end
# The Help Scout API uses Basic Auth, where username is your API Key.
# Password can be any arbitrary non-zero-length string.
@auth = { :username => key, :password => "X" }
end
# Get User
# http://developer.helpscout.net/users/
#
# Fetches a single user by id.
#
# userId Int id of the User being requested
#
# Request
# REST Method: GET
# URL: https://api.helpscout.net/v1/conversations/{conversationId}.json
#
# GET Parameters
# Name Type
# conversationId Int id of the Conversation being requested
#
# Response
# Name Type
# item User
def user(userId)
url = "/users/#{userId}.json"
item = Client.request_item(@auth, url, nil)
user = nil
if item
user = User.new(item)
end
user
end
# List Users
# http://developer.helpscout.net/users/
#
# Fetches all users
#
# Request
# REST Method: GET
# URL: https://api.helpscout.net/v1/users.json
#
# Parameters:
# Name Type Required Default Notes
# page Int No 1
#
# Response
# Name Type
# items Array Collection of User objects
def users
url = "/users.json"
items = Client.request_items(@auth, url, :page => 1)
users = []
items.each do |item|
users << User.new(item)
end
users
end
# List Users by Mailbox
# http://developer.helpscout.net/users/
#
# Fetches all users in a single mailbox
#
# mailboxId Int id of the Mailbox being requested
#
# Request
# REST Method: GET
# URL: https://api.helpscout.net/v1/mailboxes/{id}/users.json
#
# Parameters:
# Name Type Required Default Notes
# page Int No 1
#
# Response
# Name Type
# items Array Collection of User objects
def users_in_mailbox(mailboxId)
url ="/mailboxes/#{mailboxId}/users.json"
items = Client.request_items(@auth, url, :page => 1)
users = []
items.each do |item|
users << User.new(item)
end
users
end
# List Mailboxes
# http://developer.helpscout.net/mailboxes/
#
# Fetches all mailboxes
#
# Request
# REST Method: GET
# URL: https://api.helpscout.net/v1/mailboxes.json
#
# Parameters:
# Name Type Required Default Notes
# page Int No 1
#
# Response
# Name Type
# items Array Collection of Mailbox objects
def mailboxes
url = "/mailboxes.json"
mailboxes = []
begin
items = Client.request_items(@auth, url, {})
items.each do |item|
mailboxes << Mailbox.new(item)
end
rescue StandardError => e
puts "List Mailbox Request failed: #{e.message}"
end
mailboxes
end
# Get Mailbox
# http://developer.helpscout.net/mailboxes/
#
# Fetches a single Mailbox
#
# mailboxId Int id of the Mailbox being requested
#
# Request
# REST Method: GET
# URL: https://api.helpscout.net/v1/mailboxes/{id}.json
#
# Response
# Name Type
# item Mailbox
def mailbox(mailboxId)
url = "/mailboxes/#{mailboxId}.json"
item = Client.request_item(@auth, url, nil)
mailbox = nil
if item
mailbox = Mailbox.new(item)
end
mailbox
end
# Get Folders
# http://developer.helpscout.net/mailboxes/
#
# Fetches all Folders in a given mailbox
#
# mailboxId Int id of the Mailbox being requested
#
# Request
# REST Method: GET
# URL: https://api.helpscout.net/v1/mailboxes/{id}/folders.json
#
# Parameters:
# Name Type Required Default Notes
# page Int No 1
#
# Response
# Name Type
# items Array Collection of Mailbox objects
def folders_in_mailbox(mailboxId)
url = "/mailboxes/#{mailboxId}/folders.json"
items = Client.request_items(@auth, url, :page => 1)
folders = []
items.each do |item|
folders << Folder.new(item)
end
folders
end
# Get Conversation
# http://developer.helpscout.net/conversations/get/
#
# Fetches a single Conversation
#
# conversationId Int id of the Conversation being requested
#
# Request
# REST Method: GET
# URL: https://api.helpscout.net/v1/conversations/{id}.json
#
# Response
# Name Type
# item Conversation
def conversation(conversationId)
url = "/conversations/#{conversationId}.json"
begin
item = Client.request_item(@auth, url, nil)
conversation = nil
if item
conversation = Conversation.new(item)
end
rescue StandardError => e
puts "Could not fetch conversation with id #{conversationId}: #{e.message}"
end
end
# Create Conversation
# http://developer.helpscout.net/conversations/create/
#
# Creates a new Conversation.
#
# Request
# REST Method: POST
# URL: https://api.helpscout.net/v1/conversations.json
#
# POST Parameters
# Name Type Required Notes
# conversation Conversation Yes
# import boolean No The import parameter enables
# conversations to be created for
# historical purposes (i.e. if moving
# from a different platform, you can
# import your history). When import
# is set to true, no outgoing emails
# or notifications will be generated.
# reload boolean No Set this parameter to 'true' to
# return the created conversation in
# the response.
#
def create_conversation(conversation)
if !conversation
raise StandardError.new("Missing Conversation")
end
url = "/conversations.json"
begin
response = Client.create_item(@auth, url, conversation.to_json)
rescue StandardError => e
puts "Could not create conversation: #{e.message}"
end
end
# List Conversations
# http://developer.helpscout.net/conversations/list/
#
# Fetches conversations in a mailbox with a given status
#
# mailboxId Int id of the Mailbox being requested
# status String Filter by conversation status
# limit Int This function will page through
# CollectionsEnvelopes until all items are
# returned, unless a limit is specified.
# modifiedSince DateTime Returns conversations that have been modified
# since the given date/time.
#
# Possible values for status include:
# * CONVERSATION_FILTER_STATUS_ALL (Default)
# * CONVERSATION_FILTER_STATUS_ACTIVE
# * CONVERSATION_FILTER_STATUS_PENDING
#
# Request
# REST Method: GET
# URL: https://api.helpscout.net/v1/mailboxes/{mailboxId}/conversations.json
#
# Parameters:
# Name Type Required Default Notes
# page Int No 1
# status String No all Active/Pending only applies
# to the following folders:
# Unassigned
# My Tickets
# Drafts
# Assigned
# modifiedSince DateTime No Returns conversations that
# have been modified since the
# given date/time.
#
# Response
# Name Type
# items Array Collection of Conversation objects. Conversation threads
# are not returned on this call. To get the conversation
# threads, you need to retrieve the full conversation object
# via the Get Conversation call.
CONVERSATION_FILTER_STATUS_ACTIVE = "active"
CONVERSATION_FILTER_STATUS_ALL = "all"
CONVERSATION_FILTER_STATUS_PENDING = "pending"
def conversations(mailboxId, status, limit=0, modifiedSince)
url = "/mailboxes/#{mailboxId}/conversations.json"
page = 1
options = {}
if limit < 0
limit = 0
end
if status && (status == CONVERSATION_FILTER_STATUS_ACTIVE || status == CONVERSATION_FILTER_STATUS_ALL || status == CONVERSATION_FILTER_STATUS_PENDING)
options["status"] = status
end
if modifiedSince
options["modifiedSince"] = modifiedSince
end
conversations = []
begin
options["page"] = page
items = Client.request_items(@auth, url, options)
items.each do |item|
conversations << Conversation.new(item)
end
page = page + 1
rescue StandardError => e
puts "List Conversations Request failed: #{e.message}"
end while items && items.count > 0 && (limit == 0 || conversations.count < limit)
if limit > 0 && conversations.count > limit
conversations = conversations[0..limit-1]
end
conversations
end
# List Conversations in Folder
# http://developer.helpscout.net/conversations/
#
# Return conversations in a specific folder of a mailbox.
#
# mailboxId Int id of the Mailbox being requested
# folderId Int id of the Folder being requested
# status String Filter by conversation status
# limit Int This function will page through
# CollectionsEnvelopes until all items are
# returned, unless a limit is specified.
# modifiedSince DateTime Returns conversations that have been modified
# since the given date/time.
#
# Possible values for status include:
# * CONVERSATION_FILTER_STATUS_ALL (Default)
# * CONVERSATION_FILTER_STATUS_ACTIVE
# * CONVERSATION_FILTER_STATUS_PENDING
#
# Request
# REST Method: GET
# URL: https://api.helpscout.net/v1/mailboxes/{mailboxId}/folders/{folderId}/conversations.json
#
# Parameters:
# Name Type Required Default Notes
# page Int No 1
# status String No all Active/Pending only applies
# to the following folders:
# Unassigned
# My Tickets
# Drafts
# Assigned
# modifiedSince DateTime No Returns conversations that
# have been modified since the
# given date/time.
#
# Response
# Name Type
# items Array Collection of Conversation objects. Conversation threads
# are not returned on this call. To get the conversation
# threads, you need to retrieve the full conversation object
# via the Get Conversation call.
def conversations_in_folder(mailboxId, folderId, status, limit=0, modifiedSince)
url = "/mailboxes/#{mailboxId}/folders/#{folderId}/conversations.json"
page = 1
options = {}
if limit < 0
limit = 0
end
if status && (status == CONVERSATION_FILTER_STATUS_ACTIVE || status == CONVERSATION_FILTER_STATUS_ALL || status == CONVERSATION_FILTER_STATUS_PENDING)
options["status"] = status
end
if modifiedSince
options["modifiedSince"] = modifiedSince
end
conversations = []
begin
options["page"] = page
items = Client.request_items(@auth, url, options)
items.each do |item|
conversations << Conversation.new(item)
end
page = page + 1
rescue StandardError => e
puts "List Conversations In Folder Request failed: #{e.message}"
end while items && items.count > 0 && (limit == 0 || conversations.count < limit)
if limit > 0 && conversations.count > limit
conversations = conversations[0..limit-1]
end
conversations
end
# Conversation Count
# http://developer.helpscout.net/conversations/
#
# Returns a count for conversations in a mailbox with a given status
#
# mailboxId Int id of the Mailbox being requested
# status String Filter by conversation status
# modifiedSince DateTime id of the Mailbox being requested
#
# Possible values for status include:
# * CONVERSATION_FILTER_STATUS_ALL (Default)
# * CONVERSATION_FILTER_STATUS_ACTIVE
# * CONVERSATION_FILTER_STATUS_PENDING
#
# Request
# REST Method: GET
# URL: https://api.helpscout.net/v1/mailboxes/{mailboxId}/conversations.json
#
# Parameters:
# Name Type Required Default Notes
# page Int No 1
# status String No all Active/Pending only applies
# to the following folders:
# Unassigned
# My Tickets
# Drafts
# Assigned
# modifiedSince DateTime No Returns conversations that
# have been modified since the
# given date/time.
#
# Response
# Name Type
# count Integer Count of Conversation objects.
def conversation_count(mailboxId, status, modifiedSince)
url = "/mailboxes/#{mailboxId}/conversations.json"
page = 1
options = {}
if status && (status == CONVERSATION_FILTER_STATUS_ACTIVE || status == CONVERSATION_FILTER_STATUS_ALL || status == CONVERSATION_FILTER_STATUS_PENDING)
options["status"] = status
end
if modifiedSince
options["modifiedSince"] = modifiedSince
end
conversations = []
begin
options["page"] = page
count = Client.request_count(@auth, url, options)
rescue StandardError => e
puts "Conversation Count Request failed: #{e.message}"
end
end
# Get Attachment Data
# http://developer.helpscout.net/conversations/
#
# Fetches the AttachmentData from a given Attachment
#
# attachmentId Int id of the Attachment being requested
#
# Request
# REST Method: GET
# URL: https://api.helpscout.net/v1/attachments/{id}/data.json
#
# Response
# Name Type
# item Conversation::AttachmentData
def attachment_data(attachmentId)
url = "/attachments/#{attachmentId}/data.json"
item = Client.request_item(@auth, url, nil)
attachmentData = nil
if item
attachmentData = Conversation::AttachmentData.new(item)
end
attachmentData
end
# Get Customer
# http://developer.helpscout.net/customers/
#
# Fetches a single Customer
#
# customerId Int id of the Customer being requested
#
# Request
# REST Method: GET
# URL: https://api.helpscout.net/v1/customers/{id}.json
#
# Response
# Name Type
# item Customer
def customer(customerId)
url = "/customers/#{customerId}.json"
item = Client.request_item(@auth, url, nil)
customer = nil
if item
customer = Customer.new(item)
end
customer
end
# List Customers
# http://developer.helpscout.net/help-desk-api/customers/list/
#
# Customers can be filtered on any combination of first name, last name, and
# email.
#
# Customers are returned by createdAt date, from newest to oldest.
#
# Request
# REST Method: GET
# URL: https://api.helpscout.net/v1/customers.json
#
# Parameters:
# Name Type Required Default Notes
# Name Type Required Default
# firstName String No
# lastName String No
# email String No
# page Int No 1
#
# Response
# Name Type
# items Array Collection of Customer objects.
def customers(limit=0, firstName=nil, lastName=nil, email=nil)
options = {}
if firstName
options[:firstName] = firstName
end
if lastName
options[:lastName] = lastName
end
if email
options[:email] = email
end
Client.load_paginated_items_list(@auth, '/customers.json', Customer, options, limit)
end
#
# List Customers by Mailbox
# http://developer.helpscout.net/help-desk-api/customers/list-mailbox/
#
# Returns a list of Customers with conversations associated with the specified mailbox.
# At this time all customers are returned by createdAt date (from newest to oldest).
def customers_by_mailbox(limit=0, mailboxId)
Client.load_paginated_items_list(@auth, "/mailboxes/#{mailboxId}/customers.json", Customer, {}, limit)
end
# Helper method to find customers by email
def customers_by_email(email)
customers(0, nil, nil, email)
end
# Create Customer
# http://developer.helpscout.net/customers/create/
#
# Creates a new Customer.
#
# Request
# REST Method: POST
# URL: https://api.helpscout.net/v1/customers.json
#
# POST Parameters
# Name Type Required Notes
# customer Customer Yes The body of the request
# reload boolean No Set to true to return the customer in the
# response.
# Response
# Response Name Type Notes
# Header Status Integer 201
# Header Location String https://api.helpscout.net/v1/customer/{id}.json
def create_customer(customer)
if !customer
raise StandardError.new("Missing Customer")
end
url = "/customers.json"
begin
# We need to set reload flag to true to receive created object back
customer[:reload] = true
item = Client.create_item(@auth, url, customer.to_json)
Customer.new(item)
rescue StandardError => e
puts "Could not create customer: #{e.message}"
false
end
end
def ratings(start_time, end_time, rating)
url = "/reports/happiness/ratings.json"
page = 1
options = {}
options["start"] = start_time
options["end"] = end_time
options["rating"] = rating
ratings = []
begin
options["page"] = page
items = Client.request_items(@auth, url, options)
items.each do |item|
ratings << Rating.new(item)
end
page = page + 1
rescue StandardError => e
puts "Request failed: #{e.message}"
end while items && items.count > 0
ratings
end
end
end