lib/sailthru.rb in sailthru-client-1.10 vs lib/sailthru.rb in sailthru-client-1.11
- old
+ new
@@ -1,16 +1,17 @@
require 'rubygems'
+require 'net/https'
require 'net/http'
require 'uri'
require 'cgi'
require 'json'
require 'digest/md5'
require 'net/http/post/multipart'
module Sailthru
- Version = VERSION = '1.10'
+ Version = VERSION = '1.11'
class SailthruClientException < Exception
end
module Helpers
@@ -102,20 +103,23 @@
class SailthruClient
include Helpers
+ attr_accessor :verify_ssl
+
# params:
# api_key, String
# secret, String
# api_uri, String
#
# Instantiate a new client; constructor optionally takes overrides for key/secret/uri.
- def initialize(api_key, secret, api_uri)
+ def initialize(api_key, secret, api_uri = nil)
@api_key = api_key
@secret = secret
- @api_uri = api_uri
+ @api_uri = if api_uri.nil? then 'https://api.sailthru.com' else api_uri end
+ @verify_ssl = true
end
# params:
# template_name, String
# email, String
@@ -689,37 +693,38 @@
# Hash
#
# Perform an API request, using the shared-secret auth hash.
#
def api_request(action, data, request_type, binary_key = nil)
-
- data[:api_key] = @api_key
- data[:format] ||= 'json'
-
if (!binary_key.nil?)
binary_key_data = data[binary_key]
data.delete(binary_key)
end
+
+ if data[:format].nil? or data[:format] == 'json'
+ data = self.prepare_json_payload(data)
+ else
+ data[:api_key] = @api_key
+ data[:format] ||= 'json'
+ data[:sig] = get_signature_hash(data, @secret)
+ end
- data[:sig] = get_signature_hash(data, @secret)
-
if (!binary_key.nil?)
data[binary_key] = binary_key_data
end
_result = self.http_request("#{@api_uri}/#{action}", data, request_type, binary_key)
-
# NOTE: don't do the unserialize here
- if data[:format] == 'json'
+ if data[:format] == 'json'
begin
- unserialized = JSON.parse(_result)
- return unserialized ? unserialized : _result
+ unserialized = JSON.parse(_result)
+ return unserialized ? unserialized : _result
rescue JSON::JSONError => e
- return {'error' => e}
+ return {'error' => e}
end
- end
- return _result
+ end
+ return _result
end
# params:
# uri, String
@@ -741,11 +746,10 @@
if method == 'POST'
if (!binary_key.nil?)
binary_data = data[binary_key]
data[binary_key] = UploadIO.new(File.open(binary_data), "text/plain")
- #req = Net::HTTP::Post::Multipart.new(_uri.path, binary_key => UploadIO.new(File.open(binary_data), "text/plain"))
req = Net::HTTP::Post::Multipart.new(_uri.path, data)
else
req = Net::HTTP::Post.new(_uri.path, headers)
req.set_form_data(data)
end
@@ -758,15 +762,23 @@
req = Net::HTTP::Get.new(request_uri, headers)
end
end
begin
- response = Net::HTTP.start(_uri.host, _uri.port) {|http|
- http.request(req)
+ http = Net::HTTP.new(_uri.host, _uri.port)
+
+ if _uri.scheme == 'https'
+ http.use_ssl = true
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @verify_ssl != true # some openSSL client doesn't work without doing this
+ end
+
+ response = http.start {
+ http.request(req)
}
+
rescue Exception => e
- raise SailthruClientException.new("Unable to open stream: #{_uri.to_s}");
+ raise SailthruClientException.new("Unable to open stream: #{_uri.to_s}\n" + e);
end
if response.body
return response.body
else
@@ -775,8 +787,18 @@
end
def http_multipart_request(uri, data)
req = Net::HTTP::Post::Multipart.new url.path,
"file" => UploadIO.new(data['file'], "application/octet-stream")
+ end
+
+ def prepare_json_payload(data)
+ payload = {
+ :api_key => @api_key,
+ :format => 'json', #fuck XML
+ :json => data.to_json
+ }
+ payload[:sig] = get_signature_hash(payload, @secret)
+ payload
end
end
end