juhe_ruby 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 684f19c825860c8b271bdc072aa07967d537ee94
4
- data.tar.gz: f56c7b9b7eb8448ac3128ed8fd5df7ccd5aa6a29
3
+ metadata.gz: f855a236ca62e1b5eeedbb1143f34a178d5a9ba2
4
+ data.tar.gz: 58a0ddce2ca3a2e579b0c7dfd2ca39a341aa1413
5
5
  SHA512:
6
- metadata.gz: 74bdf713ce1a3f79e9adfd72c87fd564359e231ef1e9063da027323df058b19f2dd93e34c2b2837a730830c55732ee76e4dd0d800988860e6ac9c57410aafb35
7
- data.tar.gz: 2c3e65b1dc430ee01019c606394eea931aca858c2f1252230ccbdb898176cbb3e3438cc3e528c9905c645b40a51c737e8ad62b25626296a5bc1ea855108b2584
6
+ metadata.gz: 49c01ea189dc1fff7b6f287773276671da002704bfb652556410df3c7848873031947d010220316303f694774cad464ff7a5121fa612d1fafcba236f2916af41
7
+ data.tar.gz: 590f39e638ff6d43640537a9dbe721dd60be296cbf26823322a105346d2e545cb35074ac678b6d99bfd18acfb07d23fd9ae64c247e616084a16150dcf7e6e2c6
data/README.md CHANGED
@@ -29,15 +29,15 @@ Juhe::Express.search("顺丰", "575677355677")
29
29
  ```
30
30
  或者直接将app_key作为参数:
31
31
  ```ruby
32
- Juhe::Express.search("顺丰", "575677355677", "d85fa433fb8f30419dc1b3697b035b3d")
32
+ Juhe::Express.search("顺丰", "575677355677", app_key: "d85fa433fb8f30419dc1b3697b035b3d")
33
33
  ```
34
34
  返回Hash数组:
35
35
  ```ruby
36
36
  [
37
37
  {
38
- "datetime":"2013-06-25 10:44:05",# 时间
39
- "remark":"已收件",# 描述
40
- "zone":"台州市"# 区域
38
+ "datetime":"2013-06-25 10:44:05", # 时间
39
+ "remark":"已收件", # 描述
40
+ "zone":"台州市" # 区域
41
41
  },
42
42
  {
43
43
  "datetime":"2013-06-25 11:05:21",
@@ -81,3 +81,10 @@ Juhe::Express.search("顺丰", "575677355677", "d85fa433fb8f30419dc1b3697b035b3d
81
81
  }
82
82
  ]
83
83
  ```
84
+ 程序第一次运行会获取快递公司列表,如果想手动刷新快递公司信息:
85
+ ```ruby
86
+ Juhe.app_key = "d85fa433fb8f30419dc1b3697b035b3d"
87
+ Juhe::Express.refresh_companies
88
+ # or
89
+ Juhe::Express.refresh_companies(app_key: "d85fa433fb8f30419dc1b3697b035b3d")
90
+ ```
@@ -10,16 +10,25 @@ module Juhe
10
10
  base.extend ClassMethods
11
11
  end
12
12
 
13
+ class << self
14
+ attr_accessor :companies
15
+ end
16
+
13
17
  module ClassMethods
14
- def company_code_of(company_name, app_key = nil)
15
- app_key ||= Juhe.app_key
16
- result = JSON.parse(open(BASE_URL+"?key="+app_key).read)
17
- raise result["reason"] if result["resultcode"] != "200"
18
+ def company_code_of(company_name, options = nil)
19
+ refresh_companies(options) if @companies.nil?
18
20
 
19
- result["result"].each do |company|
21
+ @companies.each do |company|
20
22
  return company["no"] if company["com"] == company_name
21
23
  end
22
24
  end
25
+
26
+ def refresh_companies(options)
27
+ app_key = (options[:app_key] if options) || Juhe.app_key
28
+ result = JSON.parse(open(BASE_URL+"?key="+app_key).read)
29
+ raise result["reason"] if result["resultcode"] != "200"
30
+ @companies = result["result"]
31
+ end
23
32
  end
24
33
  end
25
34
 
@@ -28,19 +37,17 @@ module Juhe
28
37
 
29
38
  BASE_URL = "http://v.juhe.cn/exp/index"
30
39
 
31
- def self.search(company_name, number, app_key = nil)
32
- app_key ||= Juhe.app_key
40
+ def self.search(company_name, number, options = nil)
41
+ app_key = (options[:app_key] if options) || Juhe.app_key
33
42
  url = BASE_URL \
34
43
  + "?key=" \
35
44
  + app_key \
36
45
  + "&no=" + number \
37
- + "&com=" + company_code_of(company_name, app_key)
46
+ + "&com=" + company_code_of(company_name, options)
38
47
 
39
48
  result = JSON.parse(open(url).read)
40
- raise result["reason"].to_s if result["resultcode"] != "200"
49
+ raise result["reason"] if result["resultcode"] != "200"
41
50
  result["result"]
42
51
  end
43
52
  end
44
-
45
-
46
53
  end
@@ -1,3 +1,3 @@
1
1
  module JuheRuby
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,30 @@
1
+ require "test_helper"
2
+
3
+ class Juhe::ExpressTest < Test::Unit::TestCase
4
+ def setup
5
+ Juhe.app_key = "d85fa433fd8f30419dc1b3697b035b3d"
6
+ end
7
+
8
+ def test_app_key
9
+ Juhe.app_key = "123456"
10
+ assert_equal(Juhe.app_key, "123456")
11
+ end
12
+
13
+ def test_company_of_name
14
+ assert_equal(Juhe::Express.company_code_of("申通"), "sto")
15
+ end
16
+
17
+ def test_search_without_app_key
18
+ assert_equal(Juhe::Express.search("顺丰", "575677355677")["com"], "sf")
19
+ end
20
+
21
+ def test_search_with_app_key
22
+ Juhe.app_key = ""
23
+ result = Juhe::Express.search(
24
+ "顺丰",
25
+ "575677355677",
26
+ app_key: "d85fa433fd8f30419dc1b3697b035b3d"
27
+ )
28
+ assert_equal(result["com"], "sf")
29
+ end
30
+ end
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require "juhe_ruby"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: juhe_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - linuxnerd
@@ -54,6 +54,8 @@ files:
54
54
  - lib/juhe_ruby.rb
55
55
  - lib/juhe_ruby/express.rb
56
56
  - lib/juhe_ruby/version.rb
57
+ - test/express/express_test.rb
58
+ - test/test_helper.rb
57
59
  homepage: https://github.com/linuxnerd/juhe_ruby
58
60
  licenses:
59
61
  - MIT
@@ -78,4 +80,6 @@ rubygems_version: 2.1.10
78
80
  signing_key:
79
81
  specification_version: 4
80
82
  summary: '"聚合ruby api"'
81
- test_files: []
83
+ test_files:
84
+ - test/express/express_test.rb
85
+ - test/test_helper.rb