Module: Kuaidi100Rails

Defined in:
lib/kuaidi100_rails.rb,
lib/kuaidi100_rails/version.rb

Defined Under Namespace

Classes: SHash

Constant Summary collapse

API_URL =
'http://api.kuaidi100.com/api'
SUBSCRIBE_URL =
'http://www.kuaidi100.com/poll'
EXPRESS_KEYWORDS =
{yuantong: '圆通',
shunfeng: '顺丰',
bjemstckj: '北京EMS',
ems: 'ems',
shentong: '申通',
yunda: '韵达',
zhongtong: '中通',
huitongkuaidi: '汇通',
debangwuliu: '德邦',
zhaijisong: '宅急送',
tiantian: '天天',
guotongkuaidi: '国通',
zengyisudi: '增益',
suer: '速尔',
ztky: '中铁物流',
zhongtiewuliu: '中铁快运',
ganzhongnengda: '能达',
youshuwuliu: '优速',
quanfengkuaidi: '全峰',
jd: '京东'}
VERSION =
"0.1.5"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.API_KEYObject

Returns the value of attribute API_KEY.



33
34
35
# File 'lib/kuaidi100_rails.rb', line 33

def API_KEY
  @API_KEY
end

.POLL_CALLBACK_URLObject

Returns the value of attribute POLL_CALLBACK_URL.



35
36
37
# File 'lib/kuaidi100_rails.rb', line 35

def POLL_CALLBACK_URL
  @POLL_CALLBACK_URL
end

.POLL_KEYObject

Returns the value of attribute POLL_KEY.



34
35
36
# File 'lib/kuaidi100_rails.rb', line 34

def POLL_KEY
  @POLL_KEY
end

Class Method Details

.company_code(cn_name) ⇒ Object

cn_name to en_code



55
56
57
# File 'lib/kuaidi100_rails.rb', line 55

def company_code(cn_name)
  EXPRESS_KEYWORDS.select { |k, v| cn_name.to_s.downcase.include?(v.to_s.downcase) }.keys[0].to_s
end

.company_name(code_str) ⇒ Object

en_code to cn_name



60
61
62
# File 'lib/kuaidi100_rails.rb', line 60

def company_name(code_str)
  EXPRESS_KEYWORDS[code_str.to_s.to_sym]
end

.crawl(com, nu) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kuaidi100_rails.rb', line 40

def crawl(com, nu)
  begin
    url = URI.parse(API_URL)
    params = {:id => Kuaidi100Rails.API_KEY, :com => com, :nu => nu, :show => 0, :muti => 1, :order => 'desc'}
    url.query = URI.encode_www_form(params)
    p url
    res = Net::HTTP.get(url)
    hash = SHash.new(JSON.parse(res))
    p hash
  rescue Exception => e
    puts "error #{e}"
  end
end

.subscribe(com, nu, from, to, salt = nil, mobiletelephone = nil, seller = nil, commodity = nil) ⇒ Object

subscribe express

Parameters==

com company code nu express number from start from address to destination address salt random signature string mobiletelephone user phone seller seller’s name commodity product’s name



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/kuaidi100_rails.rb', line 74

def subscribe(com, nu, from, to, salt=nil, mobiletelephone=nil, seller=nil, commodity=nil)
  begin
    body = {
        company: com,
        number: nu,
        from: from,
        to: to,
        key: Kuaidi100Rails.POLL_KEY,
        parameters: {
            callbackurl: Kuaidi100Rails.POLL_CALLBACK_URL,
            salt: salt,
            resultv2: '1',
            mobiletelephone: mobiletelephone,
            seller: seller,
            commodity: commodity
        }
    }
    p body

    response = Faraday.new(:url => SUBSCRIBE_URL).post do |req|
      req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
      # req.params[:schema] = 'json'
      # req.params[:param] = body.to_json
      req.body={
          schema: 'json',
          param: body.to_json
      }
    end

    hash = SHash.new(JSON.parse(response.body))
    p hash
    return hash[:returnCode].to_i
  rescue Exception => e
    puts "error #{e}"
  end
  0
end