hkpost_api 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/hkpost_api.gemspec +24 -0
- data/lib/hkpost_api/version.rb +3 -0
- data/lib/hkpost_api.rb +62 -0
- data/spec/quote_spec.rb +39 -0
- data/spec/spec_helper.rb +19 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 668e31e674c8bbb00c0294dfe9eeb110de8640aa
|
4
|
+
data.tar.gz: f720afcbdd2f7fa574a5d67a051dbe01965348ec
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 63d4ecfa8eabc657a04a9e631a99cbec4241214aab2bb6a99e985f7753dd0233f139cedc73361b2ceeb78b46ef4a1d57b46842e9ef740ec7454ceb111ec746a4
|
7
|
+
data.tar.gz: fa887988c2205064fb4f715377a46c0ad79c206c97b3204174b79197676024bbb74b8544409bc367d18caecc48dac9dec5816713a0b2c8acf17f1ff3a8a3f023
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 William Yeung
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# HkpostApi
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'hkpost_api'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install hkpost_api
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/hkpost_api.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hkpost_api/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "hkpost_api"
|
8
|
+
gem.version = HkpostApi::VERSION
|
9
|
+
gem.authors = ["William Yeung"]
|
10
|
+
gem.email = ["william@tofugear.com"]
|
11
|
+
gem.description = %q{Hong Kong Post Postage Rates Enquiry}
|
12
|
+
gem.summary = %q{Hong Kong Post Postage Rates Enquiry}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "nokogiri"
|
21
|
+
gem.add_development_dependency "rspec"
|
22
|
+
|
23
|
+
|
24
|
+
end
|
data/lib/hkpost_api.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require "hkpost_api/version"
|
2
|
+
require 'net/http'
|
3
|
+
require 'nokogiri'
|
4
|
+
|
5
|
+
module HkpostApi
|
6
|
+
# Your code goes here...
|
7
|
+
class Quote
|
8
|
+
def initialize(endpoint=nil)
|
9
|
+
@endpoint=endpoint
|
10
|
+
@endpoint||="http://app1.hongkongpost.hk/"
|
11
|
+
@endpoint_uri=URI(@endpoint)
|
12
|
+
end
|
13
|
+
|
14
|
+
def form_html
|
15
|
+
@form_html||=Nokogiri::HTML(Net::HTTP.get(@endpoint_uri.host, "/calc/eng/overseas/step1.php"))
|
16
|
+
end
|
17
|
+
|
18
|
+
def get_shipment_types
|
19
|
+
return form_html.css('form[name="overseas_mail"] input[name="mail"]').collect {|n| n['value'] }
|
20
|
+
end
|
21
|
+
|
22
|
+
def get_quote(mail_type, weight_in_gram, country_name)
|
23
|
+
country_code=get_country_list[country_name]
|
24
|
+
raise ArgumentError.new("Invalid country name #{country_name}") if country_code.nil?
|
25
|
+
res=Net::HTTP.post_form(URI("#{@endpoint}/calc/eng/overseas/step2.php"), 'destination'=>country_code, 'mail'=>mail_type, 'weight'=>weight_in_gram)
|
26
|
+
res_html=Nokogiri::HTML(res.body)
|
27
|
+
|
28
|
+
rates={}
|
29
|
+
rates[:normal]=parse_rate_table(res_html, "Form of Delivery (Normal)")
|
30
|
+
|
31
|
+
rates[:speedpost]=parse_rate_table(res_html, "Form of Delivery (Speedpost)")
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
return rates
|
36
|
+
end
|
37
|
+
|
38
|
+
def parse_rate_table(res_html, title)
|
39
|
+
els=res_html.search "[text()*='#{title}']"
|
40
|
+
table=els.first.ancestors('table').first
|
41
|
+
rows=table.css('tr')
|
42
|
+
result = {}
|
43
|
+
rows[1..rows.length-1].each do |row|
|
44
|
+
cells=row.css('td')
|
45
|
+
result[cells[1].text.strip]=cells.last.text.strip
|
46
|
+
end
|
47
|
+
|
48
|
+
return result
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
def get_country_list
|
53
|
+
@country_list||=form_html.css('form[name="overseas_mail"] select[name="destination"] option').inject({}) {|result, n| result[n.text]=n['value'] unless n['value']=='0'; result}
|
54
|
+
return @country_list
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
def self.default_instance
|
59
|
+
@default_instance=Quote.new()
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/spec/quote_spec.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HkpostApi::Quote do
|
4
|
+
subject {HkpostApi::Quote.default_instance}
|
5
|
+
|
6
|
+
it "should be able to create default instance" do
|
7
|
+
subject.should_not be_nil
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:country_list) {subject.get_country_list}
|
11
|
+
|
12
|
+
it "should be able to fetch country list" do
|
13
|
+
country_list.should be_kind_of(Hash)
|
14
|
+
country_list.length.should > 0
|
15
|
+
end
|
16
|
+
|
17
|
+
let(:shipment_types) {subject.get_shipment_types}
|
18
|
+
it "should be able to fetch shipment type" do
|
19
|
+
shipment_types.should be_kind_of(Array)
|
20
|
+
shipment_types.length.should eq(4)
|
21
|
+
end
|
22
|
+
|
23
|
+
let(:quote_result) {subject.get_quote("parcel", 5000, subject.get_country_list.keys.first)}
|
24
|
+
it "should return shipment result" do
|
25
|
+
quote_result.should be_kind_of(Hash)
|
26
|
+
pp quote_result
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should fail with proper error message if country not found" do
|
30
|
+
begin
|
31
|
+
subject.get_quote("parcel", 5000, "ussr")
|
32
|
+
rescue ArgumentError=>e
|
33
|
+
e.message.should match(/Invalid country name/)
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'hkpost_api'
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hkpost_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- William Yeung
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nokogiri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Hong Kong Post Postage Rates Enquiry
|
42
|
+
email:
|
43
|
+
- william@tofugear.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- .rspec
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- hkpost_api.gemspec
|
55
|
+
- lib/hkpost_api.rb
|
56
|
+
- lib/hkpost_api/version.rb
|
57
|
+
- spec/quote_spec.rb
|
58
|
+
- spec/spec_helper.rb
|
59
|
+
homepage: ''
|
60
|
+
licenses: []
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.1.11
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Hong Kong Post Postage Rates Enquiry
|
82
|
+
test_files:
|
83
|
+
- spec/quote_spec.rb
|
84
|
+
- spec/spec_helper.rb
|