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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d0e7611148ac41483a29424a93722a812e6145f8
4
- data.tar.gz: 67e70e6fbcfe73146b35638a26a5aaa42bacbb9c
3
+ metadata.gz: 6cacd3d776f0ba45e963e107941edde906373b84
4
+ data.tar.gz: f48eb62d02c192c5662ceea2ed2326e816f15c38
5
5
  SHA512:
6
- metadata.gz: ec04065543d39890702faf28564ade8cf3d8a4dc922af9d4ad20766979897103f35eaa0147b0dc362d0ba09d055b0f579f68a8cf820356f9e2839ec5162e52d4
7
- data.tar.gz: 4b655321c95e2796e37ab18a14024821039e8cd0d46213ce649be9811cbf3ec1fe48849e4061a8d2d9acfb84944c7c1b2dd264b6e8f586aa94c2c8d190cbd283
6
+ metadata.gz: f20b6f55a8de947b06a91d43ddd68f607afd4395397702dc1fe57f5534bdda6692e0b815cae77bfcf37df629c077ab6e1aa33cb1b03a987040d8067f54ecdeff
7
+ data.tar.gz: 531ee4b36b4cad6028634cf15fcb20fe7f6fc36ab70362e438bf30b388affb287caaed7b0735be63476e39e3e32c4c52102af85f4930a55ef57060a1a2e60b33
data/Gemfile CHANGED
@@ -1,4 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in restclient_api_base.gemspec
4
+
5
+ gem "rest-client", "~> 1.7"
6
+ gem "lumberjack", "~> 1.0"
7
+
4
8
  gemspec
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # RestclientApiBase
2
2
 
3
- 以 REST 方式调用外部 API 的基础类, 默认 Content-Type 为 JSON
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 = 'http://www.example.com' # define app base url
34
- self.private_params = { key: 'api_key' } # define app secret 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
- A.post('/api/xxx', key_1: value_1, key_2: value_2)
39
- A.patch('/api/xxx', key_1: value_1, key_2: value_2)
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
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module ActiveSupport
2
4
  # A typical module looks like this:
3
5
  #
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module RestclientApiBase
2
- VERSION = "0.0.2"
4
+ VERSION = "0.1.1.1"
3
5
  end
@@ -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 "./lib/concern"
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, :content_type, :private_params
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
- def get api_url, params = {}
24
- res = RestClient.get(
25
- base_url + api_url,
26
- params: private_params.merge(params)
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
- res = RestClient.post(
32
- base_url + api_url,
33
- JSON.generate(private_params.merge(query)),
34
- content_type.merge(headers)
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
- res = RestClient.patch(
40
- base_url + api_url,
41
- JSON.generate(private_params.merge(query)),
42
- content_type.merge(headers)
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
- res = RestClient.put(
48
- base_url + api_url,
49
- JSON.generate(private_params.merge(query)),
50
- content_type.merge(headers)
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, params = {}
55
- res = RestClient.delete(
56
- base_url + api_url,
57
- params: private_params.merge(params),
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
- @content_type ||= { content_type: :json, accept: :json }
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 'minitest/autorun'
2
4
  require File.dirname(__FILE__) + '/../lib/restclient_api_base'
3
5
 
@@ -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.0.2
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 00:00:00.000000000 Z
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
- - restclinet_api_base.gemspec
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