rajaongkir 0.2 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1edc89559278295f1eb908ac040bf7bae02c9444
4
- data.tar.gz: 912bb6d7bff7738a6f8720f6d7a596183d8a677d
3
+ metadata.gz: 9fa0f49e0a04dcc80ad2d80284142e327e5e1d30
4
+ data.tar.gz: 7867b3824d873a8c1eba1b9476bc8032a9f4626a
5
5
  SHA512:
6
- metadata.gz: a0ff4b57c6886163602f94763ff793a6dbb1891c59a60dfbea7da211222162574181fefea1bb4e07dbe75fd090c2354c0cc6b7d1cb5e53547d06529b938852aa
7
- data.tar.gz: bc3e117de5d7d45e21c28259fc2f1876b6153c08555162f851d172a7ee5be0976a33ca373bf9f1babe78aaf25b641cdde083c36fc3c73d8eb2f3ee5f8a883df4
6
+ metadata.gz: 68e7a0da58af1e2ab190e92886f61dfbe6df99f6be2f09c049b9db13d01f1b66c7dac1d348ea407e8bf138cb43d1dea5f631fe18587556f56ac828a3ee40024a
7
+ data.tar.gz: 8cc810b02ee3e004064d6e1fb0e9e3b896ab3567b1d6d3a0b46fbbff96a6723b756dad38ed991d739cb280a6017b64935d60dbed12245496edcd03ba0df79e3e
@@ -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 = Hash.new
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
- if origin == ''
51
- error = 1
52
- error_message['origin'] = 'origin tidak boleh kosong'
53
- end
54
- if destination == ''
55
- error = 1
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
 
@@ -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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'rajaongkir'
3
- s.version = '0.2'
3
+ s.version = '0.2.1'
4
4
  s.summary = "Simple class untuk API rajaongkir.com"
5
5
  s.description = "Simple class ruby untuk API rajaongkir.com"
6
6
  s.authors = ["Harry Yunanto"]
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: '0.2'
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-20 00:00:00.000000000 Z
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