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'
VERSION =
"0.1.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.API_KEYObject

Returns the value of attribute API_KEY.



12
13
14
# File 'lib/kuaidi100_rails.rb', line 12

def API_KEY
  @API_KEY
end

.POLL_CALLBACK_URLObject

Returns the value of attribute POLL_CALLBACK_URL.



14
15
16
# File 'lib/kuaidi100_rails.rb', line 14

def POLL_CALLBACK_URL
  @POLL_CALLBACK_URL
end

.POLL_KEYObject

Returns the value of attribute POLL_KEY.



13
14
15
# File 'lib/kuaidi100_rails.rb', line 13

def POLL_KEY
  @POLL_KEY
end

Class Method Details

.crawl(com, nu) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kuaidi100_rails.rb', line 19

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) ⇒ Object

subscribe express

Parameters==

com company code nu express number from start from address to destination address salt random signature string



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/kuaidi100_rails.rb', line 40

def subscribe(com, nu, from, to, salt=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'
        }
    }
    p body

    response = Faraday.new(:url => SUBSCRIBE_URL).post do |req|
      req.headers['Content-Type'] = 'application/json'
      req.params[:schema] = 'json'
      req.params[: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