rajaongkir 0.2 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/rajaongkir.rb +21 -19
- data/lib/validator.rb +45 -0
- data/rajaongkir.gemspec +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9fa0f49e0a04dcc80ad2d80284142e327e5e1d30
|
4
|
+
data.tar.gz: 7867b3824d873a8c1eba1b9476bc8032a9f4626a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68e7a0da58af1e2ab190e92886f61dfbe6df99f6be2f09c049b9db13d01f1b66c7dac1d348ea407e8bf138cb43d1dea5f631fe18587556f56ac828a3ee40024a
|
7
|
+
data.tar.gz: 8cc810b02ee3e004064d6e1fb0e9e3b896ab3567b1d6d3a0b46fbbff96a6723b756dad38ed991d739cb280a6017b64935d60dbed12245496edcd03ba0df79e3e
|
data/lib/rajaongkir.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
require 'unirest'
|
3
3
|
require File.join(File.dirname(__FILE__), "response.rb")
|
4
|
+
require File.join(File.dirname(__FILE__), "validator.rb")
|
4
5
|
|
5
6
|
# simple class ruby untuk API Rajaongkir
|
6
7
|
# http://rajaongkir.com/dokumentasi
|
@@ -45,30 +46,31 @@ class Rajaongkir
|
|
45
46
|
# weight Berat kiriman dalam gram
|
46
47
|
# courier Kode kurir (jne, pos, tiki)
|
47
48
|
def cost(origin = '', destination = '', weight = 0, courier = 'pos')
|
48
|
-
error_message
|
49
|
+
error_message ||= {}
|
50
|
+
|
51
|
+
obj = OpenStruct.new(
|
52
|
+
:origin => origin,
|
53
|
+
:destination => destination,
|
54
|
+
:weight => weight
|
55
|
+
);
|
56
|
+
validation = Validator.new(obj)
|
57
|
+
validation.rule(:origin, :not_empty)
|
58
|
+
validation.rule(:destination, :not_empty)
|
59
|
+
validation.rule(:weight, :not_null)
|
60
|
+
|
61
|
+
if validation.valid?
|
62
|
+
params = {:origin => origin, :destination => destination, :weight => weight, :courier => courier}
|
49
63
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
error_message['destination'] = 'destination tidak boleh kosong'
|
57
|
-
end
|
58
|
-
if weight == 0
|
59
|
-
error = 1
|
60
|
-
error_message['weight'] = 'weight harus lebih besar dari 0'
|
61
|
-
end
|
62
|
-
|
63
|
-
if error == 1
|
64
|
+
request "cost", params, 'post'
|
65
|
+
else
|
66
|
+
validation.errors.each_pair do |key, value|
|
67
|
+
error_message[key.to_s] = value
|
68
|
+
end
|
69
|
+
|
64
70
|
params = set_params 500, error_message
|
65
71
|
data_return = Response.new params
|
66
72
|
|
67
73
|
return data_return
|
68
|
-
else
|
69
|
-
params = {:origin => origin, :destination => destination, :weight => weight, :courier => courier}
|
70
|
-
|
71
|
-
request "cost", params, 'post'
|
72
74
|
end
|
73
75
|
end
|
74
76
|
|
data/lib/validator.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
class Validator
|
2
|
+
attr_accessor :errors, :rules
|
3
|
+
|
4
|
+
def initialize(obj)
|
5
|
+
@rules ||= {}
|
6
|
+
@errors ||= {}
|
7
|
+
@obj = obj
|
8
|
+
|
9
|
+
# message error
|
10
|
+
@message = {
|
11
|
+
:not_empty => "tidak boleh kosong",
|
12
|
+
:not_null => "harus lebih besar dari 0"
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
# menambahkan aturan validasi
|
17
|
+
def rule(field, rule)
|
18
|
+
if ! rule.nil?
|
19
|
+
@rules[field] = rule
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# cek apakah object valid atau tidak
|
24
|
+
def valid?
|
25
|
+
valid = true
|
26
|
+
@rules.each_pair do |f,r|
|
27
|
+
if ! send(r.to_sym, @obj.send(f))
|
28
|
+
valid = false
|
29
|
+
@errors[f] = @message[r]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
@valid = valid
|
34
|
+
end
|
35
|
+
|
36
|
+
# tidak boleh kosong
|
37
|
+
def not_empty(value)
|
38
|
+
! (value.nil? || value == "")
|
39
|
+
end
|
40
|
+
|
41
|
+
# tidak boleh 0
|
42
|
+
def not_null(value)
|
43
|
+
! (value == 0)
|
44
|
+
end
|
45
|
+
end
|
data/rajaongkir.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rajaongkir
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Harry Yunanto
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: unirest
|
@@ -34,6 +34,7 @@ files:
|
|
34
34
|
- Rakefile
|
35
35
|
- lib/rajaongkir.rb
|
36
36
|
- lib/response.rb
|
37
|
+
- lib/validator.rb
|
37
38
|
- rajaongkir.gemspec
|
38
39
|
- test/test_rajaongkir.rb
|
39
40
|
homepage: https://github.com/iorme/rajaongkir-rb
|