lib/createsend.rb in createsend-1.1.1 vs lib/createsend.rb in createsend-2.0.0

- old
+ new

@@ -16,43 +16,50 @@ require 'createsend/person' require 'createsend/administrator' module CreateSend - # Just allows callers to do CreateSend.api_key "..." rather than CreateSend::CreateSend.api_key "..." etc + # Just allows callers to do CreateSend.api_key "..." rather than + # CreateSend::CreateSend.api_key "..." etc class << self def api_key(api_key=nil) r = CreateSend.api_key api_key end - + def base_uri(uri) r = CreateSend.base_uri uri end end - # Represents a CreateSend API error and contains specific data about the error. + # Represents a CreateSend API error. Contains specific data about the error. class CreateSendError < StandardError attr_reader :data def initialize(data) @data = data # @data should contain Code, Message and optionally ResultData extra = @data.ResultData ? "\nExtra result data: #{@data.ResultData}" : "" - super "The CreateSend API responded with the following error - #{@data.Code}: #{@data.Message}#{extra}" + super "The CreateSend API responded with the following error"\ + " - #{@data.Code}: #{@data.Message}#{extra}" end end + # Raised for HTTP response codes of 400...500 class ClientError < StandardError; end + # Raised for HTTP response codes of 500...600 class ServerError < StandardError; end + # Raised for HTTP response code of 400 class BadRequest < CreateSendError; end + # Raised for HTTP response code of 401 class Unauthorized < CreateSendError; end + # Raised for HTTP response code of 404 class NotFound < ClientError; end - class Unavailable < StandardError; end # Provides high level CreateSend functionality/data you'll probably need. class CreateSend include HTTParty - + + # Deals with an unfortunate situation where responses aren't valid json. class Parser::DealWithCreateSendInvalidJson < HTTParty::Parser # The createsend API returns an ID as a string when a 201 Created # response is returned. Unfortunately this is invalid json. def parse begin @@ -63,12 +70,12 @@ end end parser Parser::DealWithCreateSendInvalidJson @@base_uri = "https://api.createsend.com/api/v3" @@api_key = "" - headers({ - 'User-Agent' => "createsend-ruby-#{VERSION}", + headers({ + 'User-Agent' => "createsend-ruby-#{VERSION}", 'Content-Type' => 'application/json; charset=utf-8', 'Accept-Encoding' => 'gzip, deflate' }) base_uri @@base_uri basic_auth @@api_key, 'x' @@ -78,11 +85,11 @@ @@api_key = api_key basic_auth @@api_key, 'x' end # Gets your CreateSend API key, given your site url, username and password. - def apikey(site_url, username, password) + def apikey(site_url, username, password) site_url = CGI.escape(site_url) self.class.basic_auth username, password response = CreateSend.get("/apikey.json?SiteUrl=#{site_url}") # Revert basic_auth to use @@api_key, 'x' self.class.basic_auth @@api_key, 'x' @@ -110,21 +117,21 @@ # Gets valid timezones. def timezones response = CreateSend.get('/timezones.json') response.parsed_response end - + # Gets the administrators def administrators response = CreateSend.get('/admins.json') response.map{|item| Hashie::Mash.new(item)} end - + def get_primary_contact response = CreateSend.get('/primarycontact.json') Hashie::Mash.new(response) end - + def set_primary_contact(email) options = { :query => { :email => email } } response = CreateSend.put("/primarycontact.json", options) Hashie::Mash.new(response) end \ No newline at end of file