Class: Rajaongkir

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

Overview

simple class ruby untuk API Rajaongkir rajaongkir.com/dokumentasi

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = nil) ⇒ Rajaongkir

key = API KEY dari rajaongkir



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

def initialize(key = nil)
  @key = key
  @base_url = "http://rajaongkir.com/api/"
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



10
11
12
# File 'lib/rajaongkir.rb', line 10

def base_url
  @base_url
end

#keyObject

Returns the value of attribute key.



10
11
12
# File 'lib/rajaongkir.rb', line 10

def key
  @key
end

Instance Method Details

#city(params = {}) ⇒ Object

fungsi untuk mendapatkan data kota

params = => ‘6’, ‘id’ => ‘161’



31
32
33
# File 'lib/rajaongkir.rb', line 31

def city(params = {})
  request "city",params
end

#cost(origin = '', destination = '', weight = 0, courier = 'pos') ⇒ Object

destination ID kota tujuan weight Berat kiriman dalam gram courier Kode kurir (jne, pos, tiki)



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rajaongkir.rb', line 48

def cost(origin = '', destination = '', weight = 0, courier = 'pos')
  error_message ||= {}

  obj = OpenStruct.new(
    :origin => origin,
    :destination => destination,
    :weight => weight
    );
  validation = Validator.new(obj)
  validation.rule(:origin, :not_empty)
  validation.rule(:destination, :not_empty)
  validation.rule(:weight, :not_null)

  if validation.valid?
    params = {:origin => origin, :destination => destination, :weight => weight, :courier => courier}

    request "cost", params, 'post'
  else
    validation.errors.each_pair do |key, value|
      error_message[key.to_s] = value
    end
    
    params = set_params 500, error_message
    data_return = Response.new params

    return data_return
  end
end

#get_base_urlObject

fungsi untuk mendapatkan base_url



24
25
26
# File 'lib/rajaongkir.rb', line 24

def get_base_url
  $base_url
end

#get_keyObject

fungsi untuk mendapatkan API-KEY



19
20
21
# File 'lib/rajaongkir.rb', line 19

def get_key
  @key
end

#province(params = {}) ⇒ Object

fungsi untuk mendapatkan data provinsi

params = => ‘6’



38
39
40
# File 'lib/rajaongkir.rb', line 38

def province(params = {})
  request "province", params
end

#set_params(code, message) ⇒ Object

fungsi untuk set params yang digunakan pada response



78
79
80
81
82
83
84
85
# File 'lib/rajaongkir.rb', line 78

def set_params code, message
  params = Hash.new
  params['code'] = code
  params['body'] = message
  params['headers'] = nil

  return params
end