class Octopush::Client

Public Class Methods

new() click to toggle source
# File lib/octopush-ruby/client.rb, line 8
def initialize
  raise "Should set user configuration before use" if Octopush.configuration.nil?

  @constants = Octopush::Constants
  @domain = @constants::DOMAIN
end

Public Instance Methods

create_sub_account(first_name, last_name, raison_sociable, alert_bound, alert_sms_type) click to toggle source

create sub account

first_name
last_name
raison_sociable
alert_bound
alert_sms_type - check Octopush::Constants::SMS_TYPES
# File lib/octopush-ruby/client.rb, line 72
def create_sub_account first_name, last_name, raison_sociable, alert_bound,
                       alert_sms_type
  path = @constants::PATH_SUB_ACCOUNT
  data = user_hash.merge( first_name: first_name,
                          last_name: last_name,
                          raison_sociable: raison_sociable,
                          alert_bound: alert_bound,
                          alert_sms_type: alert_sms_type
                        )
  res = request @domain, path, data
end
credit_sub_account(sub_account_email, sms_amount, sms_type) click to toggle source

credit sub account

sub_account - sub account email
sms_amount - number of credits
sms_type - a sms type, check Octopush::Constants::SMS_TYPES
# File lib/octopush-ruby/client.rb, line 88
def credit_sub_account sub_account_email, sms_amount, sms_type
  path = @constants::PATH_CREDIT_SUB_ACCOUNT_TOKEN
  data = user_hash.merge(sub_account_email: sub_account_email)
  res = request @domain, path, data
  token_res = parse_response res.body

  token = token_res['token']
  path = @constants::PATH_CREDIT_SUB_ACCOUNT
  if sms_type != 'FR' and sms_type != 'XXX'
    sms_type = 'FR'
  end

  data = data.merge(sms_number: sms_amount,
                    sms_type: sms_type,
                    token: token)

  res = request @domain, path, data
end
edit_options(*args) click to toggle source

update user options args should be a hash with the options that you want to update could be answer_email, sms_alert_bound, sms_alert_type @example

{answer_email: 'an_email@domain.com'}
# File lib/octopush-ruby/client.rb, line 20
def edit_options *args
  path = @constants::PATH_EDIT_OPTIONS
  data = user_hash.merge args[0]
  res = request @domain, path, data
end
get_balance() click to toggle source

returns current user's balance return a hash in the form: {balance_type => balance}

# File lib/octopush-ruby/client.rb, line 28
def get_balance
  path = @constants::PATH_BALANCE
  data = user_hash
  response = request @domain, path, data

  h = {}
  response["balance"].each do |balance|
    h = h.merge(balance.attributes["type"] => balance)
  end

  h
end
send_sms(sms, sending_date=nil, request_keys=nil) click to toggle source

send a sms

sms - a Octopush::SMS instance
sending_date - a date to send sms, required if sms_mode is DIFFERE,
               check Octopush::Constants::SMS_MODES for modes allowed
request_keys - Lists the key fields of the application you want to add
               in the sha1 hash. Check Octopush::Constants::REQUEST_KEYS
# File lib/octopush-ruby/client.rb, line 47
def send_sms sms, sending_date=nil, request_keys=nil
  raise 'require a sms object' if sms.class != Octopush::SMS

  path = @constants::PATH_SMS
  data = user_hash.merge(sms.variables_hash)

  if data[:sms_mode] == @constants::SMS_MODES['DIFFERE']
    raise 'Need specify sending_date for DIFFERE mode' if sending_date.nil?
    data = data.merge(sending_date: sending_date)
  end

  if !request_keys.nil?
    sha1 = get_request_sha1_string(request_keys)
    data = data.merge(request_keys: request_keys, request_sha1: sha1)
  end

  res = request @domain, path, data
end

Private Instance Methods

get_request_sha1_string(request_keys, data) click to toggle source

get a sha1 string in base to request_keys

# File lib/octopush-ruby/client.rb, line 142
def get_request_sha1_string request_keys, data
  char_to_field = @constants::REQUEST_KEYS
  request_string = ''
  request_keys.split('').each do |key|
    if !char_to_field[key].nil? and !data[char_to_field[key].to_sym].nil?
      request_string += data[char_to_field[key].to_sym]
    end
  end

  Digest::SHA1.hexdigest request_string
end
parse_response(response) click to toggle source

octopush api returns a xml after each request. We parse the xml to hash for more easy use

# File lib/octopush-ruby/client.rb, line 130
def parse_response response
  parser = Nori.new
  res_hash = parser.parse response
  code = res_hash["octopush"]["error_code"]
  if code != "000"
    raise Octopush::Constants::ERRORS[code]
  else
    res_hash["octopush"]
  end
end
request(domain, path, data, ssl=false) click to toggle source
# File lib/octopush-ruby/client.rb, line 116
def request domain, path, data, ssl=false
  prefix = ssl ? 'https://' : 'http://'
  url = prefix + domain + path
  uri = URI url
  req = Net::HTTP::Post.new uri.path
  req.set_form_data data
  res = Net::HTTP.start(uri.host, uri.port) do |http|
    http.request(req)
  end
  parse_response res.body
end
user_hash() click to toggle source
# File lib/octopush-ruby/client.rb, line 109
def user_hash
  {
    user_login: Octopush.configuration.user_login,
    api_key: Octopush.configuration.api_key
  }
end