Class: CreateSend::CreateSend

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/createsend.rb

Overview

Provides high level CreateSend functionality/data you’ll probably need.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.api_key(api_key = nil) ⇒ Object

Sets the API key which will be used to make calls to the CreateSend API.



68
69
70
71
72
73
# File 'lib/createsend.rb', line 68

def self.api_key(api_key=nil)
  return @@api_key unless api_key
  CreateSendOptions['api_key'] = api_key
  @@api_key = api_key
  basic_auth @@api_key, 'x'
end

.delete(*args) ⇒ Object



112
# File 'lib/createsend.rb', line 112

def self.delete(*args); handle_response super end

.get(*args) ⇒ Object



109
# File 'lib/createsend.rb', line 109

def self.get(*args); handle_response super end

.handle_response(response) ⇒ Object

:nodoc:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/createsend.rb', line 114

def self.handle_response(response) # :nodoc:
  case response.code
  when 400
    raise BadRequest.new(Hashie::Mash.new response)
  when 401
    raise Unauthorized.new(Hashie::Mash.new response)
  when 404
    raise NotFound.new
  when 400...500
    raise ClientError.new
  when 500...600
    raise ServerError.new
  else
    response
  end
end

.post(*args) ⇒ Object



110
# File 'lib/createsend.rb', line 110

def self.post(*args); handle_response super end

.put(*args) ⇒ Object



111
# File 'lib/createsend.rb', line 111

def self.put(*args); handle_response super end

Instance Method Details

#apikey(site_url, username, password) ⇒ Object

Gets your CreateSend API key, given your site url, username and password.



76
77
78
79
80
81
82
83
# File 'lib/createsend.rb', line 76

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'
  Hashie::Mash.new(response)
end

#clientsObject

Gets your clients.



86
87
88
89
# File 'lib/createsend.rb', line 86

def clients
  response = CreateSend.get('/clients.json')
  response.map{|item| Hashie::Mash.new(item)}
end

#countriesObject

Gets valid countries.



92
93
94
95
# File 'lib/createsend.rb', line 92

def countries
  response = CreateSend.get('/countries.json')
  response.parsed_response
end

#systemdateObject

Gets the current date in your account’s timezone.



98
99
100
101
# File 'lib/createsend.rb', line 98

def systemdate
  response = CreateSend.get('/systemdate.json')
  Hashie::Mash.new(response)
end

#timezonesObject

Gets valid timezones.



104
105
106
107
# File 'lib/createsend.rb', line 104

def timezones
  response = CreateSend.get('/timezones.json')
  response.parsed_response
end