sailthru-client 1.10 → 1.11
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +26 -0
- data/lib/sailthru.rb +42 -20
- metadata +19 -7
data/README.md
CHANGED
@@ -195,3 +195,29 @@ Examples
|
|
195
195
|
email = 'praj@sailthru.com'
|
196
196
|
tags = ['red', 'blue']
|
197
197
|
response = sailthru.set_horizon(email, tags)
|
198
|
+
|
199
|
+
### [job] (http://docs.sailthru.com/api/job)
|
200
|
+
|
201
|
+
# get status of job id
|
202
|
+
job_id = '4dd58f036803fa3b5500000b'
|
203
|
+
response = sailthru.get_job_status(job_id)
|
204
|
+
|
205
|
+
# process import job for email string
|
206
|
+
list = 'test-list'
|
207
|
+
emails = 'a@a.com,b@b.com'
|
208
|
+
response = sailthru.process_import_job(list, emails)
|
209
|
+
|
210
|
+
# process import job from CSV or text file
|
211
|
+
list = 'test-list'
|
212
|
+
source_file = '/home/praj/Desktop/emails.txt'
|
213
|
+
response = sailthru.process_import_job(list, source_file)
|
214
|
+
|
215
|
+
# process snapshot job
|
216
|
+
query = {}
|
217
|
+
report_email = 'praj@sailthru.com'
|
218
|
+
postback_url = 'http://example.com/reports/snapshot_postback'
|
219
|
+
response = sailthru.process_snapshot_job(query)
|
220
|
+
|
221
|
+
# process export list job
|
222
|
+
list = 'test-list'
|
223
|
+
response = sailthru.process_export_list_job(list)
|
data/lib/sailthru.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'rubygems'
|
2
|
+
require 'net/https'
|
2
3
|
require 'net/http'
|
3
4
|
require 'uri'
|
4
5
|
require 'cgi'
|
@@ -8,7 +9,7 @@ require 'net/http/post/multipart'
|
|
8
9
|
|
9
10
|
module Sailthru
|
10
11
|
|
11
|
-
Version = VERSION = '1.
|
12
|
+
Version = VERSION = '1.11'
|
12
13
|
|
13
14
|
class SailthruClientException < Exception
|
14
15
|
end
|
@@ -104,16 +105,19 @@ module Sailthru
|
|
104
105
|
|
105
106
|
include Helpers
|
106
107
|
|
108
|
+
attr_accessor :verify_ssl
|
109
|
+
|
107
110
|
# params:
|
108
111
|
# api_key, String
|
109
112
|
# secret, String
|
110
113
|
# api_uri, String
|
111
114
|
#
|
112
115
|
# Instantiate a new client; constructor optionally takes overrides for key/secret/uri.
|
113
|
-
def initialize(api_key, secret, api_uri)
|
116
|
+
def initialize(api_key, secret, api_uri = nil)
|
114
117
|
@api_key = api_key
|
115
118
|
@secret = secret
|
116
|
-
@api_uri = api_uri
|
119
|
+
@api_uri = if api_uri.nil? then 'https://api.sailthru.com' else api_uri end
|
120
|
+
@verify_ssl = true
|
117
121
|
end
|
118
122
|
|
119
123
|
# params:
|
@@ -691,33 +695,34 @@ module Sailthru
|
|
691
695
|
# Perform an API request, using the shared-secret auth hash.
|
692
696
|
#
|
693
697
|
def api_request(action, data, request_type, binary_key = nil)
|
694
|
-
|
695
|
-
data[:api_key] = @api_key
|
696
|
-
data[:format] ||= 'json'
|
697
|
-
|
698
698
|
if (!binary_key.nil?)
|
699
699
|
binary_key_data = data[binary_key]
|
700
700
|
data.delete(binary_key)
|
701
701
|
end
|
702
|
-
|
703
|
-
data[:
|
702
|
+
|
703
|
+
if data[:format].nil? or data[:format] == 'json'
|
704
|
+
data = self.prepare_json_payload(data)
|
705
|
+
else
|
706
|
+
data[:api_key] = @api_key
|
707
|
+
data[:format] ||= 'json'
|
708
|
+
data[:sig] = get_signature_hash(data, @secret)
|
709
|
+
end
|
704
710
|
|
705
711
|
if (!binary_key.nil?)
|
706
712
|
data[binary_key] = binary_key_data
|
707
713
|
end
|
708
714
|
_result = self.http_request("#{@api_uri}/#{action}", data, request_type, binary_key)
|
709
715
|
|
710
|
-
|
711
716
|
# NOTE: don't do the unserialize here
|
712
|
-
|
717
|
+
if data[:format] == 'json'
|
713
718
|
begin
|
714
|
-
|
715
|
-
|
719
|
+
unserialized = JSON.parse(_result)
|
720
|
+
return unserialized ? unserialized : _result
|
716
721
|
rescue JSON::JSONError => e
|
717
|
-
|
722
|
+
return {'error' => e}
|
718
723
|
end
|
719
|
-
|
720
|
-
|
724
|
+
end
|
725
|
+
return _result
|
721
726
|
end
|
722
727
|
|
723
728
|
|
@@ -743,7 +748,6 @@ module Sailthru
|
|
743
748
|
if (!binary_key.nil?)
|
744
749
|
binary_data = data[binary_key]
|
745
750
|
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
751
|
req = Net::HTTP::Post::Multipart.new(_uri.path, data)
|
748
752
|
else
|
749
753
|
req = Net::HTTP::Post.new(_uri.path, headers)
|
@@ -760,11 +764,19 @@ module Sailthru
|
|
760
764
|
end
|
761
765
|
|
762
766
|
begin
|
763
|
-
|
764
|
-
|
767
|
+
http = Net::HTTP.new(_uri.host, _uri.port)
|
768
|
+
|
769
|
+
if _uri.scheme == 'https'
|
770
|
+
http.use_ssl = true
|
771
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @verify_ssl != true # some openSSL client doesn't work without doing this
|
772
|
+
end
|
773
|
+
|
774
|
+
response = http.start {
|
775
|
+
http.request(req)
|
765
776
|
}
|
777
|
+
|
766
778
|
rescue Exception => e
|
767
|
-
raise SailthruClientException.new("Unable to open stream: #{_uri.to_s}");
|
779
|
+
raise SailthruClientException.new("Unable to open stream: #{_uri.to_s}\n" + e);
|
768
780
|
end
|
769
781
|
|
770
782
|
if response.body
|
@@ -778,5 +790,15 @@ module Sailthru
|
|
778
790
|
req = Net::HTTP::Post::Multipart.new url.path,
|
779
791
|
"file" => UploadIO.new(data['file'], "application/octet-stream")
|
780
792
|
end
|
793
|
+
|
794
|
+
def prepare_json_payload(data)
|
795
|
+
payload = {
|
796
|
+
:api_key => @api_key,
|
797
|
+
:format => 'json', #fuck XML
|
798
|
+
:json => data.to_json
|
799
|
+
}
|
800
|
+
payload[:sig] = get_signature_hash(payload, @secret)
|
801
|
+
payload
|
802
|
+
end
|
781
803
|
end
|
782
804
|
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:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: "1.
|
8
|
+
- 11
|
9
|
+
version: "1.11"
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Prajwal Tuladhar
|
@@ -14,8 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
18
|
-
default_executable:
|
17
|
+
date: 2011-06-15 00:00:00 Z
|
19
18
|
dependencies:
|
20
19
|
- !ruby/object:Gem::Dependency
|
21
20
|
name: json
|
@@ -59,6 +58,20 @@ dependencies:
|
|
59
58
|
version: "0"
|
60
59
|
type: :development
|
61
60
|
version_requirements: *id003
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: shoulda
|
63
|
+
prerelease: false
|
64
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
hash: 3
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
type: :development
|
74
|
+
version_requirements: *id004
|
62
75
|
description:
|
63
76
|
email: praj@sailthru.com
|
64
77
|
executables: []
|
@@ -70,7 +83,6 @@ extra_rdoc_files:
|
|
70
83
|
files:
|
71
84
|
- README.md
|
72
85
|
- lib/sailthru.rb
|
73
|
-
has_rdoc: true
|
74
86
|
homepage: http://docs.sailthru.com
|
75
87
|
licenses: []
|
76
88
|
|
@@ -101,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
101
113
|
requirements: []
|
102
114
|
|
103
115
|
rubyforge_project:
|
104
|
-
rubygems_version: 1.
|
116
|
+
rubygems_version: 1.8.5
|
105
117
|
signing_key:
|
106
118
|
specification_version: 3
|
107
119
|
summary: A simple client library to remotely access the Sailthru REST API.
|