Sha256: aa8aac6c4acf1314e99f77bebdd352c5a16a4b38c7a778bfa5543aadb165c8b9

Contents?: true

Size: 970 Bytes

Versions: 2

Compression:

Stored size: 970 Bytes

Contents

# coding: utf-8
module GuanyiErp
  class Request

    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

    def self.parse_json(json_data_str)
      begin
        return JSON.parse(json_data_str)
      rescue
      end
      nil
    end

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
guanyi-erp-1.0.3 lib/guanyi_erp/request.rb
guanyi-erp-1.0.2 lib/guanyi_erp/request.rb