raja_ongkir 0.0.0 → 0.0.2

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
- SHA1:
3
- metadata.gz: c14afe8d4400f955bb49b662569dc144d5577c6a
4
- data.tar.gz: c6895140d4b17106cf6560d29c19a2c27de1a785
2
+ SHA256:
3
+ metadata.gz: b25b66ca2a77d0597cd6a49973fe637a5cb179c48a7cb035fd13f85ef7c85374
4
+ data.tar.gz: e8b47dfef5a9915ea72cbe6e9704baf54004d434ffd58780db041c03946b4209
5
5
  SHA512:
6
- metadata.gz: 69e51b900a91cb29aa315b65311dc0062202aa5d77a7f8fceb8d0a9f805f010d431f0ca57c5e0390d9800b84693fd322b7e42359a6580ecb96b36564cc7aac01
7
- data.tar.gz: 9a978e355364ae41031086b9300290111f1c2615239654bd54c3ed82f6de18d7431b8193c8c3310a6860b8556ba8312eb08237ffbf4e9e8c9fa433ebdefb2ed2
6
+ metadata.gz: 4bad741c8c4a8ef593add25e1b5f52a34d319108fedc339477c962a38dd037ac7f7d919efb68de0cbf4015c00496266f8a8a91a80b9a79bb5fdbc32fb4bc7941
7
+ data.tar.gz: 9bcb3e8883c913011747d98bd473bcada4fc17def2a1d6e85dd8823d6a2d56b55db14afba51151ee03dd3966a2893ae22ee172bb4a0ccfb6ae6a66309d865f5d
@@ -0,0 +1 @@
1
+ service_name: travis-ci
@@ -1,5 +1,12 @@
1
- sudo: false
2
1
  language: ruby
