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 +4 -4
- data/README.md +7 -2
- data/lib/restclient_api_base/version.rb +1 -1
- data/lib/restclient_api_base.rb +33 -26
- data/test/test_restclient_api_base.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f8b33bce6264015237582f7a5cbc2c5980e9149
|
4
|
+
data.tar.gz: 3cec9973618a2a36169cd6722f236fba528d1d23
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
34
|
-
self.private_params = { key: 'api_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
|
data/lib/restclient_api_base.rb
CHANGED
@@ -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,
|
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
|
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.
|
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
|
+
date: 2014-08-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|