crapi 0.1.0 → 0.1.1
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/Gemfile.lock +3 -3
- data/crapi.gemspec +1 -1
- data/lib/crapi/client.rb +107 -105
- data/lib/crapi/proxy.rb +40 -38
- data/lib/crapi/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b8a75306ad51c628ac62d5acfffa0ea9690e0cd530b46f1454a81e35eeef69c
|
4
|
+
data.tar.gz: 6fe28e880625a02743eede743da273095ba07f94d2263c335bd2d6eee46d41d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1272a0e4b796300a017d4e34a8d736319ef02631070722cafbd44fbdaab0ddbb3336d4969ba18e33ad342098397548266038c1724991c5d278278672fc60ceb
|
7
|
+
data.tar.gz: 6a98ec9905c69ca5d6e26bfd70c0a02d96a8f2ed68e2ecdaa353c474fcf7e38516ddb5641caef395d4368f14ecf08fad318b0d222995cd77f22b87a39cd54974
|
data/Gemfile.lock
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
crapi (0.1.
|
5
|
-
activesupport (~> 5.2)
|
4
|
+
crapi (0.1.1)
|
5
|
+
activesupport (~> 5.2.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
@@ -59,4 +59,4 @@ DEPENDENCIES
|
|
59
59
|
rspec_junit_formatter (~> 0.3)
|
60
60
|
|
61
61
|
BUNDLED WITH
|
62
|
-
1.16.
|
62
|
+
1.16.2
|
data/crapi.gemspec
CHANGED
data/lib/crapi/client.rb
CHANGED
@@ -4,136 +4,138 @@ require 'openssl'
|
|
4
4
|
|
5
5
|
require 'crapi/proxy'
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
7
|
+
module Crapi
|
8
|
+
class Client
|
9
|
+
attr_accessor :default_headers
|
10
|
+
|
11
|
+
JSON_CONTENT_TYPE = 'application/json'.freeze
|
12
|
+
FORM_CONTENT_TYPE = 'application/x-www-form-urlencoded'.freeze
|
13
|
+
|
14
|
+
def initialize(base_uri, opts = {})
|
15
|
+
@base_uri = case base_uri
|
16
|
+
when URI then base_uri
|
17
|
+
when String then URI(base_uri)
|
18
|
+
else raise ArgumentError, %(Unexpected "base_url" type: #{base_url.class})
|
19
|
+
end
|
20
|
+
|
21
|
+
@proxy_host = opts[:proxy_host]
|
22
|
+
@proxy_port = opts[:proxy_port]
|
23
|
+
@proxy_username = opts[:proxy_username]
|
24
|
+
@proxy_password = opts[:proxy_password]
|
25
|
+
|
26
|
+
@http = Net::HTTP.new(@base_uri.host, @base_uri.port,
|
27
|
+
@proxy_host, @proxy_port, @proxy_username, @proxy_password)
|
28
|
+
@http.use_ssl = (@base_uri.scheme == 'https')
|
29
|
+
@http.verify_mode = ::OpenSSL::SSL::VERIFY_NONE if opts[:insecure].present?
|
30
|
+
|
31
|
+
@default_headers = { 'Content-Type': JSON_CONTENT_TYPE }.with_indifferent_access
|
32
|
+
end
|
24
33
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
@http.verify_mode = ::OpenSSL::SSL::VERIFY_NONE if opts[:insecure].present?
|
34
|
+
def new_proxy(segment = '/', headers: nil)
|
35
|
+
Proxy.new(add: segment, to: self, headers: headers)
|
36
|
+
end
|
29
37
|
|
30
|
-
|
31
|
-
end
|
38
|
+
## CRUD methods ...
|
32
39
|
|
33
|
-
|
34
|
-
|
35
|
-
end
|
40
|
+
def delete(path, headers: {}, query: {})
|
41
|
+
headers = @default_headers.merge(headers)
|
36
42
|
|
37
|
-
|
43
|
+
response = @http.delete(full_path(path, query: query), headers)
|
44
|
+
ensure_success!(response)
|
45
|
+
parse_response(response)
|
46
|
+
end
|
38
47
|
|
39
|
-
|
40
|
-
|
48
|
+
def get(path, headers: {}, query: {})
|
49
|
+
headers = @default_headers.merge(headers)
|
41
50
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
51
|
+
response = @http.get(full_path(path, query: query), headers)
|
52
|
+
ensure_success!(response)
|
53
|
+
parse_response(response)
|
54
|
+
end
|
46
55
|
|
47
|
-
|
48
|
-
|
56
|
+
def patch(path, headers: {}, query: {}, payload: {})
|
57
|
+
headers = @default_headers.merge(headers)
|
58
|
+
payload = format_payload(payload, as: headers[:'Content-Type'])
|
49
59
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
def patch(path, headers: {}, query: {}, payload: {})
|
56
|
-
headers = @default_headers.merge(headers)
|
57
|
-
payload = format_payload(payload, as: headers[:'Content-Type'])
|
60
|
+
response = @http.patch(full_path(path, query: query), payload, headers)
|
61
|
+
ensure_success!(response)
|
62
|
+
parse_response(response)
|
63
|
+
end
|
58
64
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
end
|
65
|
+
def post(path, headers: {}, query: {}, payload: {})
|
66
|
+
headers = @default_headers.merge(headers)
|
67
|
+
payload = format_payload(payload, as: headers[:'Content-Type'])
|
63
68
|
|
64
|
-
|
65
|
-
|
66
|
-
|
69
|
+
response = @http.post(full_path(path, query: query), payload, headers)
|
70
|
+
ensure_success!(response)
|
71
|
+
parse_response(response)
|
72
|
+
end
|
67
73
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
end
|
74
|
+
def put(path, headers: {}, query: {}, payload: {})
|
75
|
+
headers = @default_headers.merge(headers)
|
76
|
+
payload = format_payload(payload, as: headers[:'Content-Type'])
|
72
77
|
|
73
|
-
|
74
|
-
|
75
|
-
|
78
|
+
response = @http.put(full_path(path, query: query), payload, headers)
|
79
|
+
ensure_success!(response)
|
80
|
+
parse_response(response)
|
81
|
+
end
|
76
82
|
|
77
|
-
|
78
|
-
ensure_success!(response)
|
79
|
-
parse_response(response)
|
80
|
-
end
|
83
|
+
##
|
81
84
|
|
82
|
-
|
85
|
+
private
|
83
86
|
|
84
|
-
|
87
|
+
##
|
85
88
|
|
86
|
-
|
89
|
+
def full_path(path, query: {})
|
90
|
+
path = [@base_uri.path, path].map { |i| i.gsub(%r{^/|/$}, '') }.keep_if(&:present?)
|
91
|
+
path = "/#{path.join('/')}"
|
87
92
|
|
88
|
-
|
89
|
-
|
90
|
-
|
93
|
+
if query.present?
|
94
|
+
path += case query
|
95
|
+
when Hash, Array then "?#{URI.encode_www_form(query)}"
|
96
|
+
when String then "?#{query}"
|
97
|
+
else raise ArgumentError, %(Unexpected "query" type: #{query.class})
|
98
|
+
end
|
99
|
+
end
|
91
100
|
|
92
|
-
|
93
|
-
path += case query
|
94
|
-
when Hash, Array then "?#{URI.encode_www_form(query)}"
|
95
|
-
when String then "?#{query}"
|
96
|
-
else raise Crapi::ArgumentError, %(Unexpected "query" type: #{query.class})
|
97
|
-
end
|
101
|
+
path
|
98
102
|
end
|
99
103
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
return if response.is_a? Net::HTTPSuccess
|
105
|
-
raise(Crapi::BadHttpResponseError, "#{response.code} - #{response.message}")
|
106
|
-
end
|
104
|
+
def ensure_success!(response)
|
105
|
+
return if response.is_a? Net::HTTPSuccess
|
106
|
+
raise BadHttpResponseError, "#{response.code} - #{response.message}"
|
107
|
+
end
|
107
108
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
109
|
+
def format_payload(payload, as: JSON_CONTENT_TYPE)
|
110
|
+
## Non-Hash payloads are passed through as-is.
|
111
|
+
return payload unless payload.is_a? Hash
|
112
|
+
|
113
|
+
## Massage Hash-like payloads into a suitable format.
|
114
|
+
case as
|
115
|
+
when JSON_CONTENT_TYPE
|
116
|
+
JSON.generate(payload.as_json)
|
117
|
+
when FORM_CONTENT_TYPE
|
118
|
+
payload.to_query
|
119
|
+
else
|
120
|
+
payload.to_s
|
121
|
+
end
|
120
122
|
end
|
121
|
-
end
|
122
123
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
124
|
+
def parse_response(response)
|
125
|
+
case response.content_type
|
126
|
+
when JSON_CONTENT_TYPE
|
127
|
+
JSON.parse(response.body, quirks_mode: true, symbolize_names: true)
|
128
|
+
else
|
129
|
+
response.body
|
130
|
+
end
|
129
131
|
end
|
130
132
|
end
|
131
|
-
end
|
132
133
|
|
133
|
-
## Net::HTTP needs a shortcut instance method for PUT calls like it does for GET/DELETE/PATCH/POST.
|
134
|
-
##
|
135
|
-
class Net::HTTP
|
136
|
-
|
137
|
-
|
134
|
+
## Net::HTTP needs a shortcut instance method for PUT calls like it does for GET/DELETE/PATCH/POST.
|
135
|
+
##
|
136
|
+
class Net::HTTP
|
137
|
+
def put(path, data, initheader = nil, dest = nil, &block)
|
138
|
+
send_entity(path, data, initheader, dest, Put, &block)
|
139
|
+
end
|
138
140
|
end
|
139
141
|
end
|
data/lib/crapi/proxy.rb
CHANGED
@@ -1,42 +1,44 @@
|
|
1
1
|
require 'active_support/all'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
3
|
+
module Crapi
|
4
|
+
class Proxy
|
5
|
+
attr_accessor :default_headers
|
6
|
+
|
7
|
+
def initialize(add:, to:, headers: nil)
|
8
|
+
@parent = to
|
9
|
+
@segment = add
|
10
|
+
@default_headers = (headers || {}).with_indifferent_access
|
11
|
+
end
|
12
|
+
|
13
|
+
def new_proxy(segment = '/', headers: nil)
|
14
|
+
Proxy.new(add: segment, to: self, headers: headers)
|
15
|
+
end
|
16
|
+
|
17
|
+
## CRUD methods ...
|
18
|
+
|
19
|
+
def delete(path, headers: {}, query: {})
|
20
|
+
@parent.delete("/#{@segment}/#{path}".gsub(%r{/+}, '/'),
|
21
|
+
headers: @default_headers.merge(headers), query: query)
|
22
|
+
end
|
23
|
+
|
24
|
+
def get(path, headers: {}, query: {})
|
25
|
+
@parent.get("/#{@segment}/#{path}".gsub(%r{/+}, '/'),
|
26
|
+
headers: @default_headers.merge(headers), query: query)
|
27
|
+
end
|
28
|
+
|
29
|
+
def patch(path, headers: {}, query: {}, payload: {})
|
30
|
+
@parent.patch("/#{@segment}/#{path}".gsub(%r{/+}, '/'),
|
31
|
+
heades: @default_headers.merge(headers), query: query, payload: payload)
|
32
|
+
end
|
33
|
+
|
34
|
+
def post(path, headers: {}, query: {}, payload: {})
|
35
|
+
@parent.post("/#{@segment}/#{path}".gsub(%r{/+}, '/'),
|
36
|
+
headers: @default_headers.merge(headers), query: query, payload: payload)
|
37
|
+
end
|
38
|
+
|
39
|
+
def put(path, headers: {}, query: {}, payload: {})
|
40
|
+
@parent.put("/#{@segment}/#{path}".gsub(%r{/+}, '/'),
|
41
|
+
headers: @default_headers.merge(headers), query: query, payload: payload)
|
42
|
+
end
|
41
43
|
end
|
42
44
|
end
|
data/lib/crapi/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: crapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nestor Custodio
|
@@ -100,14 +100,14 @@ dependencies:
|
|
100
100
|
requirements:
|
101
101
|
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version:
|
103
|
+
version: 5.2.0
|
104
104
|
type: :runtime
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version:
|
110
|
+
version: 5.2.0
|
111
111
|
description:
|
112
112
|
email:
|
113
113
|
- sakimorix@gmail.com
|