restclient_api_base 0.0.3 → 0.0.4
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 +4 -4
- data/README.md +1 -1
- data/lib/concern.rb +2 -0
- data/lib/restclient_api_base.rb +47 -30
- data/lib/restclient_api_base/version.rb +3 -1
- data/test/test_helper.rb +2 -0
- data/test/test_restclient_api_base.rb +2 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 509536339ef55a76d0ce5b2b79b4cf83aeeee644
|
4
|
+
data.tar.gz: a7de0f09cb2b8ad99a2397ca5c543efa22d6ffdf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 983a7ad03f32db52d26c32a472890779ef24cb033454985c046bcf83cb199a2dd52e1bf025e47e520728672dabd8792320f6d2a53d66b730bc55749e0970a9c9
|
7
|
+
data.tar.gz: f16c6d7cf92360dbccb79355fbbba0b1adeaf97545083e45cdd8c7e4bfb02d93dad6372a709efdb850233f2ce1c1372dbde071f5913a35b3bcae86fd48447c9f
|
data/README.md
CHANGED
data/lib/concern.rb
CHANGED
data/lib/restclient_api_base.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require "restclient_api_base/version"
|
2
4
|
require "json"
|
3
5
|
require "rest-client"
|
@@ -15,47 +17,66 @@ module RestclientApiBase
|
|
15
17
|
module ClassMethods
|
16
18
|
|
17
19
|
# base_url: the api base url, like: http://api.example.com, default is ''
|
18
|
-
# content_type: dafault is { content_type: :json, accept: :json }
|
19
20
|
# private_params: your api secret, like: { app_id: 'xxxx' }, default is {}
|
20
21
|
|
21
|
-
attr_accessor :base_url, :
|
22
|
+
attr_accessor :base_url, :private_params
|
22
23
|
|
23
24
|
def get api_url, params = {}
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
begin
|
26
|
+
res = RestClient.get(
|
27
|
+
base_url + api_url,
|
28
|
+
params: private_params.merge(params)
|
29
|
+
)
|
30
|
+
rescue => e
|
31
|
+
e.response
|
32
|
+
end
|
28
33
|
end
|
29
34
|
|
30
35
|
def post api_url, query = {}, headers = {}
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
+
begin
|
37
|
+
res = RestClient.post(
|
38
|
+
base_url + api_url,
|
39
|
+
JSON.generate(private_params.merge(query)),
|
40
|
+
content_type.merge(headers)
|
41
|
+
)
|
42
|
+
rescue => e
|
43
|
+
e.response
|
44
|
+
end
|
36
45
|
end
|
37
46
|
|
38
47
|
def patch api_url, query = {}, headers = {}
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
48
|
+
begin
|
49
|
+
res = RestClient.patch(
|
50
|
+
base_url + api_url,
|
51
|
+
JSON.generate(private_params.merge(query)),
|
52
|
+
content_type.merge(headers)
|
53
|
+
)
|
54
|
+
rescue => e
|
55
|
+
e.response
|
56
|
+
end
|
44
57
|
end
|
45
58
|
|
46
59
|
def put api_url, query = {}, headers = {}
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
60
|
+
begin
|
61
|
+
res = RestClient.put(
|
62
|
+
base_url + api_url,
|
63
|
+
JSON.generate(private_params.merge(query)),
|
64
|
+
content_type.merge(headers)
|
65
|
+
)
|
66
|
+
rescue => e
|
67
|
+
e.response
|
68
|
+
end
|
52
69
|
end
|
53
70
|
|
54
71
|
def delete api_url, params = {}
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
72
|
+
begin
|
73
|
+
res = RestClient.delete(
|
74
|
+
base_url + api_url,
|
75
|
+
params: private_params.merge(params),
|
76
|
+
)
|
77
|
+
rescue => e
|
78
|
+
e.response
|
79
|
+
end
|
59
80
|
end
|
60
81
|
|
61
82
|
def base_url
|
@@ -63,11 +84,7 @@ module RestclientApiBase
|
|
63
84
|
end
|
64
85
|
|
65
86
|
def content_type
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
def content_type= type
|
70
|
-
@content_type = { content_type: type, accept: type }
|
87
|
+
{ content_type: :json, accept: :json }
|
71
88
|
end
|
72
89
|
|
73
90
|
def private_params
|
data/test/test_helper.rb
CHANGED