class Smartsend::Client

Constants

BASE_URL

Public Class Methods

new(account=nil) click to toggle source
# File lib/smartsend/client.rb, line 13
def initialize(account=nil)
  @account = account || Smartsend.account
end
with_logs() { || ... } click to toggle source
# File lib/smartsend/client.rb, line 7
def self.with_logs
  @@logging = true
  yield
  @@logging = false
end

Public Instance Methods

get(path) click to toggle source
# File lib/smartsend/client.rb, line 26
def get(path)
  log("GET #{path}")

  request do
    http.get(url(path))
  end
end
get_plain(path) click to toggle source
# File lib/smartsend/client.rb, line 34
def get_plain(path)
  http.get(url(path))
end
post(path, params) click to toggle source
# File lib/smartsend/client.rb, line 17
def post(path, params)
  log("POST #{path}")
  log(params)

  request do
    http.post(url(path), json: params)
  end
end
request() { || ... } click to toggle source
# File lib/smartsend/client.rb, line 38
def request
  response = yield

  log(response)
  log(response.body.to_s)

  case response.code
  when (200..299)
    Response.new(JSON.parse(response)).successful!(response.code)
  when 401
    raise Smartsend::AuthorizationError, 'Unable to authorize'
  when 404
    raise Smartsend::NotFoundError, 'Resource not found'
  else
    Response.new(JSON.parse(response)).failed!(response.code)
  end
end

Private Instance Methods

http() click to toggle source
# File lib/smartsend/client.rb, line 97
def http
  raise Smartsend::MissingConfigError, 'Missing api_token' if @account.api_token.nil?

  HTTP.headers({
    accept: 'application/json',
    user_agent: user_agent_string,
    referer: @account.referer
  })
end
log(value) click to toggle source
# File lib/smartsend/client.rb, line 83
def log(value)
  Logger.new($stdout).log(value) if @@logging
end
url(path) click to toggle source
# File lib/smartsend/client.rb, line 89
def url(path)
  "#{BASE_URL}/#{path}?api_token=#{@account.api_token}"
end
user_agent_string() click to toggle source
# File lib/smartsend/client.rb, line 93
def user_agent_string
  (["Ruby/#{Smartsend::VERSION}"] + @account.user_agents.to_a).join(' ')
end