Class: Shipindo::Carrier::Jne

Inherits:
Object
  • Object
show all
Defined in:
lib/shipindo/carriers/jne.rb

Instance Method Summary collapse

Instance Method Details

#find_city(name) ⇒ Object



20
21
22
23
# File 'lib/shipindo/carriers/jne.rb', line 20

def find_city(name)
  city = list_city(name).first
  city unless city[:code] == " null "
end

#find_rates(options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/shipindo/carriers/jne.rb', line 25

def find_rates(options)
  if options[:origin] && options[:destination]

    unless options[:origin_code]
      options[:origin_code] = find_city(options[:origin])[:code]
    end

    unless options[:destination_code]
      options[:destination_code] = find_city(options[:destination])[:code]
    end
    # another jne server flunk, response city name is from request parameters,
    # if there isn't any eg. using only code if cities, then it's displayed as empty.
    options[:from] = options[:origin]
    options[:to]   = options[:destination]
  end

  unless options[:origin_code] && options[:destination_code]
    raise(ArgumentError, "origin & destination or code must be defined")
  end

  options[:weight] ||= 1

  resp = RestClient.post "http://www.jne.co.id/index.php?mib=tariff&lang=IN", options

  doc  = Nokogiri::HTML.parse(resp.body)

  options[:response] = parse_rates(doc)
  options
end

#list_city(query, limit = 1) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/shipindo/carriers/jne.rb', line 10

def list_city(query, limit=1)
  # bug in JNE server, limit parameter is ignored
  resp = RestClient.get "http://www.jne.co.id/tariff.php?q=#{ query }&limit=#{ limit }"

  resp.body.split("\r\n").map do |item|
    city, code  = item.split("|")
    { :city => city, :code => code }
  end
end