class CreateSend::CreateSend

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

Attributes

auth_details[R]

Public Class Methods

authorize_url(client_id, redirect_uri, scope, state=nil) click to toggle source

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

# File lib/createsend/createsend.rb, line 64
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) click to toggle source

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

# File lib/createsend/createsend.rb, line 74
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
new(*args) click to toggle source
# File lib/createsend/createsend.rb, line 106
def initialize(*args)
  if args.size > 0
    auth args.first # Expect auth details as first argument
  end
end
refresh_access_token(refresh_token) click to toggle source

Refresh an OAuth access token, given an OAuth refresh token. Returns a new access token, 'expires in' value, and refresh token.

# File lib/createsend/createsend.rb, line 93
def self.refresh_access_token(refresh_token)
  options = {
    :body => "grant_type=refresh_token&refresh_token=#{CGI.escape(refresh_token)}" }
  response = HTTParty.post(@@oauth_token_uri, options)
  if response.has_key? 'error' and response.has_key? 'error_description'
    err = "Error refreshing 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
user_agent(user_agent) click to toggle source

Set a custom user agent string to be used when instances of CreateSend::CreateSend make API calls.

user_agent - The user agent string to use in the User-Agent header when

instances of this class make API calls. If set to nil, the
default value of CreateSend::USER_AGENT_STRING will be used.
# File lib/createsend/createsend.rb, line 58
def self.user_agent(user_agent)
  headers({'User-Agent' => user_agent || USER_AGENT_STRING})
end

Public Instance Methods

add_auth_details_to_options(args) click to toggle source
# File lib/createsend/createsend.rb, line 242
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
administrators() click to toggle source

Gets the administrators for the account.

# File lib/createsend/createsend.rb, line 173
def administrators
  response = get('/admins.json')
  response.map{|item| Hashie::Mash.new(item)}
end
auth(auth_details) click to toggle source

Authenticate using either OAuth or an API key.

# File lib/createsend/createsend.rb, line 122
def auth(auth_details)
  @auth_details = auth_details
end
billing_details() click to toggle source

Get your billing details.

# File lib/createsend/createsend.rb, line 149
def billing_details
  response = get('/billingdetails.json')
  Hashie::Mash.new(response)
end
clients() click to toggle source

Gets your clients.

# File lib/createsend/createsend.rb, line 143
def clients
  response = get('/clients.json')
  response.map{|item| Hashie::Mash.new(item)}
end
countries() click to toggle source

Gets valid countries.

# File lib/createsend/createsend.rb, line 155
def countries
  response = get('/countries.json')
  response.parsed_response
end
cs_delete(*args)
Alias for: delete
cs_get(*args)
Alias for: get
cs_post(*args)
Alias for: post
cs_put(*args)
Alias for: put
delete(*args) click to toggle source
# File lib/createsend/createsend.rb, line 236
def delete(*args)
  args = add_auth_details_to_options(args)
  handle_response CreateSend.delete(*args)
end
Also aliased as: cs_delete
external_session_url(email, chrome, url, integrator_id, client_id) click to toggle source

Get a URL which initiates a new external session for the user with the given email. Full details: www.campaignmonitor.com/api/account/#single_sign_on

email - The email address of the Campaign Monitor user for whom

the login session should be created.

chrome - Which 'chrome' to display - Must be either β€œall”,

"tabs", or "none".

url - The URL to display once logged in. e.g. β€œ/subscribers/” integrator_id - The integrator ID. You need to contact Campaign Monitor

support to get an integrator ID.

client_id - The Client ID of the client which should be active once

logged in to the Campaign Monitor account.

Returns An object containing a single field SessionUrl which represents the URL to initiate the external Campaign Monitor session.

# File lib/createsend/createsend.rb, line 207
def external_session_url(email, chrome, url, integrator_id, client_id)
  options = { :body => {
    :Email => email,
    :Chrome => chrome,
    :Url => url,
    :IntegratorID => integrator_id,
    :ClientID => client_id }.to_json }
  response = put("/externalsession.json", options)
  Hashie::Mash.new(response)
end
get(*args) click to toggle source
# File lib/createsend/createsend.rb, line 218
def get(*args)
  args = add_auth_details_to_options(args)
  handle_response CreateSend.get(*args)
end
Also aliased as: cs_get
get_primary_contact() click to toggle source

Gets the primary contact for the account.

# File lib/createsend/createsend.rb, line 179
def get_primary_contact
  response = get('/primarycontact.json')
  Hashie::Mash.new(response)
end
post(*args) click to toggle source
# File lib/createsend/createsend.rb, line 224
def post(*args)
  args = add_auth_details_to_options(args)
  handle_response CreateSend.post(*args)
end
Also aliased as: cs_post
put(*args) click to toggle source
# File lib/createsend/createsend.rb, line 230
def put(*args)
  args = add_auth_details_to_options(args)
  handle_response CreateSend.put(*args)
end
Also aliased as: cs_put
refresh_token() click to toggle source

Refresh the current OAuth token using the current refresh token.

# File lib/createsend/createsend.rb, line 127
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

  access_token, expires_in, refresh_token =
    self.class.refresh_access_token @auth_details[:refresh_token]
  auth({
    :access_token => access_token,
    :refresh_token => refresh_token})
  [access_token, expires_in, refresh_token]
end
set_primary_contact(email) click to toggle source

Set the primary contect for the account.

# File lib/createsend/createsend.rb, line 185
def set_primary_contact(email)
  options = { :query => { :email => email } }
  response = put("/primarycontact.json", options)
  Hashie::Mash.new(response)
end
systemdate() click to toggle source

Gets the current date in your account's timezone.

# File lib/createsend/createsend.rb, line 161
def systemdate
  response = get('/systemdate.json')
  Hashie::Mash.new(response)
end
timezones() click to toggle source

Gets valid timezones.

# File lib/createsend/createsend.rb, line 167
def timezones
  response = get('/timezones.json')
  response.parsed_response
end