json_requester 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +11 -0
- data/lib/json_requester.rb +72 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 53a9ef445b7220bf673bb9ba292d10d749bf487991fa785be223767d48061aa6
|
4
|
+
data.tar.gz: f8943433d455d141aa2c87498d5c5fae44b0a9a62bc3a1945e0224de4cf95e22
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 152468b23940aba15216bdedeb535c2a9353655b5ba7f0d6b5df67370f9401be2b37cd90f39e4c10eb2235beda604ee757255d20f419674607ed262bd117d756
|
7
|
+
data.tar.gz: 26ca1dabffac19d11ab9a4f4f06522f3d6a66d92edf9e6924d3c8d9ccb0a339dab17eb2932ed38a78bf8e700497e87565392c5f30f93d0236c7b3081ad4516d5
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
class JsonRequester
|
5
|
+
attr_reader :host, :conn
|
6
|
+
|
7
|
+
def initialize(host)
|
8
|
+
@host = host
|
9
|
+
@conn = init_conn
|
10
|
+
end
|
11
|
+
|
12
|
+
def http_send(http_method, path, params={}, headers={})
|
13
|
+
if http_method == :get
|
14
|
+
normal_send(http_method, path, params, headers)
|
15
|
+
else
|
16
|
+
json_send(http_method, path, params, headers)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def normal_send(http_method, path, params={}, headers={})
|
21
|
+
res = @conn.send(http_method) do |req|
|
22
|
+
req.url path
|
23
|
+
req.params = params if object_present?(params)
|
24
|
+
req.headers = headers if object_present?(headers)
|
25
|
+
end
|
26
|
+
process_response(res)
|
27
|
+
rescue => e
|
28
|
+
error_response(e)
|
29
|
+
end
|
30
|
+
|
31
|
+
def json_send(http_method, path, params={}, headers={})
|
32
|
+
res = @conn.send(http_method) do |req|
|
33
|
+
req.url path
|
34
|
+
req.headers = headers if object_present?(headers)
|
35
|
+
req.headers['Content-Type'] = 'application/json'
|
36
|
+
req.body = params.to_json if object_present?(params)
|
37
|
+
end
|
38
|
+
process_response(res)
|
39
|
+
rescue => e
|
40
|
+
error_response(e)
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def init_conn
|
46
|
+
Faraday.new(url: host) do |faraday|
|
47
|
+
faraday.request :url_encoded # form-encode POST params
|
48
|
+
faraday.response :logger # log requests to $stdout
|
49
|
+
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def process_response(response)
|
54
|
+
result = {'status' => response.status}
|
55
|
+
begin
|
56
|
+
body = JSON.parse(response.body)
|
57
|
+
body = body.is_a?(Hash) ? body : {'body' => body}
|
58
|
+
body['body_status'] = body.delete('status') unless body['status'].is_a?(Integer)
|
59
|
+
rescue
|
60
|
+
body = {'body' => response.body}
|
61
|
+
end
|
62
|
+
result.merge(body)
|
63
|
+
end
|
64
|
+
|
65
|
+
def error_response(err)
|
66
|
+
{'status' => 500, 'message' => "#{err.class.name}: #{err.message}"}
|
67
|
+
end
|
68
|
+
|
69
|
+
def object_present?(object)
|
70
|
+
!(object.nil? || object.empty?)
|
71
|
+
end
|
72
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json_requester
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- JiaRou Lee
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-07-23 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: wrapper of faraday
|
14
|
+
email: laura34963@kdanmobile.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files:
|
18
|
+
- README.md
|
19
|
+
files:
|
20
|
+
- README.md
|
21
|
+
- lib/json_requester.rb
|
22
|
+
homepage: https://github.com/laura34963/json_requester
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options:
|
28
|
+
- "--charset=UTF-8"
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 2.5.1
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubygems_version: 3.0.1
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: Basic Wrapper of Faraday
|
46
|
+
test_files: []
|