restclient_api_base 0.0.5 → 0.0.6

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: e29b7b73cd034da75ef57895421cc66ce6bb1fdc
4
- data.tar.gz: 308cc38dd40071562a17c39936da542828ed61b9
3
+ metadata.gz: 4f8b33bce6264015237582f7a5cbc2c5980e9149
4
+ data.tar.gz: 3cec9973618a2a36169cd6722f236fba528d1d23
5
5
  SHA512:
6
- metadata.gz: 837736b116a6babaa088fffdb313d6dddbda2694bcdbd81e58465a855e689aa89cb78c042efe71a7e45d9f151a778b047153ca54a6aedc52ce4d1a2c7210a65d
7
- data.tar.gz: 77e7cd4199a17a79e904076baf02243ea864eb16783fc7fedf5eb54470f618a689fa31d461653bfdd498151acdb1e77b98428fd6b6b142ee0027becb52ab2af3
6
+ metadata.gz: 3db011b5050a43688ecc8164fe3f440d07ff43593d7689bbdae322a5229ea3170c05920073a78b6da8922e319cddae49c7c0e43080069aa20cddb9974d6f1b8b
7
+ data.tar.gz: 13148f77625727d3cf04254b0ab9b9b33b7210fa3f7ea24251fbb7aeaaeff101fcb234849ddab081828beabd2df24e4de3e73a05c3fbd4c5311c08e6933f1ad4
data/README.md CHANGED
@@ -30,8 +30,10 @@ require 'restclient_api_base'
30
30
  module A
31
31
  include RestclientApiBase
32
32
 
33
- self.base_url = 'http://www.example.com' # define app base url
34
- self.private_params = { key: 'api_key' } # define app secret key
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.logger_name = 'Error' # define error name in Rails.logger or STDOUT
36
+ self.debug = true # debug
35
37
  end
36
38
 
37
39
  res = A.get('/api/xxx', key_1: value_1, key_2: value_2) # => { response: json_response }
@@ -41,6 +43,9 @@ res.headers # => {:content_type=>"text/html; charset=utf-8", :cache_control=>"p
41
43
 
42
44
  res = A.post('/api/xxx', key_1: value_1, key_2: value_2)
43
45
  res = A.patch('/api/xxx', key_1: value_1, key_2: value_2)
46
+
47
+ write_log(res) # => [Error-2014-08-11 17:41:46] res..
48
+
44
49
  ```
45
50
 
46
51
  ## Contributing
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module RestclientApiBase
4
- VERSION = "0.0.5"
4
+ VERSION = "0.0.6"
5
5
  end
@@ -20,61 +20,68 @@ module RestclientApiBase
20
20
  # base_url: the api base url, like: http://api.example.com, default is ''
21
21
  # private_params: your api secret, like: { app_id: 'xxxx' }, default is {}
22
22
 
23
- attr_accessor :base_url, :private_params, :logger_name
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
24
30
 
25
- def get api_url, params = {}
26
31
  begin
27
- res = RestClient.get(
28
- base_url + api_url,
29
- params: private_params.merge(params)
30
- )
32
+ res = RestClient.get(url, params)
31
33
  rescue => e
32
34
  e.response
33
35
  end
34
36
  end
35
37
 
36
38
  def post api_url, query = {}, headers = {}
39
+ url = base_url + api_url
40
+ params = private_params.merge(query)
41
+
42
+ puts "method: POST, url: #{url}, params: #{params}" if debug
43
+
37
44
  begin
38
- res = RestClient.post(
39
- base_url + api_url,
40
- JSON.generate(private_params.merge(query)),
41
- content_type.merge(headers)
42
- )
45
+ res = RestClient.post(url, JSON.generate(params), content_type.merge(headers))
43
46
  rescue => e
44
47
  e.response
45
48
  end
46
49
  end
47
50
 
48
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
+
49
57
  begin
50
- res = RestClient.patch(
51
- base_url + api_url,
52
- JSON.generate(private_params.merge(query)),
53
- content_type.merge(headers)
54
- )
58
+ res = RestClient.patch(url, JSON.generate(params), content_type.merge(headers))
55
59
  rescue => e
56
60
  e.response
57
61
  end
58
62
  end
59
63
 
60
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
+
61
70
  begin
62
- res = RestClient.put(
63
- base_url + api_url,
64
- JSON.generate(private_params.merge(query)),
65
- content_type.merge(headers)
66
- )
71
+ res = RestClient.put(url, JSON.generate(params), content_type.merge(headers))
67
72
  rescue => e
68
73
  e.response
69
74
  end
70
75
  end
71
76
 
72
- def delete api_url, params = {}
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
+
73
83
  begin
74
- res = RestClient.delete(
75
- base_url + api_url,
76
- params: private_params.merge(params),
77
- )
84
+ res = RestClient.delete(url, params)
78
85
  rescue => e
79
86
  e.response
80
87
  end
@@ -13,6 +13,7 @@ class TestRestclientApiBase < Minitest::Test
13
13
 
14
14
  self.base_url = 'https://api.github.com'
15
15
  self.private_params = {}
16
+ self.debug = true
16
17
  end
17
18
 
18
19
  def test_get
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.5
4
+ version: 0.0.6
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-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client