class ApiCall

Attributes

api_key[RW]
test[RW]
url[RW]

Public Class Methods

new(api_key, locale = 'en', test = false) click to toggle source
# File lib/unisender_api/api_call.rb, line 7
def initialize(api_key, locale = 'en', test = false)
  self.api_key = api_key
  self.test = test
  self.url = "http://api.unisender.com/#{locale}/api/"
end

Public Instance Methods

api_call(method, params) click to toggle source
# File lib/unisender_api/api_call.rb, line 14
def api_call(method, params)
  url = self.url + method
  params.merge!(:test_mode => '1') if self.test
  params.merge!(:api_key => self.api_key)
  result = JSON.parse(self.post(url, params))
  return result
end
gen_hash(param, label) click to toggle source
# File lib/unisender_api/api_call.rb, line 23
def gen_hash(param, label)
  hash = Hash.new
  
  
  case param
  when Time
    hash.merge!("#{label}" => param.strftime("%F %H:%M"))
  when TrueClass 
    hash.merge!("#{label}" => '1')
  when FalseClass
    hash.merge!("#{label}" => '0')
  when String
    hash.merge!("#{label}" => param.to_s)
  when Fixnum
    hash.merge!("#{label}" => param.to_s)
  when Bignum
    hash.merge!("#{label}" => param.to_s)
  when Float
    hash.merge!("#{label}" => param.to_s)
  when Hash
    param.each do |key, value|
      hash.merge!(self.gen_hash(value,"#{label}[#{key}]"))
    end
  when Array
    i = 0
    array.each do |value|
      hash.merge!(self.gen_hash(value, "#{label}[#{i}]"))
      i = i+1
    end
  end
  return hash
end
post(url, param) click to toggle source
# File lib/unisender_api/api_call.rb, line 56
def post(url, param)
   uri = URI.parse(url)
   http = Net::HTTP.new(uri.host, uri.port)
   params = Addressable::URI.new
   params.query_values = param
   http.post(uri.path, params.query).body
end