restclient_api_base 0.0.2 → 0.1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +4 -0
- data/README.md +17 -6
- data/lib/concern.rb +2 -0
- data/lib/restclient_api_base/version.rb +3 -1
- data/lib/restclient_api_base.rb +72 -36
- data/{restclinet_api_base.gemspec → restclient_api_base.gemspec} +1 -0
- data/test/test_helper.rb +2 -0
- data/test/test_restclient_api_base.rb +6 -0
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6cacd3d776f0ba45e963e107941edde906373b84
|
4
|
+
data.tar.gz: f48eb62d02c192c5662ceea2ed2326e816f15c38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f20b6f55a8de947b06a91d43ddd68f607afd4395397702dc1fe57f5534bdda6692e0b815cae77bfcf37df629c077ab6e1aa33cb1b03a987040d8067f54ecdeff
|
7
|
+
data.tar.gz: 531ee4b36b4cad6028634cf15fcb20fe7f6fc36ab70362e438bf30b388affb287caaed7b0735be63476e39e3e32c4c52102af85f4930a55ef57060a1a2e60b33
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# RestclientApiBase
|
2
2
|
|
3
|
-
以 REST 方式调用外部 API 的基础类,
|
3
|
+
以 REST 方式调用外部 API 的基础类, Content-Type 为 JSON
|
4
4
|
|
5
5
|
## Dependency
|
6
6
|
|
@@ -8,6 +8,8 @@
|
|
8
8
|
|
9
9
|
[rest-client](https://github.com/rest-client/rest-client)
|
10
10
|
|
11
|
+
[lumberjack](https://github.com/bdurand/lumberjack)
|
12
|
+
|
11
13
|
## Installation
|
12
14
|
|
13
15
|
Add this line to your application's Gemfile:
|
@@ -30,13 +32,22 @@ require 'restclient_api_base'
|
|
30
32
|
module A
|
31
33
|
include RestclientApiBase
|
32
34
|
|
33
|
-
self.base_url
|
34
|
-
self.private_params = { key: 'api_key' }
|
35
|
+
self.base_url = 'http://www.example.com' # define app base url
|
36
|
+
self.private_params = { key: 'api_key' } # define app secret key
|
37
|
+
self.logger_name = 'Error' # define error name in log dir
|
38
|
+
self.debug = true # debug
|
35
39
|
end
|
36
40
|
|
37
|
-
A.get('/api/xxx', key_1: value_1, key_2: value_2) # => { response: json_response }
|
38
|
-
|
39
|
-
|
41
|
+
res = A.get('/api/xxx', key_1: value_1, key_2: value_2) # => { response: json_response }
|
42
|
+
res.code # => 200
|
43
|
+
res.cookies # => { 'key' => 'value' }
|
44
|
+
res.headers # => {:content_type=>"text/html; charset=utf-8", :cache_control=>"private" ...
|
45
|
+
|
46
|
+
res = A.post('/api/xxx', key_1: value_1, key_2: value_2)
|
47
|
+
res = A.patch('/api/xxx', key_1: value_1, key_2: value_2)
|
48
|
+
|
49
|
+
write_log(res) # => [2014-08-19T22:10:29.438 INFO (32890)] res..
|
50
|
+
|
40
51
|
```
|
41
52
|
|
42
53
|
## Contributing
|
data/lib/concern.rb
CHANGED
data/lib/restclient_api_base.rb
CHANGED
@@ -1,12 +1,15 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require "restclient_api_base/version"
|
2
4
|
require "json"
|
3
5
|
require "rest-client"
|
6
|
+
require "lumberjack"
|
4
7
|
|
5
8
|
# For other framework except Rails
|
6
9
|
begin
|
7
10
|
require "active_support/concern"
|
8
11
|
rescue LoadError => e
|
9
|
-
require "
|
12
|
+
require "concern"
|
10
13
|
end
|
11
14
|
|
12
15
|
module RestclientApiBase
|
@@ -15,47 +18,73 @@ module RestclientApiBase
|
|
15
18
|
module ClassMethods
|
16
19
|
|
17
20
|
# base_url: the api base url, like: http://api.example.com, default is ''
|
18
|
-
# content_type: dafault is { content_type: :json, accept: :json }
|
19
21
|
# private_params: your api secret, like: { app_id: 'xxxx' }, default is {}
|
20
22
|
|
21
|
-
attr_accessor :base_url, :
|
23
|
+
attr_accessor :base_url, :private_params, :logger_name, :debug
|
24
|
+
|
25
|
+
def get api_url, options = {}
|
26
|
+
url = base_url + api_url
|
27
|
+
params = private_params.merge(options)
|
28
|
+
|
29
|
+
puts "method: GET, url: #{url}, params: #{params}" if debug
|
22
30
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
31
|
+
begin
|
32
|
+
res = RestClient.get(url, params: params)
|
33
|
+
rescue => e
|
34
|
+
e.response
|
35
|
+
end
|
28
36
|
end
|
29
37
|
|
30
38
|
def post api_url, query = {}, headers = {}
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
39
|
+
url = base_url + api_url
|
40
|
+
params = private_params.merge(query)
|
41
|
+
|
42
|
+
puts "method: POST, url: #{url}, params: #{params}" if debug
|
43
|
+
|
44
|
+
begin
|
45
|
+
res = RestClient.post(url, JSON.generate(params), content_type.merge(headers))
|
46
|
+
rescue => e
|
47
|
+
e.response
|
48
|
+
end
|
36
49
|
end
|
37
50
|
|
38
|
-
def patch api_url, query = {}
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
51
|
+
def patch api_url, query = {}, headers = {}
|
52
|
+
url = base_url + api_url
|
53
|
+
params = private_params.merge(query)
|
54
|
+
|
55
|
+
puts "method: PATCH, url: #{url}, params: #{params}" if debug
|
56
|
+
|
57
|
+
begin
|
58
|
+
res = RestClient.patch(url, JSON.generate(params), content_type.merge(headers))
|
59
|
+
rescue => e
|
60
|
+
e.response
|
61
|
+
end
|
44
62
|
end
|
45
63
|
|
46
|
-
def put api_url, query = {}
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
64
|
+
def put api_url, query = {}, headers = {}
|
65
|
+
url = base_url + api_url
|
66
|
+
params = private_params.merge(query)
|
67
|
+
|
68
|
+
puts "method: PUT, url: #{url}, params: #{params}" if debug
|
69
|
+
|
70
|
+
begin
|
71
|
+
res = RestClient.put(url, JSON.generate(params), content_type.merge(headers))
|
72
|
+
rescue => e
|
73
|
+
e.response
|
74
|
+
end
|
52
75
|
end
|
53
76
|
|
54
|
-
def delete api_url,
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
77
|
+
def delete api_url, options = {}
|
78
|
+
url = base_url + api_url
|
79
|
+
params = private_params.merge(options)
|
80
|
+
|
81
|
+
puts "method: DELETE, url: #{url}, params: #{params}" if debug
|
82
|
+
|
83
|
+
begin
|
84
|
+
res = RestClient.delete(url, params)
|
85
|
+
rescue => e
|
86
|
+
e.response
|
87
|
+
end
|
59
88
|
end
|
60
89
|
|
61
90
|
def base_url
|
@@ -63,11 +92,7 @@ module RestclientApiBase
|
|
63
92
|
end
|
64
93
|
|
65
94
|
def content_type
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
def content_type= type
|
70
|
-
@content_type = { content_type: type, accept: type }
|
95
|
+
{ content_type: :json, accept: :json }
|
71
96
|
end
|
72
97
|
|
73
98
|
def private_params
|
@@ -78,5 +103,16 @@ module RestclientApiBase
|
|
78
103
|
raise 'the private params must be a hash' unless params.is_a? Hash
|
79
104
|
@private_params = params
|
80
105
|
end
|
106
|
+
|
107
|
+
def logger_name
|
108
|
+
@logger_name ||= self.to_s.downcase.gsub(/::/, '_')
|
109
|
+
end
|
110
|
+
|
111
|
+
def write_log *args
|
112
|
+
logger = Lumberjack::Logger.new("log/#{logger_name}.log")
|
113
|
+
logger.info(args.join('-'))
|
114
|
+
end
|
115
|
+
|
116
|
+
alias :write_api_log :write_log
|
81
117
|
end
|
82
|
-
end
|
118
|
+
end
|
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_dependency "rest-client", "~> 1.7"
|
22
|
+
spec.add_dependency "lumberjack", "~> 1.0"
|
22
23
|
spec.add_development_dependency "bundler", "~> 1.6"
|
23
24
|
spec.add_development_dependency "rake"
|
24
25
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
4
|
|
3
5
|
class TestRestclientApiBase < Minitest::Test
|
@@ -11,10 +13,14 @@ class TestRestclientApiBase < Minitest::Test
|
|
11
13
|
|
12
14
|
self.base_url = 'https://api.github.com'
|
13
15
|
self.private_params = {}
|
16
|
+
self.debug = true
|
14
17
|
end
|
15
18
|
|
16
19
|
def test_get
|
17
20
|
list = TestClient.get(@list_url)
|
21
|
+
assert_equal 200, list.code, 'the http status code is 200'
|
22
|
+
assert_equal "application/json; charset=utf-8", list.headers[:content_type]
|
23
|
+
assert list.cookies
|
18
24
|
assert_equal true, JSON.parse(list).is_a?(Array)
|
19
25
|
end
|
20
26
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restclient_api_base
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Spirit
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: lumberjack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -67,7 +81,7 @@ files:
|
|
67
81
|
- lib/concern.rb
|
68
82
|
- lib/restclient_api_base.rb
|
69
83
|
- lib/restclient_api_base/version.rb
|
70
|
-
-
|
84
|
+
- restclient_api_base.gemspec
|
71
85
|
- test/test_helper.rb
|
72
86
|
- test/test_restclient_api_base.rb
|
73
87
|
homepage: https://github.com/NaixSpirit/restclient_api_base
|