Class: CreateSend::CreateSend

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

Overview

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

Direct Known Subclasses

Administrator, Subscriber

Constant Summary collapse

@@base_uri =
"https://api.createsend.com/api/v3"
@@oauth_base_uri =
"https://api.createsend.com/oauth"
@@oauth_token_uri =
"#{@@oauth_base_uri}/token"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CreateSend

Returns a new instance of CreateSend.



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

def initialize(*args)
  if args.size > 0
    auth args.first # Expect auth details as first argument
  end
end

Instance Attribute Details

#auth_detailsObject (readonly)

Returns the value of attribute auth_details.



39
40
41
# File 'lib/createsend/createsend.rb', line 39

def auth_details
  @auth_details
end

Class Method Details

.authorize_url(client_id, redirect_uri, scope, state = nil) ⇒ Object

Get the authorization URL for your application, given the application’s client_id, redirect_uri, scope, and optional state data.



43
44
45
46
47
48
49
# File 'lib/createsend/createsend.rb', line 43

def self.authorize_url(client_id, redirect_uri, scope, state=nil)
  qs = "client_id=#{CGI.escape(client_id.to_s)}"
  qs << "&redirect_uri=#{CGI.escape(redirect_uri.to_s)}"
  qs << "&scope=#{CGI.escape(scope.to_s)}"
  qs << "&state=#{CGI.escape(state.to_s)}" if state
  "#{@@oauth_base_uri}?#{qs}"
end

.exchange_token(client_id, client_secret, redirect_uri, code) ⇒ Object

Exchange a provided OAuth code for an OAuth access token, ‘expires in’ value and refresh token.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/createsend/createsend.rb', line 53

def self.exchange_token(client_id, client_secret, redirect_uri, code)
  body = "grant_type=authorization_code"
  body << "&client_id=#{CGI.escape(client_id.to_s)}"
  body << "&client_secret=#{CGI.escape(client_secret.to_s)}"
  body << "&redirect_uri=#{CGI.escape(redirect_uri.to_s)}"
  body << "&code=#{CGI.escape(code.to_s)}"
  options = {:body => body}
  response = HTTParty.post(@@oauth_token_uri, options)
  if response.has_key? 'error' and response.has_key? 'error_description'
    err = "Error exchanging code for access token: "
    err << "#{response['error']} - #{response['error_description']}"
    raise err
  end
  r = Hashie::Mash.new(response)
  [r.access_token, r.expires_in, r.refresh_token]
end

Instance Method Details

#add_auth_details_to_options(args) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/createsend/createsend.rb', line 204

def add_auth_details_to_options(args)
  if @auth_details
    options = {}
    if args.size > 1
      options = args[1]
    end
    if @auth_details.has_key? :access_token
      options[:headers] = {
        "Authorization" => "Bearer #{@auth_details[:access_token]}" }
    elsif @auth_details.has_key? :api_key
      if not options.has_key? :basic_auth
        options[:basic_auth] = {
          :username => @auth_details[:api_key], :password => 'x' }
      end
    end
    args[1] = options
  end
  args
end

#administratorsObject

Gets the administrators for the account.



162
163
164
165
# File 'lib/createsend/createsend.rb', line 162

def administrators
  response = 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.



122
123
124
125
126
127
128
129
# File 'lib/createsend/createsend.rb', line 122

def apikey(site_url, username, password)
  site_url = CGI.escape(site_url)
  options = {:basic_auth => {:username => username, :password => password}}
  response = get("/apikey.json?SiteUrl=#{site_url}", options)
  result = Hashie::Mash.new(response)
  auth({:api_key => result.ApiKey}) if not @auth_details
  result
end

#auth(auth_details) ⇒ Object

Authenticate using either OAuth or an API key.



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

def auth(auth_details)
  @auth_details = auth_details
end

#billing_detailsObject

Get your billing details.



138
139
140
141
# File 'lib/createsend/createsend.rb', line 138

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

#clientsObject

Gets your clients.



132
133
134
135
# File 'lib/createsend/createsend.rb', line 132

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

#countriesObject

Gets valid countries.



144
145
146
147
# File 'lib/createsend/createsend.rb', line 144

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

#delete(*args) ⇒ Object Also known as: cs_delete



198
199
200
201
# File 'lib/createsend/createsend.rb', line 198

def delete(*args)
  args = add_auth_details_to_options(args)
  handle_response CreateSend.delete(*args)
end

#get(*args) ⇒ Object Also known as: cs_get



180
181
182
183
# File 'lib/createsend/createsend.rb', line 180

def get(*args)
  args = add_auth_details_to_options(args)
  handle_response CreateSend.get(*args)
end

#get_primary_contactObject

Gets the primary contact for the account.



168
169
170
171
# File 'lib/createsend/createsend.rb', line 168

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

#handle_response(response) ⇒ Object

:nodoc:



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/createsend/createsend.rb', line 224

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

#post(*args) ⇒ Object Also known as: cs_post



186
187
188
189
# File 'lib/createsend/createsend.rb', line 186

def post(*args)
  args = add_auth_details_to_options(args)
  handle_response CreateSend.post(*args)
end

#put(*args) ⇒ Object Also known as: cs_put



192
193
194
195
# File 'lib/createsend/createsend.rb', line 192

def put(*args)
  args = add_auth_details_to_options(args)
  handle_response CreateSend.put(*args)
end

#refresh_tokenObject

Refresh the current OAuth token using the current refresh token.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/createsend/createsend.rb', line 104

def refresh_token
  if not @auth_details or
    not @auth_details.has_key? :refresh_token or
    not @auth_details[:refresh_token]
    raise '@auth_details[:refresh_token] does not contain a refresh token.'
  end

  options = {
    :body => "grant_type=refresh_token&refresh_token=#{@auth_details[:refresh_token]}" }
  response = HTTParty.post(@@oauth_token_uri, options)
  r = Hashie::Mash.new(response)
  auth({
    :access_token => r.access_token,
    :refresh_token => r.refresh_token})
  [r.access_token, r.expires_in, r.refresh_token]
end

#set_primary_contact(email) ⇒ Object

Set the primary contect for the account.



174
175
176
177
178
# File 'lib/createsend/createsend.rb', line 174

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

#systemdateObject

Gets the current date in your account’s timezone.



150
151
152
153
# File 'lib/createsend/createsend.rb', line 150

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

#timezonesObject

Gets valid timezones.



156
157
158
159
# File 'lib/createsend/createsend.rb', line 156

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