Class: ApiCall

Inherits:
Object
  • Object
show all
Defined in:
lib/unisender_api/api_call.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, locale = 'en', test = false) ⇒ ApiCall

Returns a new instance of ApiCall.



7
8
9
10
11
# 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

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



5
6
7
# File 'lib/unisender_api/api_call.rb', line 5

def api_key
  @api_key
end

#testObject

Returns the value of attribute test.



5
6
7
# File 'lib/unisender_api/api_call.rb', line 5

def test
  @test
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/unisender_api/api_call.rb', line 5

def url
  @url
end

Instance Method Details

#api_call(method, params) ⇒ Object



14
15
16
17
18
19
20
# 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) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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) ⇒ Object



56
57
58
59
60
61
62
# 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