3
- rvm:
4
- - 2.0.0
5
- before_install: gem install bundler -v 1.13.6
2
+ notifications:
3
+ email:
4
+ - siriusmdely@gmail.com
5
+ deploy:
6
+ provider: rubygems
7
+ api_key:
8
+ secure: kuvEbqWeuDC4J6QGvvzkxc0HhpZNCFB8KN9ybd3SRblz2zgIIdIM9vhMwsk1cqDQnD2kAT1OKEM/bLOrcOGl7OV4IHExhL7ZgQeIzzDhanOaMjkIufRB2IC2Onjdnxvz90P+ZXae6nOUJkdvf31aR8q186iRTfJvP+iYgWQ63jH4mgOySwl1ZE1c56Wt0YTpaeUY1k/QuweYzVML8yg+bA4xyzqWS/grXfJL2bMQ1qHgVEDG3G1zPQF/hpf4CdxjUrsLPa+0UzazBlKhjkTp5arCgaLopK6fR3vGt8lFEq0aBywKqR+qSIIE8ejtWsnHV94kCyWaqKaYRpS6en088jTP0DZUXB5qLCfg9l5dMtuOGKem5YdYFxvoHUB6X5NBizhW4pyf/R66EYsRjmLFlyCUduElTi8nEoK/AbxQMkhF5XmNJMIKCFgwuf/sjDFCFvowKMAR0X4x5YbbGC6vGVZGmK9D8/xymo8rPwl2p8nSrSo7pKdsg57q/T+TobGhxu3rewCo7y61LFSEeXZ9NDSisiKFyUSATfxKVI+JMFLLFEHga/L1BWb1I2YQEq1Lo1mDwV3qDIOOG5XMqBo3hU22doT+kUR7DXQ/lgFUz6gutFbnusqrkUNuTQj6fzCzaogyJ8EsDkMVLGjPoJ6XvJWnnlhHn7AMshp+B8eHxRo=
9
+ gem: raja_ongkir
10
+ on:
11
+ tags: true
12
+ repo: siriusdely/raja_ongkir
data/README.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # RajaOngkir
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/raja_ongkir.svg)](https://badge.fury.io/rb/raja_ongkir)
4
+ [![Build Status](https://travis-ci.org/siriusdely/raja_ongkir.svg?branch=master)](https://travis-ci.org/siriusdely/raja_ongkir)
5
+ [![Coverage Status](https://coveralls.io/repos/github/siriusdely/raja_ongkir/badge.svg?branch=master)](https://coveralls.io/github/siriusdely/raja_ongkir?branch=master)
6
+ [![Maintainability](https://api.codeclimate.com/v1/badges/0d3c042cf7cdf145a25d/maintainability)](https://codeclimate.com/github/siriusdely/raja_ongkir/maintainability)
7
+
3
8
  Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/raja_ongkir`. To experiment with that code, run `bin/console` for an interactive prompt.
4
9
 
5
10
  TODO: Delete this and the text above, and describe your gem
@@ -32,7 +37,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
37
 
33
38
  ## Contributing
34
39
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/raja_ongkir. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/siriusdely/raja_ongkir. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
41
 
37
42
 
38
43
  ## License
data/Rakefile CHANGED
@@ -1,10 +1,13 @@
1
- require "bundler/gem_tasks"
2
- require "rake/testtask"
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+ require 'rubocop/rake_task'
3
4
 
4
5
  Rake::TestTask.new(:test) do |t|
5
- t.libs << "test"
6
- t.libs << "lib"
7
- t.test_files = FileList['test/**/*_test.rb']
6
+ t.libs << 'test'
7
+ t.libs << 'lib'
8
+ t.test_files = FileList['test/**/*_test.rb', 'test/**/*_spec.rb']
8
9
  end
9
10
 
10
- task :default => :test
11
+ RuboCop::RakeTask.new
12
+
13
+ task default: %i[test rubocop]
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "raja_ongkir"
3
+ require 'bundler/setup'
4
+ require 'raja_ongkir'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "raja_ongkir"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start
@@ -1,20 +1,23 @@
1
- require "httparty"
2
- require "net/http"
3
- require "openssl"
4
- require "raja_ongkir/version"
5
- require "uri"
1
+ require 'httparty'
2
+ require 'net/http'
3
+ require 'openssl'
4
+ require 'raja_ongkir/helper'
5
+ require 'raja_ongkir/version'
6
+ require 'uri'
6
7
 
7
8
  module RajaOngkir
8
- BASE_URL = "https://api.rajaongkir.com"
9
+ BASE_URL = 'https://api.rajaongkir.com'.freeze
9
10
 
11
+ ## Client for rajaongkir.com
12
+ #
10
13
  class Client
11
14
  include HTTParty
12
15
 
13
16
  base_uri BASE_URL
14
17
 
15
- def initialize(account_type = "starter", api_key)
18
+ def initialize(api_key, account_type = 'starter')
16
19
  @account_type = account_type
17
- @api_key = api_key
20
+ Helper.api_key = api_key
18
21
  end
19
22
 
20
23
  def hi
@@ -25,57 +28,55 @@ module RajaOngkir
25
28
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
26
29
 
27
30
  request = Net::HTTP::Get.new(url)
28
- request["key"] = @api_key
31
+ request['key'] = @api_key
29
32
 
30
33
  response = http.request(request)
31
34
  puts response.read_body
32
35
  end
33
36
 
34
37
  def provinces(q = nil, options = {})
35
- if (options.length > 0 and not q.nil? and not q.is_a?(String)) or
36
- (options.length <= 0 and not q.nil? and not q.is_a?(Hash))
37
- raise "Provided q must be an String"
38
- end
39
- if q.is_a?(Hash)
40
- options = options.merge(q)
41
- q = nil
42
- end
43
- reload = options[:reload]
44
- if reload or not @provinces
45
- p "reload"
38
+ q, options = Helper.sanitize_params q, options
39
+ if @provinces.nil? || options[:reload]
46
40
  # query = id ? build_query(id: id) : build_query
47
- response = self.class.get("/#{@account_type}/province", build_query)
48
- status = response["rajaongkir"]["status"]
49
- unless status["code"] >= 200 and status["code"] < 300
50
- raise "#{status["description"]} CODE #{status["code"]}"
51
- else
52
- @provinces = response["rajaongkir"]["results"]
53
- end
54
- end
55
- unless q and q.is_a?(String)
56
- @provinces
57
- else
58
- @provinces.select{ |p| p["province"].downcase =~ /#{Regexp.quote(q.downcase)}/ }
41
+ response = self.class.get("/#{@account_type}/province",
42
+ Helper.build_query)
43
+ @provinces = Helper.items_from_resp response
59
44
  end
45
+ return Helper.filter_by_keyword @provinces, q if q && q.is_a?(String)
46
+ @provinces
60
47
  end
61
48
 
62
49
  def province(id, options = {})
63
- unless id.is_a?(Integer)
64
- raise "Provided id must be an Integer."
65
- end
50
+ raise 'Provided id must be an Integer.' unless id.is_a?(Integer)
66
51
  @provinces = provinces(options)
67
- @provinces.find{ |p| p["province_id"] == id.to_s }
52
+ @provinces.find { |p| p['province_id'] == id.to_s }
68
53
  end
69
54
 
70
- def cities
71
- response = self.class.get("/#{@account_type}/city", headers: {"key" => @api_key})
55
+ def cities(q = nil, options = {})
56
+ q, options = Helper.sanitize_params q, options
57
+ reload = options[:reload]
58
+ if @cities.nil? || reload
59
+ response = self.class.get("/#{@account_type}/city", Helper.build_query)
60
+ @cities = Helper.items_from_resp response
61
+ end
62
+ return Helper.filter_by_keyword @cities, q if q && q.is_a?(String)
63
+ @cities
72
64
  end
73
65
 
74
- private
66
+ def city(id, options = {})
67
+ raise 'Provided id must be an Integer.' unless id.is_a?(Integer)
68
+ @cities = cities(options)
69
+ @cities.find { |c| c['city_id'] == id.to_s }
70
+ end
75
71
 
76
- def build_query query={}
77
- query ||= {}
78
- {query: query, headers: {"key" => @api_key}}
72
+ def costs(origin_id, destination_id, grams, courier = 'jne')
73
+ origin_id, destination_id, grams, courier =
74
+ Helper.sanitize_costs_params origin_id, destination_id, grams, courier
75
+ response = self.class.post(
76
+ "/#{@account_type}/cost",
77
+ Helper.build_costs_query(origin_id, destination_id, grams, courier)
78
+ )
79
+ @costs = Helper.items_from_resp response
79
80
  end
80
81
  end
81
82
  end
@@ -0,0 +1,66 @@
1
+ module RajaOngkir
2
+ ## Helper for RajaOngkir::Client
3
+ #
4
+ class Helper
5
+ def self.sanitize_costs_params(origin_id, destination_id, grams, courier)
6
+ origin_id = origin_id.to_i
7
+ destination_id = destination_id.to_i
8
+ grams = grams.to_i
9
+ raise 'origin_id, destination_id, and grams must be a positive Integer' if
10
+ origin_id <= 0 || destination_id <= 0 || grams <= 0
11
+ [origin_id, destination_id, grams, courier]
12
+ end
13
+
14
+ def self.build_costs_query(origin_id, destination_id, grams, courier)
15
+ {
16
+ headers: {
17
+ 'key' => api_key,
18
+ 'content-type' => 'application/x-www-form-urlencoded'
19
+ },
20
+ body: "origin=#{origin_id}&destination=#{destination_id}&" \
21
+ "weight=#{grams}&courier=#{courier}"
22
+ }
23
+ end
24
+
25
+ # rubocop:disable Metrics/AbcSize
26
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
27
+ def self.sanitize_params(q, options)
28
+ return [nil, options.merge(q)] if
29
+ options.length <= 0 && q && q.is_a?(Hash)
30
+ raise 'Provided q must be a String' if
31
+ (options.length <= 0 && q && !q.is_a?(String)) ||
32
+ (!options.empty? && !q.nil? && !q.is_a?(String))
33
+
34
+ [q, options]
35
+ end
36
+ # rubocop:enable all
37
+
38
+ class << self
39
+ attr_accessor :api_key
40
+
41
+ def build_query(query = {})
42
+ query ||= {}
43
+ { query: query, headers: { 'key' => api_key } }
44
+ end
45
+
46
+ def items_from_resp(response)
47
+ status = response['rajaongkir']['status']
48
+ items = response['rajaongkir']['results']
49
+ return items if status['code'] >= 200 && status['code'] < 300
50
+ raise "#{status['description']} CODE #{status['code']}"
51
+ end
52
+
53
+ def filter_by_keyword(items, q)
54
+ item = items.first
55
+ if item['city_name']
56
+ key = 'city_name'
57
+ elsif item['province']
58
+ key = 'province'
59
+ end
60
+ items.select do |p|
61
+ p[key].downcase =~ /#{Regexp.quote(q.downcase)}/
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -1,3 +1,3 @@
1
1
  module RajaOngkir
2
- VERSION = "0.0.0"
2
+ VERSION = '0.0.2'.freeze
3
3
  end
@@ -1,37 +1,61 @@
1
- # coding: utf-8
2
1
  lib = File.expand_path('../lib', __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'raja_ongkir/version'
5
4
 
5
+ # class Gem::Specification
6
+ # def check_allowed_push_host
7
+ # # Prevent pushing this gem to RubyGems.org.
8
+ # # To allow pushes either set the 'allowed_push_host'
9
+ # # to allow pushing to a single host or delete this section
10
+ # # to allow pushing to any host.
11
+ # if respond_to?(:metadata)
12
+ # metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
13
+ # else
14
+ # raise 'RubyGems 2.0 or newer is required to protect against ' \
15
+ # 'public gem pushes.'
16
+ # end
17
+ # end
18
+ # end
19
+
20
+ Gem::Specification.class_exec do
21
+ def check_allowed_push_host
22
+ # Prevent pushing this gem to RubyGems.org.
23
+ # To allow pushes either set the 'allowed_push_host'
24
+ # to allow pushing to a single host or delete this section
25
+ # to allow pushing to any host.
26
+ if respond_to?(:metadata)
27
+ metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
28
+ else
29
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
30
+ 'public gem pushes.'
31
+ end
32
+ end
33
+ end
34
+
6
35
  Gem::Specification.new do |spec|
7
- spec.name = "raja_ongkir"
36
+ spec.name = 'raja_ongkir'
8
37
  spec.version = RajaOngkir::VERSION
9
- spec.authors = ["Sirius Dely"]
10
- spec.email = ["siriusdely@icloud.com"]
38
+ spec.authors = ['Sirius Dely']
39
+ spec.email = ['siriusdely@icloud.com']
11
40
 
12
- spec.summary = %q{Client for rajaongkir.com}
13
- spec.description = %q{Client for rajaongkir.com}
14
- spec.homepage = "https://github.com/siriusdely/raja-ongkir"
15
- spec.license = "MIT"
41
+ spec.summary = 'Client for rajaongkir.com'
42
+ spec.description = 'Client for rajaongkir.com'
43
+ spec.homepage = 'https://github.com/siriusdely/raja_ongkir'
44
+ spec.license = 'MIT'
16
45
 
17
- # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
- # to allow pushing to a single host or delete this section to allow pushing to any host.
19
- if spec.respond_to?(:metadata)
20
- spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
- else
22
- raise "RubyGems 2.0 or newer is required to protect against " \
23
- "public gem pushes."
24
- end
46
+ spec.check_allowed_push_host
25
47
 
26
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
48
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
49
  f.match(%r{^(test|spec|features)/})
28
50
  end
29
- spec.bindir = "exe"
51
+ spec.bindir = 'exe'
30
52
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
- spec.require_paths = ["lib"]
53
+ spec.require_paths = ['lib']
32
54
 
33
- spec.add_development_dependency "bundler", "~> 1.13"
34
- spec.add_development_dependency "rake", "~> 10.0"
35
- spec.add_development_dependency "minitest", "~> 5.0"
36
- spec.add_runtime_dependency "httparty", "~> 0.15"
55
+ spec.add_development_dependency 'bundler', '~> 1.13'
56
+ spec.add_development_dependency 'coveralls', '~> 0.8'
57
+ spec.add_development_dependency 'minitest', '~> 5.0'
58
+ spec.add_development_dependency 'rake', '~> 10.0'
59
+ spec.add_development_dependency 'rubocop', '~> 0.49'
60
+ spec.add_runtime_dependency 'httparty', '~> 0.15'
37
61
  end
metadata CHANGED
@@ -1,69 +1,97 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: raja_ongkir
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sirius Dely
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-22 00:00:00.000000000 Z
11
+ date: 2017-12-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.13'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.13'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: coveralls
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '0.8'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '0.8'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '5.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.49'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.49'
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: httparty
57
85
  requirement: !ruby/object:Gem::Requirement
58
86
  requirements:
59
- - - ~>
87
+ - - "~>"
60
88
  - !ruby/object:Gem::Version
61
89
  version: '0.15'
62
90
  type: :runtime
63
91
  prerelease: false
64
92
  version_requirements: !ruby/object:Gem::Requirement
65
93
  requirements:
66
- - - ~>
94
+ - - "~>"
67
95
  - !ruby/object:Gem::Version
68
96
  version: '0.15'
69
97
  description: Client for rajaongkir.com
@@ -73,8 +101,9 @@ executables: []
73
101
  extensions: []
74
102
  extra_rdoc_files: []
75
103
  files:
76
- - .gitignore
77
- - .travis.yml
104
+ - ".coveralls.yml"
105
+ - ".gitignore"
106
+ - ".travis.yml"
78
107
  - CODE_OF_CONDUCT.md
79
108
  - Gemfile
80
109
  - LICENSE.txt
@@ -83,9 +112,10 @@ files:
83
112
  - bin/console
84
113
  - bin/setup
85
114
  - lib/raja_ongkir.rb
115
+ - lib/raja_ongkir/helper.rb
86
116
  - lib/raja_ongkir/version.rb
87
117
  - raja_ongkir.gemspec
88
- homepage: https://github.com/siriusdely/raja-ongkir
118
+ homepage: https://github.com/siriusdely/raja_ongkir
89
119
  licenses:
90
120
  - MIT
91
121
  metadata:
@@ -96,17 +126,17 @@ require_paths:
96
126
  - lib
97
127
  required_ruby_version: !ruby/object:Gem::Requirement
98
128
  requirements:
99
- - - '>='
129
+ - - ">="
100
130
  - !ruby/object:Gem::Version
101
131
  version: '0'
102
132
  required_rubygems_version: !ruby/object:Gem::Requirement
103
133
  requirements:
104
- - - '>='
134
+ - - ">="
105
135
  - !ruby/object:Gem::Version
106
136
  version: '0'
107
137
  requirements: []
108
138
  rubyforge_project:
109
- rubygems_version: 2.0.14
139
+ rubygems_version: 2.7.4
110
140
  signing_key:
111
141
  specification_version: 4
112
142
  summary: Client for rajaongkir.com