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.

Constant Summary collapse

@@base_uri =
"https://api.createsend.com/api/v3"
@@api_key =
""

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.



83
84
85
86
87
# File 'lib/createsend.rb', line 83

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

.delete(*args) ⇒ Object



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

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

.get(*args) ⇒ Object



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

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

.handle_response(response) ⇒ Object

:nodoc:



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/createsend.rb', line 151

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



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

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

.put(*args) ⇒ Object



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

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

Instance Method Details

#administratorsObject

Gets the administrators



130
131
132
133
# File 'lib/createsend.rb', line 130

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

#apikey(site_url, username, password) ⇒ Object

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



90
91
92
93
94
95
96
97
# File 'lib/createsend.rb', line 90

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

#billing_detailsObject

Get your billing details.



106
107
108
109
# File 'lib/createsend.rb', line 106

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

#clientsObject

Gets your clients.



100
101
102
103
# File 'lib/createsend.rb', line 100

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

#countriesObject

Gets valid countries.



112
113
114
115
# File 'lib/createsend.rb', line 112

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

#get_primary_contactObject



135
136
137
138
# File 'lib/createsend.rb', line 135

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

#set_primary_contact(email) ⇒ Object



140
141
142
143
144
# File 'lib/createsend.rb', line 140

def set_primary_contact(email)
  options = { :query => { :email => email } }
  response = CreateSend.put("/primarycontact.json", options)
  Hashie::Mash.new(response)
end

#systemdateObject

Gets the current date in your account’s timezone.



118
119
120
121
# File 'lib/createsend.rb', line 118

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

#timezonesObject

Gets valid timezones.



124
125
126
127
# File 'lib/createsend.rb', line 124

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