pamfaxr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,799 @@
1
+ class PamFaxr
2
+ class FaxJob
3
+ require 'awesome_print'
4
+ ##
5
+ # Instantiates the FaxJob class
6
+ #
7
+ # @param [Hash] params the options for instantiating a FaxJob class
8
+ # @option params [String] :api_credentials the credentials for accessing the PamFax API
9
+ # @option params [Object] :http an SSL enabled net/http object
10
+ #
11
+ # @return [Object] the instantiated FaxJob class
12
+ def initialize(options={})
13
+ @base_resource = '/FaxJob'
14
+ @api_credentials = options[:api_credentials]
15
+ @http = options[:http]
16
+ end
17
+
18
+ ##
19
+ # Creates a new fax job
20
+ #
21
+ # @return [Hash] the result of the fax job creation request
22
+ #
23
+ # @example
24
+ # pamfaxr.create_fax_job
25
+ #
26
+ # returns:
27
+ #
28
+ # {
29
+ # "result" => {
30
+ # "code" => "success",
31
+ # "count" => 1,
32
+ # "message" => ""
33
+ # },
34
+ # "FaxContainer" => {
35
+ # "price" => 0,
36
+ # "send_mail" => 0,
37
+ # "uuid" => "ebKVV4XGwx99Wu",
38
+ # "error_notification" => 0,
39
+ # "group_notification" => 0,
40
+ # "send_sms" => 0,
41
+ # "currency_rate" => 1,
42
+ # "cover_page_length" => nil,
43
+ # "send_chat" => 0,
44
+ # "pages" => nil,
45
+ # "cover_id" => 0,
46
+ # "currency" => true,
47
+ # "affiliatepartner" => nil,
48
+ # "updated" => nil,
49
+ # "cover_text" => nil,
50
+ # "processing_started" => nil,
51
+ # "created" => "2011-01-17 19:27:57",
52
+ # "state" => "editing"
53
+ # }
54
+ # }
55
+ def create_fax_job
56
+ resource = @base_resource + "/Create" + @api_credentials + "&user_ip=#{Socket.gethostname}&user_agent=pamfax.rb&origin=script"
57
+ get(resource)
58
+ end
59
+
60
+ ##
61
+ # Adds a recipient to a fax job
62
+ #
63
+ # @param [String] the phone number to add to the fax job
64
+ #
65
+ # @return [Hash] the result of the add recipient request
66
+ #
67
+ # @example adding a recipient
68
+ # pamfaxr.add_recipient('14155551212')
69
+ #
70
+ # returns:
71
+ #
72
+ # {
73
+ # "result" => {
74
+ # "code" => "success",
75
+ # "count" => 1,
76
+ # "message" => ""
77
+ # },
78
+ # "FaxRecipient" => {
79
+ # "price_per_page" => 0.09,
80
+ # "name" => "",
81
+ # "number" => "+14155551212",
82
+ # "price_source" => "PNR",
83
+ # "country" => "US",
84
+ # "area_code" => "415",
85
+ # "zone" => "1",
86
+ # "number_type" => "FIXED",
87
+ # "description" => "California (CA)"
88
+ # }
89
+ # }
90
+ def add_recipient(number)
91
+ resource = @base_resource + "/AddRecipient" + @api_credentials + "&number=#{number}"
92
+ get(resource)
93
+ end
94
+
95
+ ##
96
+ # Adds a local file to the fax job
97
+ #
98
+ # @param [string, Required] filename to add to the fax including full path
99
+ #
100
+ # @return [Hash] the result of the request to add a file
101
+ #
102
+ # @example adding a local file
103
+ # pamfaxr.add_file('/path_to_my_file/filename.pdf')
104
+ #
105
+ # returns:
106
+ #
107
+ # {
108
+ # "result" => {
109
+ # "code" => "success",
110
+ # "count" => 1,
111
+ # "message" => ""
112
+ # },
113
+ # "FaxContainerFile" => {
114
+ # "name" => "/path_to_my_file/filename.pdf",
115
+ # "file_order" => 1,
116
+ # "ext" => "pdf",
117
+ # "file_uuid" => "JNMi19AeQ6QkoC",
118
+ # "mime" => "application/pdf",
119
+ # "state" => ""
120
+ # }
121
+ # }
122
+ def add_file(filename)
123
+ data, headers = Multipart::Post.prepare_query({ 'filename' => filename, 'file' => File.open(File.join(filename)) })
124
+ resource = @base_resource + "/AddFile" + @api_credentials
125
+ post(resource, data, headers)
126
+ end
127
+
128
+ ##
129
+ # Adds a remote file to the fax job
130
+ #
131
+ # @param [required, String] url of the file to send with the fax, http basic auth is optional
132
+ #
133
+ # @return [Hash] the result of the request to add a file
134
+ #
135
+ # @example adding a remote file
136
+ # pamfaxr.add_remote_file('https://s3.amazonaws.com/pamfax-test/R-intro.pdf')
137
+ #
138
+ # returns:
139
+ #
140
+ # {
141
+ # "result" => {
142
+ # "code" => "success",
143
+ # "count" => 1,
144
+ # "message" => ""
145
+ # },
146
+ # "FaxContainerFile" => {
147
+ # "name" => "R-intro.pdf",
148
+ # "file_order" => 0,
149
+ # "ext" => "pdf",
150
+ # "file_uuid" => "CwPx31xjl9k7Cp",
151
+ # "mime" => "application/pdf",
152
+ # "state" => ""
153
+ # }
154
+ # }
155
+ def add_remote_file(url)
156
+ resource = @base_resource + "/AddRemoteFile" + @api_credentials + "&url=#{url}"
157
+ get(resource)
158
+ end
159
+
160
+ ##
161
+ # Cancel an outstanding fax
162
+ #
163
+ # @param [required, String] uuid of the fax to cancel
164
+ #
165
+ # @return [Hash] the result of the request to cancel an outstanding fax
166
+ #
167
+ # @example cancel a fax
168
+ # pamfaxr.cancel('ebKVV4XGwx99Wu')
169
+ #
170
+ # returns:
171
+ #
172
+ # {
173
+ # "result" => {
174
+ # "code" => "success",
175
+ # "count" => 0,
176
+ # "message" => ""
177
+ # }
178
+ # }
179
+ def cancel(uuid)
180
+ resource = @base_resource + "/Cancel" + @api_credentials + "&uuid=#{uuid}"
181
+ get(resource)
182
+ end
183
+
184
+ ##
185
+ # Clone a fax job
186
+ #
187
+ # @param [required, String] uuid of the fax to clone
188
+ #
189
+ # @return [Hash] the result of the request to clone an outstanding fax
190
+ #
191
+ # @example cloning a sent fax
192
+ # pamfaxr.clone_fax('ebKVV4XGwx99Wu')
193
+ #
194
+ # returns:
195
+ #
196
+ # {
197
+ # "result" => {
198
+ # "code" => "fax_not_found",
199
+ # "count" => 0,
200
+ # "message" => "Fax not found"
201
+ # }
202
+ # }
203
+ def clone_fax(uuid)
204
+ resource = @base_resource + "/CloneFax" + @api_credentials + "&user_ip=#{Socket.gethostname}&user_agent=pamfax.rb&uuid=#{uuid}"
205
+ get(resource)
206
+ end
207
+
208
+ ##
209
+ # Returns the available cover templates
210
+ #
211
+ # @return [Array] an array of hashes of the available covers
212
+ #
213
+ # @example list available fax covers
214
+ # pamfaxr.list_available_covers
215
+ #
216
+ # returns:
217
+ #
218
+ # {
219
+ # "result" => {
220
+ # "code" => "success",
221
+ # "count" => 1,
222
+ # "message" => ""
223
+ # },
224
+ # "Covers" => {
225
+ # "type" => "list",
226
+ # "content" => [
227
+ # [0] {
228
+ # "template_id" => "eae27d77ca20db309e056e3d2dcd7d69",
229
+ # "title" => "Basic",
230
+ # "creator" => 0,
231
+ # "id" => 1,
232
+ # "thumb_id" => "091d584fced301b442654dd8c23b3fc9",
233
+ # "description" => nil,
234
+ # "preview_id" => "7eabe3a1649ffa2b3ff8c02ebfd5659f"
235
+ # },
236
+ # [1] {
237
+ # "template_id" => "ca46c1b9512a7a8315fa3c5a946e8265",
238
+ # "title" => "Flowers",
239
+ # "creator" => 0,
240
+ # "id" => 3,
241
+ # "thumb_id" => "45fbc6d3e05ebd93369ce542e8f2322d",
242
+ # "description" => nil,
243
+ # "preview_id" => "979d472a84804b9f647bc185a877a8b5"
244
+ # },
245
+ # [2] {
246
+ # "template_id" => "e96ed478dab8595a7dbda4cbcbee168f",
247
+ # "title" => "Message",
248
+ # "creator" => 0,
249
+ # "id" => 4,
250
+ # "thumb_id" => "ec8ce6abb3e952a85b8551ba726a1227",
251
+ # "description" => nil,
252
+ # "preview_id" => "63dc7ed1010d3c3b8269faf0ba7491d4"
253
+ # },
254
+ # [3] {
255
+ # "template_id" => "f340f1b1f65b6df5b5e3f94d95b11daf",
256
+ # "title" => "Simple",
257
+ # "creator" => 0,
258
+ # "id" => 5,
259
+ # "thumb_id" => "335f5352088d7d9bf74191e006d8e24c",
260
+ # "description" => nil,
261
+ # "preview_id" => "cb70ab375662576bd1ac5aaf16b3fca4"
262
+ # }
263
+ # ]
264
+ # }
265
+ # }
266
+ def list_available_covers
267
+ resource = @base_resource + "/ListAvailableCovers" + @api_credentials
268
+ get(resource)
269
+ end
270
+
271
+ ##
272
+ # Returns the files associated to the current faxjob
273
+ #
274
+ # @return [Array] an array of hashes of the associated files
275
+ #
276
+ # @example listing fax files associated to a job
277
+ # pamfaxr.list_fax_files
278
+ #
279
+ # returns:
280
+ #
281
+ # {
282
+ # "result" => {
283
+ # "code" => "success",
284
+ # "count" => 1,
285
+ # "message" => ""
286
+ # },
287
+ # "Files" => {
288
+ # "type" => "list",
289
+ # "content" => [
290
+ # [0] {
291
+ # "name" => "R-intro.pdf",
292
+ # "size" => 646768,
293
+ # "extension" => "pdf",
294
+ # "uuid" => "CwPx31xjl9k7Cp",
295
+ # "pages" => 101,
296
+ # "contentmd5" => "fd898b168b9780212a2ddd5bfbd79d65",
297
+ # "mimetype" => "application/pdf",
298
+ # "created" => "2011-01-17 19:28:05"
299
+ # }
300
+ # ]
301
+ # }
302
+ # }
303
+ def list_fax_files
304
+ resource = @base_resource + "/ListFaxFiles" + @api_credentials
305
+ get(resource)
306
+ end
307
+
308
+ ##
309
+ # Returns the recipients associated to the current faxjob
310
+ #
311
+ # @return [Array] an array of hashes of recipients
312
+ #
313
+ # @example listing recipients on a fax job
314
+ # pamfaxr.list_recipients
315
+ #
316
+ # returns:
317
+ #
318
+ # {
319
+ # "result" => {
320
+ # "code" => "success",
321
+ # "count" => 1,
322
+ # "message" => ""
323
+ # },
324
+ # "Recipients" => {
325
+ # "type" => "list",
326
+ # "content" => [
327
+ # {
328
+ # "price" => 0,
329
+ # "duration" => 0,
330
+ # "name" => "",
331
+ # "delivery_started" => nil,
332
+ # "price_per_page" => 0.09,
333
+ # "number" => "+13035551212",
334
+ # "price_source" => "PNR",
335
+ # "country" => "US",
336
+ # "sent" => nil,
337
+ # "completed" => nil,
338
+ # "area_code" => "303",
339
+ # "formatted_number" => "+1 303 5551212",
340
+ # "zone" => "1",
341
+ # "uuid" => "ExozS0NJe7CErm",
342
+ # "currency_rate" => "1",
343
+ # "pages" => nil,
344
+ # "status_code" => 0,
345
+ # "number_type" => "FIXED",
346
+ # "transmission_report" => "",
347
+ # "currency" => "1",
348
+ # "description" => "Colorado (CO)",
349
+ # "updated" => "2011-01-17 19:28:16",
350
+ # "status_message" => nil,
351
+ # "created" => "2011-01-17 19:28:16",
352
+ # "state" => "unknown"
353
+ # },
354
+ # {
355
+ # "price" => 0,
356
+ # "duration" => 0,
357
+ # "name" => "",
358
+ # "delivery_started" => nil,
359
+ # "price_per_page" => 0.09,
360
+ # "number" => "+14155551212",
361
+ # "price_source" => "PNR",
362
+ # "country" => "US",
363
+ # "sent" => nil,
364
+ # "completed" => nil,
365
+ # "area_code" => "415",
366
+ # "formatted_number" => "+1 415 5551212",
367
+ # "zone" => "1",
368
+ # "uuid" => "yoAT88QXAda80g",
369
+ # "currency_rate" => "1",
370
+ # "pages" => nil,
371
+ # "status_code" => 0,
372
+ # "number_type" => "FIXED",
373
+ # "transmission_report" => "",
374
+ # "currency" => "1",
375
+ # "description" => "California (CA)",
376
+ # "updated" => "2011-01-17 19:28:14",
377
+ # "status_message" => nil,
378
+ # "created" => "2011-01-17 19:28:14",
379
+ # "state" => "unknown"
380
+ # }
381
+ # ]
382
+ # }
383
+ # }
384
+ def list_recipients
385
+ resource = @base_resource + "/ListRecipients" + @api_credentials
386
+ get(resource)
387
+ end
388
+
389
+ ##
390
+ # Sets the current fax cover sheet
391
+ #
392
+ # @param [String] the template ID to use
393
+ # @param [String] the text to use in the template
394
+ #
395
+ # @return [Hash] the result of the set fax cover request
396
+ #
397
+ # @example set the cover for the fax
398
+ # pamfaxr.set_cover('3', 'Foobar is here!')
399
+ #
400
+ # returns:
401
+ #
402
+ # {
403
+ # "result" => {
404
+ # "code" => "success",
405
+ # "count" => 0,
406
+ # "message" => ""
407
+ # }
408
+ # }
409
+ def set_cover(template_id, text)
410
+ resource = URI.encode(@base_resource + "/SetCover" + @api_credentials + "&template_id=#{template_id}&text=#{text}")
411
+ get(resource)
412
+ end
413
+
414
+ ##
415
+ # Obtains the state of the FaxJob build, may block or return immediately
416
+ #
417
+ # @param [Hash] params the options for obtaining the fax state
418
+ # @option params [optional, Boolean] :blocking true if you want to block until the FaxJob is ready, otherwise it returns the current state
419
+ # @option params [optional, Integer] :interval the time to wait, in seconds, between each check of the status
420
+ #
421
+ # @return [Hash] the result of the status request
422
+ #
423
+ # @example get the state of the fax
424
+ # pamfaxr.get_state
425
+ #
426
+ # returns:
427
+ #
428
+ # {
429
+ # "result" => {
430
+ # "code" => "success",
431
+ # "count" => 2,
432
+ # "message" => ""
433
+ # },
434
+ # "converting" => false,
435
+ # "Files" => {
436
+ # "type" => "list",
437
+ # "content" => [
438
+ # [0] {
439
+ # "file_order" => 0,
440
+ # "state" => "converted"
441
+ # }
442
+ # ]
443
+ # },
444
+ # "FaxContainer" => {
445
+ # "price" => 18.36,
446
+ # "sms_cost" => 0,
447
+ # "send_mail" => "0",
448
+ # "uuid" => "ebKVV4XGwx99Wu",
449
+ # "error_notification" => "0",
450
+ # "group_notification" => "0",
451
+ # "send_sms" => "0",
452
+ # "currency_rate" => 1,
453
+ # "cover_page_length" => "1",
454
+ # "send_chat" => "0",
455
+ # "pages" => 102,
456
+ # "cover_id" => 3,
457
+ # "currency" => true,
458
+ # "affiliatepartner" => nil,
459
+ # "updated" => "2011-01-17 19:28:22",
460
+ # "cover_text" => "Foobar is here!",
461
+ # "processing_started" => nil,
462
+ # "created" => "2011-01-17 19:27:57",
463
+ # "state" => "ready_to_send"
464
+ # }
465
+ # }
466
+ def get_state(options={})
467
+ if options[:blocking]
468
+ state = nil
469
+ result = nil
470
+ while state == nil
471
+ result = fetch_state
472
+ sleep options[:interval]
473
+ end
474
+ result
475
+ else
476
+ fetch_state
477
+ end
478
+ end
479
+
480
+ ##
481
+ # Gets the preview of the pages
482
+ #
483
+ # @param [required, String] uuid of the fax to get the preview for
484
+ #
485
+ # @return [Hash] the result of the preview request
486
+ #
487
+ # @example get a preview of the fax pages
488
+ # pamfaxr.get_preview('ebKVV4XGwx99Wu')
489
+ #
490
+ # returns:
491
+ #
492
+ # {
493
+ # "Status" => {
494
+ # "open" => 5,
495
+ # "done" => 0
496
+ # },
497
+ # "result" => {
498
+ # "code" => "success",
499
+ # "count" => 2,
500
+ # "message" => ""
501
+ # },
502
+ # "PreviewPages" => {
503
+ # "type" => "list"
504
+ # }
505
+ # }
506
+ def get_preview(uuid)
507
+ resource = URI.encode(@base_resource + "/GetPreview" + @api_credentials + "&uuid=#{uuid}")
508
+ get(resource)
509
+ end
510
+
511
+ ##
512
+ # Remove all of the files associated to a fax
513
+ #
514
+ # @param [required, String] uuid of the fax to remove all the files for
515
+ #
516
+ # @return [Hash] the result of the remove request
517
+ #
518
+ # @example remove all files
519
+ # pamfaxr.remove_all_files
520
+ #
521
+ # returns:
522
+ #
523
+ # {
524
+ # "result" => {
525
+ # "code" => "success",
526
+ # "count" => 0,
527
+ # "message" => ""
528
+ # }
529
+ # }
530
+ def remove_all_files
531
+ resource = URI.encode(@base_resource + "/RemoveAllFiles" + @api_credentials)
532
+ get(resource)
533
+ end
534
+
535
+ ##
536
+ # Remove all of the recipients associated to a fax
537
+ #
538
+ # @param [required, String] uuid of the fax to remove all the recipients for
539
+ #
540
+ # @return [Hash] the result of the remove request
541
+ #
542
+ # @example remove all recipients
543
+ # pamfaxr.remove_all_recipients
544
+ #
545
+ # returns:
546
+ #
547
+ # {
548
+ # "result" => {
549
+ # "code" => "success",
550
+ # "count" => 0,
551
+ # "message" => ""
552
+ # }
553
+ # }
554
+ def remove_all_recipients
555
+ resource = URI.encode(@base_resource + "/RemoveAllRecipients" + @api_credentials)
556
+ get(resource)
557
+ end
558
+
559
+ ##
560
+ # Remove all the cover page for the associated fax
561
+ #
562
+ # @param [required, String] uuid of the fax to remove the cover page for
563
+ #
564
+ # @return [Hash] the result of the remove request
565
+ #
566
+ # @example remove cover
567
+ # pamfaxr.remove_cover
568
+ #
569
+ # returns:
570
+ #
571
+ # {
572
+ # "result" => {
573
+ # "code" => "success",
574
+ # "count" => 0,
575
+ # "message" => ""
576
+ # }
577
+ # }
578
+ def remove_cover
579
+ resource = URI.encode(@base_resource + "/RemoveCover" + @api_credentials)
580
+ get(resource)
581
+ end
582
+
583
+ ##
584
+ # Remove a particular file associated to a fax
585
+ #
586
+ # @param [required, String] uuid of the fax to remove the particular file for
587
+ #
588
+ # @return [Hash] the result of the remove request
589
+ #
590
+ # @example remove file
591
+ # pamfaxr.remove_file('JNMi19AeQ6QkoC')
592
+ #
593
+ # returns:
594
+ #
595
+ # {
596
+ # "result" => {
597
+ # "code" => "success",
598
+ # "count" => 0,
599
+ # "message" => ""
600
+ # }
601
+ # }
602
+ def remove_file(file_uuid)
603
+ resource = URI.encode(@base_resource + "/RemoveFile" + @api_credentials + "&file_uuid=#{file_uuid}")
604
+ get(resource)
605
+ end
606
+
607
+ ##
608
+ # Remove a particular recipient associated to a fax
609
+ #
610
+ # @param [required, String] uuid of the fax to remove a particular recipients for
611
+ #
612
+ # @return [Hash] the result of the remove request
613
+ #
614
+ # @example TBD
615
+ def remove_recipient(recipient_uuid)
616
+ resource = URI.encode(@base_resource + "/RemoveRecipient" + @api_credentials + "&uuid=#{uuid}")
617
+ get(resource)
618
+ end
619
+
620
+ ##
621
+ # Request to send the built fax
622
+ #
623
+ # @return [Hash] the result of the request to send the built fax
624
+ #
625
+ # @example send a fax
626
+ # pamfaxr.send_fax
627
+ #
628
+ # returns:
629
+ #
630
+ # {
631
+ # "result" => {
632
+ # "code" => "not_enough_credit",
633
+ # "count" => 0,
634
+ # "message" => "You don't have enough PamFax Credit. <a href=\\\"https://www.pamfax.biz/shop\\\">Buy PamFax Credit now</a>."
635
+ # }
636
+ # }
637
+ def send_fax
638
+ resource = @base_resource + "/Send" + @api_credentials
639
+ get(resource)
640
+ end
641
+
642
+ ##
643
+ # Request to send the built fax later
644
+ #
645
+ # @return [Hash] the result of the request to send the built fax later
646
+ #
647
+ # @example
648
+ # pamfaxr.send_fax_later
649
+ #
650
+ # returns:
651
+ #
652
+ # {
653
+ # "result" => {
654
+ # "code" => "success",
655
+ # "count" => 0,
656
+ # "message" => ""
657
+ # }
658
+ # }
659
+ def send_fax_later
660
+ resource = @base_resource + "/SendLater" + @api_credentials
661
+ get(resource)
662
+ end
663
+
664
+ ##
665
+ # Captures any unknown methods gracefully by throwing a Runtime Error
666
+ def method_missing(method, *args)
667
+ raise RuntimeError, "Unknown method #{method}"
668
+ end
669
+
670
+ private
671
+
672
+ ##
673
+ # Fetches the state of the fax job
674
+ #
675
+ # @return [Hash] the status of the current fax job
676
+ def fetch_state
677
+ resource = @base_resource + "/GetFaxState" + @api_credentials
678
+ fax_state = JSON.parse @http.get(resource).body
679
+ fax_state.merge!('converting' => converting?(fax_state))
680
+ fax_state
681
+ end
682
+
683
+ ##
684
+ # Returns whether or not a file in the fax job is still in a converting state
685
+ #
686
+ # @param [required, Hash] the hash returned by fetch_state
687
+ #
688
+ # @return [Boolean] true if a file is still in the converting process
689
+ def converting?(fax_state)
690
+ converting = false
691
+ fax_state['Files']['content'].each { |file| converting = true if file['state'] == '' || file['state'] == 'converting' }
692
+ converting
693
+ end
694
+
695
+ ##
696
+ # Gets the resource
697
+ #
698
+ # @param [required, String] resource to get
699
+ # @return [Hash] the result of the request
700
+ def get(resource)
701
+ begin
702
+ JSON.parse @http.get(resource, { 'Content-Type' => 'application/json' }).body
703
+ rescue => error
704
+ end
705
+ end
706
+
707
+ ##
708
+ # Posts to the resource
709
+ #
710
+ # @param [required, String] resource to post
711
+ # @param [requried, String] data of the body to post
712
+ # @param [required, Hash] headers to send with the post request
713
+ #
714
+ # @return [Hash] the result of the request
715
+ def post(resource, data, headers={})
716
+ begin
717
+ result, body = @http.post(resource, data, headers)
718
+ JSON.parse body
719
+ rescue => error
720
+ end
721
+ end
722
+ end
723
+
724
+ ##
725
+ # Creates an instance of the PamFax class
726
+ #
727
+ # @param [Hash] params the options for instantiating a PamFax class
728
+ # @option params [optionsl, String] :base_uri the URI of the PamFax API, it defaults to https://api.pamfax.biz
729
+ # @option params [String] :key the PamFax API key you have been assigned
730
+ # @option params [String] :secret the PamFax API secret you have been assigned
731
+ # @option params [String] :username the PamFax username you are going to use to send faxes
732
+ # @option params [String] :password the PamFax password you are going to use to send faxes
733
+ #
734
+ # @return [Object] the instantiated FaxJob class
735
+ #
736
+ # @example create a new PamFax object
737
+ # pamfaxr = PamFax.new({ :key => 'your_api_key', :secret => 'your_api_secret' })
738
+ def initialize(options={})
739
+ base_uri = options[:base_uri] || "https://api.pamfax.biz"
740
+
741
+ options.merge!({ :http => create_http(URI.parse(base_uri)),
742
+ :api_credentials => "?apikey=#{options[:key]}&apisecret=#{options[:secret]}&apioutputformat=API_FORMAT_JSON" })
743
+ options[:api_credentials] = options[:api_credentials] + "&usertoken=#{get_user_token(options)}"
744
+
745
+ @fax_job = FaxJob.new options
746
+ end
747
+
748
+ ##
749
+ # Captures the request for the fax job and sends them to the instantiated FaxJob object
750
+ def method_missing(method, *args)
751
+ @fax_job.send(method, *args)
752
+ end
753
+
754
+ private
755
+
756
+ ##
757
+ # Gets the user token to use with subsequent requests
758
+ #
759
+ # @param [Hash] params the options for getting the user token
760
+ # @option params [String] :api_credentials the PamFax credentials built with key/secret
761
+ # @option params [String] :username the PamFax username you are going to use to send faxes
762
+ # @option params [String] :password the PamFax password you are going to use to send faxes
763
+ #
764
+ # @return [Object] the instantiated FaxJob class
765
+ def get_user_token(options={})
766
+ result = verify_user(options)
767
+ if result['result']['code'] == 'success'
768
+ result['UserToken']['token']
769
+ else
770
+ raise RuntimeError, result['result']['message']
771
+ end
772
+ end
773
+
774
+ ##
775
+ # Calls the VerifyUser resource to fetch the verified user details
776
+ #
777
+ # @param [Hash] params the options for verifying the user
778
+ # @option params [String] :api_credentials the PamFax credentials built with key/secret
779
+ # @option params [String] :username the PamFax username you are going to use to send faxes
780
+ # @option params [String] :password the PamFax password you are going to use to send faxes
781
+ #
782
+ # @return [Object] the instantiated FaxJob class
783
+ def verify_user(options={})
784
+ resource = "/Session/VerifyUser/#{options[:api_credentials]}&username=#{options[:username]}&password=#{options[:password]}"
785
+ JSON.parse options[:http].get(resource).body
786
+ end
787
+
788
+ ##
789
+ # Creates an ssl enabled net/http object
790
+ #
791
+ # @param [String] the URL to use to connect to the service
792
+ #
793
+ # @return [Object] an instantiated net/http object that is ssl enabled
794
+ def create_http(base_uri)
795
+ http = Net::HTTP.new(base_uri.host, base_uri.port)
796
+ http.use_ssl = true
797
+ http
798
+ end
799
+ end