juhe_ruby 0.0.2 → 0.0.3

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: f855a236ca62e1b5eeedbb1143f34a178d5a9ba2
4
- data.tar.gz: 58a0ddce2ca3a2e579b0c7dfd2ca39a341aa1413
3
+ metadata.gz: 7efe5f04f612bc16d7e09e65cb6f2fb167a276f4
4
+ data.tar.gz: 3db3d59dfb27c9f4e02454df623567019b42db1d
5
5
  SHA512:
6
- metadata.gz: 49c01ea189dc1fff7b6f287773276671da002704bfb652556410df3c7848873031947d010220316303f694774cad464ff7a5121fa612d1fafcba236f2916af41
7
- data.tar.gz: 590f39e638ff6d43640537a9dbe721dd60be296cbf26823322a105346d2e545cb35074ac678b6d99bfd18acfb07d23fd9ae64c247e616084a16150dcf7e6e2c6
6
+ metadata.gz: 9ab3b545c5e844313864761184e085ec9308ee388a5f6644e7e10bc57257c70ab3f07d82199fde37fe8fc26b0c693c3a983fcae23cbf4584b1e989daf18f671f
7
+ data.tar.gz: 0af455ab11fa6bf67829b38a484782496113b883624ac6b403c77158ca6ef57f631e3c728f311f48af5a0c40f4efa2cd37b694bcd47f490287a6ff0a3f8c2d6c
data/README.md CHANGED
@@ -22,9 +22,11 @@ $ gem install juhe_ruby
22
22
  ## 用法
23
23
 
24
24
  ### 常用快递查询
25
- 为app_key一次赋值,多次查询
25
+ 目前支持的快递公司,`%w[顺丰 申通 圆通 韵达 天天 EMS 中通 汇通]`详细的请在[聚合页面](http://www.juhe.cn/docs/api/id/43/aid/103)上查询。api在第一次查询的时候也会调用支持快递公司查询借口。
26
+
27
+ 具体用法为express_app_key一次赋值,多次查询:
26
28
  ```ruby
27
- Juhe.app_key = "d85fa433fb8f30419dc1b3697b035b3d" # 注册时,聚合提供的app_key
29
+ Juhe.express_app_key = "d85fa433fb8f30419dc1b3697b035b3d" # 注册时,聚合提供的app_key
28
30
  Juhe::Express.search("顺丰", "575677355677")
29
31
  ```
30
32
  或者直接将app_key作为参数:
@@ -83,7 +85,7 @@ Juhe::Express.search("顺丰", "575677355677", app_key: "d85fa433fb8f30419dc1b36
83
85
  ```
84
86
  程序第一次运行会获取快递公司列表,如果想手动刷新快递公司信息:
85
87
  ```ruby
86
- Juhe.app_key = "d85fa433fb8f30419dc1b3697b035b3d"
88
+ Juhe.express_app_key = "d85fa433fb8f30419dc1b3697b035b3d"
87
89
  Juhe::Express.refresh_companies
88
90
  # or
89
91
  Juhe::Express.refresh_companies(app_key: "d85fa433fb8f30419dc1b3697b035b3d")
@@ -2,45 +2,17 @@ require "open-uri"
2
2
  require "json"
3
3
 
4
4
  module Juhe
5
-
6
- module ExpressCompany
7
- BASE_URL = "http://v.juhe.cn/exp/com"
8
-
9
- def self.included(base)
10
- base.extend ClassMethods
11
- end
5
+ module Express
6
+ BASE_URL = "http://v.juhe.cn/exp"
12
7
 
13
8
  class << self
14
9
  attr_accessor :companies
15
10
  end
16
11
 
17
- module ClassMethods
18
- def company_code_of(company_name, options = nil)
19
- refresh_companies(options) if @companies.nil?
20
-
21
- @companies.each do |company|
22
- return company["no"] if company["com"] == company_name
23
- end
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
32
- end
33
- end
34
-
35
- module Express
36
- include ExpressCompany
37
-
38
- BASE_URL = "http://v.juhe.cn/exp/index"
39
-
40
12
  def self.search(company_name, number, options = nil)
41
- app_key = (options[:app_key] if options) || Juhe.app_key
13
+ app_key = (options[:app_key] if options) || Juhe.express_app_key
42
14
  url = BASE_URL \
43
- + "?key=" \
15
+ + "/index?key=" \
44
16
  + app_key \
45
17
  + "&no=" + number \
46
18
  + "&com=" + company_code_of(company_name, options)
@@ -49,5 +21,20 @@ module Juhe
49
21
  raise result["reason"] if result["resultcode"] != "200"
50
22
  result["result"]
51
23
  end
24
+
25
+ def self.company_code_of(company_name, options = nil)
26
+ refresh_companies(options) if @companies.nil?
27
+
28
+ @companies.each do |company|
29
+ return company["no"] if company["com"] == company_name
30
+ end
31
+ end
32
+
33
+ def self.refresh_companies(options)
34
+ app_key = (options[:app_key] if options) || Juhe.express_app_key
35
+ result = JSON.parse(open(BASE_URL+"/com?key="+app_key).read)
36
+ raise result["reason"] if result["resultcode"] != "200"
37
+ @companies = result["result"]
38
+ end
52
39
  end
53
40
  end
@@ -1,3 +1,3 @@
1
1
  module JuheRuby
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/juhe_ruby.rb CHANGED
@@ -3,6 +3,11 @@ require "juhe_ruby/express"
3
3
 
4
4
  module Juhe
5
5
  class << self
6
- attr_accessor :app_key
6
+ modules = Juhe.constants.map{ |el| Juhe.const_get(el) }.select{ |m| m.instance_of?(Module) }
7
+ modules.each do |mod|
8
+ if mod.to_s =~ /\AJuhe::(.*)/
9
+ attr_accessor "#{$1.downcase}_app_key".to_sym
10
+ end
11
+ end
7
12
  end
8
13
  end
@@ -2,12 +2,12 @@ require "test_helper"
2
2
 
3
3
  class Juhe::ExpressTest < Test::Unit::TestCase
4
4
  def setup
5
- Juhe.app_key = "d85fa433fd8f30419dc1b3697b035b3d"
5
+ Juhe.express_app_key = "d85fa433fd8f30419dc1b3697b035b3d"
6
6
  end
7
7
 
8
8
  def test_app_key
9
- Juhe.app_key = "123456"
10
- assert_equal(Juhe.app_key, "123456")
9
+ Juhe.express_app_key = "123456"
10
+ assert_equal(Juhe.express_app_key, "123456")
11
11
  end
12
12
 
13
13
  def test_company_of_name
@@ -19,7 +19,7 @@ class Juhe::ExpressTest < Test::Unit::TestCase
19
19
  end
20
20
 
21
21
  def test_search_with_app_key
22
- Juhe.app_key = ""
22
+ Juhe.express_app_key = ""
23
23
  result = Juhe::Express.search(
24
24
  "顺丰",
25
25
  "575677355677",
@@ -27,4 +27,11 @@ class Juhe::ExpressTest < Test::Unit::TestCase
27
27
  )
28
28
  assert_equal(result["com"], "sf")
29
29
  end
30
+
31
+ def test_only_the_first_company_query_uses_api
32
+ first_time = Juhe::Express.company_code_of("申通") # => "sto"
33
+ Juhe::express_app_key = "wrong app key"
34
+ second_time = Juhe::Express.company_code_of("申通")
35
+ assert_equal(first_time, second_time)
36
+ end
30
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: juhe_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - linuxnerd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-23 00:00:00.000000000 Z
11
+ date: 2014-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler