Method: Ulpos::Client#invoke

Defined in:
lib/ulpos/client.rb

#invoke(method, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ulpos/client.rb', line 12

def invoke(method, options = {})
  params = {
    :timestamp => Time.now.strftime("%Y-%m-%d %H:%M:%S"),
    :format => 'json',
    :app_key => @app_key,
    :v => '2.0',
    :method => method,
    :sign_method => 'md5'
  }
  params.merge!(options)
  str = @app_secret + (params.sort.collect { |param| "#{param[0]}#{param[1]}" }).join("") + @app_secret
  params["sign"] = Digest::MD5.hexdigest(str).upcase!
  res = Net::HTTP.post_form(URI.parse(@endpoint), params)
  if params[:format] == 'json'
    JSON.parse(res.body)
  elsif params[:format] == 'xml'
    res.body
  else
    res.body
  end
end