class GuanyiErp::Request

Public Class Methods

parse_json(json_data_str) click to toggle source
# File lib/guanyi_erp/request.rb, line 24
def self.parse_json(json_data_str)
  begin
    return JSON.parse(json_data_str)
  rescue
  end
  nil
end
post(params) click to toggle source
# File lib/guanyi_erp/request.rb, line 5
def self.post(params)

  response = Faraday.new(:url => GuanyiErp::Config.api_host).post do |req|
    req.url GuanyiErp::Config.api_path
    req.headers['Content-Type'] = 'application/json'
    body_hash = {
        appkey: GuanyiErp::Config.app_key,
        sessionkey: GuanyiErp::Config.session_key
    }.merge(params)
    body_hash = body_hash.merge({sign: sign(body_hash)})
    puts body_hash.to_json
    req.body= body_hash.to_json
  end
  body = parse_json(response.body)
  return nil unless body
  body

end
sign(body_hash) click to toggle source
# File lib/guanyi_erp/request.rb, line 32
def self.sign(body_hash)
  Digest::MD5.hexdigest("#{GuanyiErp::Config.secret}#{body_hash.to_json.gsub(/\\u([0-9a-z]{4})/){|s| [$1.to_i(16)].pack("U")}}#{GuanyiErp::Config.secret}")
end