sailthru-client 1.09 → 1.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/sailthru.rb +96 -13
  2. metadata +20 -6
@@ -1,13 +1,14 @@
1
+ require 'rubygems'
1
2
  require 'net/http'
2
3
  require 'uri'
3
4
  require 'cgi'
4
- require 'rubygems'
5
5
  require 'json'
6
6
  require 'digest/md5'
7
+ require 'net/http/post/multipart'
7
8
 
8
9
  module Sailthru
9
10
 
10
- Version = VERSION = '1.09'
11
+ Version = VERSION = '1.10'
11
12
 
12
13
  class SailthruClientException < Exception
13
14
  end
@@ -605,6 +606,63 @@ module Sailthru
605
606
  def stats(data)
606
607
  api_get(:stats, data)
607
608
  end
609
+
610
+ # params
611
+ # job, String
612
+ # options, hash
613
+ # report_email, String
614
+ # postback_url, String
615
+ # binary_key, String
616
+ #
617
+ # interface for making request to job call
618
+ def process_job(job, options = {}, report_email = nil, postback_url = nil, binary_key = nil)
619
+ data = options
620
+ data['job'] = job
621
+ if !report_email.nil?
622
+ data['report_email'] = report_email
623
+ end
624
+
625
+ if !postback_url.nil?
626
+ data['postback_url'] = postback_url
627
+ end
628
+ api_post(:job, data, binary_key)
629
+ end
630
+
631
+ # implementation for import_job
632
+ def process_import_job(list, emails, report_email = nil, postback_url = nil)
633
+ data = {}
634
+ data['list'] = list
635
+ data['emails'] = emails
636
+ process_job(:import, data, report_email, postback_url)
637
+ end
638
+
639
+ # implementation for import job using file upload
640
+ def process_import_job_from_file(list, file_path, report_email = nil, postback_url = nil)
641
+ data = {}
642
+ data['list'] = list
643
+ data['file'] = file_path
644
+ process_job(:import, data, report_email, postback_url, 'file')
645
+ end
646
+
647
+ # implementation for snapshot job
648
+ def process_snapshot_job(query = {}, report_email = nil, postback_url = nil)
649
+ data = {}
650
+ data['query'] = query
651
+ process_job(:snapshot, data, report_email, postback_url)
652
+ end
653
+
654
+ # implementation for export list job
655
+ def process_export_list_job(list, report_email = nil, postback_url = nil)
656
+ data = {}
657
+ data['list'] = list
658
+ process_job(:export_list_data, data, report_email, postback_url)
659
+ end
660
+
661
+ # get status of a job
662
+ def get_job_status(job_id)
663
+ api_get(:job, {'job_id' => job_id})
664
+ end
665
+
608
666
 
609
667
  # Perform API GET request
610
668
  def api_get(action, data)
@@ -612,8 +670,8 @@ module Sailthru
612
670
  end
613
671
 
614
672
  # Perform API POST request
615
- def api_post(action, data)
616
- api_request(action, data, 'POST')
673
+ def api_post(action, data, binary_key = nil)
674
+ api_request(action, data, 'POST', binary_key)
617
675
  end
618
676
 
619
677
  #Perform API DELETE request
@@ -632,11 +690,22 @@ module Sailthru
632
690
  #
633
691
  # Perform an API request, using the shared-secret auth hash.
634
692
  #
635
- def api_request(action, data, request_type)
693
+ def api_request(action, data, request_type, binary_key = nil)
694
+
636
695
  data[:api_key] = @api_key
637
696
  data[:format] ||= 'json'
697
+
698
+ if (!binary_key.nil?)
699
+ binary_key_data = data[binary_key]
700
+ data.delete(binary_key)
701
+ end
702
+
638
703
  data[:sig] = get_signature_hash(data, @secret)
639
- _result = self.http_request("#{@api_uri}/#{action}", data, request_type)
704
+
705
+ if (!binary_key.nil?)
706
+ data[binary_key] = binary_key_data
707
+ end
708
+ _result = self.http_request("#{@api_uri}/#{action}", data, request_type, binary_key)
640
709
 
641
710
 
642
711
  # NOTE: don't do the unserialize here
@@ -658,20 +727,29 @@ module Sailthru
658
727
  # method, String "GET" or "POST"
659
728
  # returns:
660
729
  # String, body of response
661
- def http_request(uri, data, method = 'POST')
730
+ def http_request(uri, data, method = 'POST', binary_key = nil)
662
731
  data = flatten_nested_hash(data, false)
663
- if method == 'POST'
664
- post_data = data
665
- else
732
+
733
+ if method != 'POST'
666
734
  uri += "?" + data.map{ |key, value| "#{CGI::escape(key.to_s)}=#{CGI::escape(value.to_s)}" }.join("&")
667
735
  end
736
+
668
737
  req = nil
669
738
  headers = {"User-Agent" => "Sailthru API Ruby Client #{VERSION}"}
670
739
 
671
740
  _uri = URI.parse(uri)
741
+
672
742
  if method == 'POST'
673
- req = Net::HTTP::Post.new(_uri.path, headers)
674
- req.set_form_data(data)
743
+ if (!binary_key.nil?)
744
+ binary_data = data[binary_key]
745
+ data[binary_key] = UploadIO.new(File.open(binary_data), "text/plain")
746
+ #req = Net::HTTP::Post::Multipart.new(_uri.path, binary_key => UploadIO.new(File.open(binary_data), "text/plain"))
747
+ req = Net::HTTP::Post::Multipart.new(_uri.path, data)
748
+ else
749
+ req = Net::HTTP::Post.new(_uri.path, headers)
750
+ req.set_form_data(data)
751
+ end
752
+
675
753
  else
676
754
  request_uri = "#{_uri.path}?#{_uri.query}"
677
755
  if method == 'DELETE'
@@ -688,12 +766,17 @@ module Sailthru
688
766
  rescue Exception => e
689
767
  raise SailthruClientException.new("Unable to open stream: #{_uri.to_s}");
690
768
  end
691
-
769
+
692
770
  if response.body
693
771
  return response.body
694
772
  else
695
773
  raise SailthruClientException.new("No response received from stream: #{_uri.to_s}")
696
774
  end
697
775
  end
776
+
777
+ def http_multipart_request(uri, data)
778
+ req = Net::HTTP::Post::Multipart.new url.path,
779
+ "file" => UploadIO.new(data['file'], "application/octet-stream")
780
+ end
698
781
  end
699
782
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sailthru-client
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 9
9
- version: "1.09"
8
+ - 10
9
+ version: "1.10"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Prajwal Tuladhar
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-05-10 00:00:00 -04:00
17
+ date: 2011-05-19 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -32,7 +32,7 @@ dependencies:
32
32
  type: :runtime
33
33
  version_requirements: *id001
34
34
  - !ruby/object:Gem::Dependency
35
- name: fakeweb
35
+ name: multipart-post
36
36
  prerelease: false
37
37
  requirement: &id002 !ruby/object:Gem::Requirement
38
38
  none: false
@@ -43,8 +43,22 @@ dependencies:
43
43
  segments:
44
44
  - 0
45
45
  version: "0"
46
- type: :development
46
+ type: :runtime
47
47
  version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: fakeweb
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :development
61
+ version_requirements: *id003
48
62
  description:
49
63
  email: praj@sailthru.com
50
64
  executables: []