Class: Rajaongkir::API

Inherits:
Object
  • Object
show all
Defined in:
lib/rajaongkir/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, opts) ⇒ API

api_key = API Key from RajaOngkir opts = Host of RajaOngkir opts = Account Type (starter/basic/pro) opts = Timeout, default 5s opts = Open Timeout, default 5s



14
15
16
17
18
19
20
# File 'lib/rajaongkir/api.rb', line 14

def initialize(api_key, opts)
  @api_key = api_key
  @host = opts[:host]
  @type = opts[:type]
  @timeout = opts[:timeout]
  @open_timeout = opts[:open_timeout]
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



7
8
9
# File 'lib/rajaongkir/api.rb', line 7

def api_key
  @api_key
end

#hostObject

Returns the value of attribute host.



7
8
9
# File 'lib/rajaongkir/api.rb', line 7

def host
  @host
end

#open_timeoutObject

Returns the value of attribute open_timeout.



7
8
9
# File 'lib/rajaongkir/api.rb', line 7

def open_timeout
  @open_timeout
end

#timeoutObject

Returns the value of attribute timeout.



7
8
9
# File 'lib/rajaongkir/api.rb', line 7

def timeout
  @timeout
end

#typeObject

Returns the value of attribute type.



7
8
9
# File 'lib/rajaongkir/api.rb', line 7

def type
  @type
end

Instance Method Details

#city(opts = {}) ⇒ Object

Retrieve cities list from RajaOngkir

Example: >> cities = rajaongkir.city

> #<Rajaongkir::Response @results=[“province_id”=>“21”, “province”=>“Nanggroe Aceh Darussalam (NAD)”, “type”=>“Kabupaten”, “city_name”=>“Aceh Barat”, “postal_code”=>“23681”, “province_id”=>“21”, “province”=>“Nanggroe Aceh Darussalam (NAD)”, “type”=>“Kabupaten”, “city_name”=>“Aceh Barat Daya”, “postal_code”=>“23764”]>



41
42
43
44
45
46
47
48
# File 'lib/rajaongkir/api.rb', line 41

def city(opts = {})
  payload = {
    id: opts[:city_id],
    province_id: opts[:province_id]
  }.compact

  call(:get, 'city', payload)
end

#cost(opts = {}) ⇒ Object

Retrieve cost from source city to destination city given weight and courier

Example: >> cost = rajaongkir.cost(origin: 1, destination: 2, weight: 1, courier: ‘jne’)

> #<Rajaongkir::Response @results=[“name”=>“Jalur Nugraha Ekakurir (JNE)”, “costs”=>[{“service”=>“OKE”, “description”=>“Ongkos Kirim Ekonomis”, “cost”=>[{“value”=>18000, “etd”=>“4-6”, “note”=>“”]}, “description”=>“Layanan Reguler”, “cost”=>[{“value”=>20000, “etd”=>“2-3”, “note”=>“”]}]}]>



56
57
58
59
60
61
62
63
64
65
# File 'lib/rajaongkir/api.rb', line 56

def cost(opts = {})
  payload = {
    origin: opts[:origin].to_s,
    destination: opts[:destination].to_s,
    weight: opts[:weight].to_i,
    courier: opts[:courier].to_s
  }.compact

  call(:post, 'cost', payload)
end

#province(opts = {}) ⇒ Object

Retrieve provinces list from RajaOngkir

Example: >> provinces = rajaongkir.province

> <Rajaongkir::Response @results=[“province”=>“Bali”, “province”=>“Bangka Belitung”, “province”=>“Banten”]>



27
28
29
30
31
32
33
# File 'lib/rajaongkir/api.rb', line 27

def province(opts = {})
  payload = {
    id: opts[:province_id]
  }.compact

  call(:get, 'province', payload)
end