web_api 0.3.191208 → 1.0.210927
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 +40 -28
- data/lib/web_api/web_api.rb +50 -50
- data/lib/web_api/web_api_method.rb +75 -78
- data/lib/web_api.rb +4 -5
- metadata +16 -10
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 82115f267d8164697506e891e5dace6f796a69ab8923c1dd193dd5d38faeff3e
|
|
4
|
+
data.tar.gz: c60078933f51883cadefb158d221607e9e11bad9cd66fc47185f87d2df45adc4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 32e2b5f9b5f467aee348c968a409bd7df263eeb73395929e8e3f43945bdf9ed2cf6f9d3b0c715dc6685239f56601d1ed99928a858d9d622aca285542a5539d16
|
|
7
|
+
data.tar.gz: ed2d94e383e02ae8c327157054b91d069131470e6b21ed107797879eaef05d6072d134eff70ea7999e7b6e989d430b082b28e936b1d0c1e9a020c03aee781e36
|
data/README.md
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# WebApi
|
|
2
2
|
|
|
3
|
+
* [VERSION 1.0.210927](https://www.github.com/carlosjhr64/web_api)
|
|
3
4
|
* [github](https://www.github.com/carlosjhr64/web_api)
|
|
4
5
|
* [rubygems](https://rubygems.org/gems/web_api)
|
|
5
6
|
|
|
@@ -8,22 +9,23 @@
|
|
|
8
9
|
Ruby library for web api's.
|
|
9
10
|
|
|
10
11
|
## SYNOPSIS:
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
```ruby
|
|
13
|
+
require 'web_api'
|
|
14
|
+
webapi = WebApi.new "https://api.site.org/path-to-base/",
|
|
15
|
+
header: {Authorization: "Bearer ABC123XYZ"}
|
|
16
|
+
# for a post to https://api.site.org/path-to-base/resource...
|
|
17
|
+
webapi.add(:resource, type: :post)
|
|
18
|
+
# You can pass the post's (or query's) key value pairs in a hash.
|
|
19
|
+
body = webapi.resource(data: {key: "value"})
|
|
20
|
+
```
|
|
19
21
|
## INSTALL:
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
```console
|
|
23
|
+
$ gem install web_api
|
|
24
|
+
```
|
|
23
25
|
## MORE:
|
|
24
26
|
|
|
25
27
|
There's not that much code here...
|
|
26
|
-
under 200 lines in `lib/**.rb` at the time of this
|
|
28
|
+
under 200 lines in `lib/**.rb` at the time of this writing.
|
|
27
29
|
Take a look at the examples given at [github](https://github.com/carlosjhr64/web_api/tree/master/examples)
|
|
28
30
|
for use cases.
|
|
29
31
|
|
|
@@ -34,11 +36,17 @@ The model is that at each step...
|
|
|
34
36
|
3. call to a WebApi method
|
|
35
37
|
|
|
36
38
|
...one builds up the url, type, data, and header of the http request.
|
|
37
|
-
The WebApi methods `#new`, `#add`, and `#<method>`
|
|
39
|
+
The WebApi methods `#new`, `#add`, and `#<method>`
|
|
40
|
+
all have the same argument signature:
|
|
38
41
|
|
|
39
|
-
extension String,
|
|
42
|
+
extension String,
|
|
43
|
+
type: Symbol,
|
|
44
|
+
data: Hash,
|
|
45
|
+
header: Hash,
|
|
46
|
+
dumper: Proc,
|
|
47
|
+
Parser: Proc|Hash(String, Proc)
|
|
40
48
|
|
|
41
|
-
The extension builds up the url by
|
|
49
|
+
The extension builds up the url by concatenation.
|
|
42
50
|
The data and headers hashes are built up with merge.
|
|
43
51
|
The type, dumper, and parser can be changed at each step.
|
|
44
52
|
|
|
@@ -46,28 +54,32 @@ One can read the code to check the minor nuances of each method's signature,
|
|
|
46
54
|
such as default values.
|
|
47
55
|
|
|
48
56
|
Note that `#add` will assume extension is the same as the name of the method if
|
|
49
|
-
no
|
|
57
|
+
no extension is given.
|
|
50
58
|
|
|
51
|
-
Note that `#<method>` will assume the user meant to pass data if
|
|
52
|
-
|
|
59
|
+
Note that `#<method>` will assume the user meant to pass data if
|
|
60
|
+
it only gets a hash:
|
|
53
61
|
|
|
54
|
-
#<method>({"a"=>"ABC","x"=>"XYZ"})
|
|
62
|
+
#<method>({"a"=>"ABC","x"=>"XYZ"})
|
|
63
|
+
#=> #<method>('', data: {"a"=>"ABC","x"=>"XYZ"})
|
|
55
64
|
|
|
56
|
-
The dumper to dump the data in a post request is JSON.dump by default
|
|
65
|
+
The dumper to dump the data in a post request is JSON.dump by default
|
|
66
|
+
if JSON is available.
|
|
57
67
|
|
|
58
|
-
The parser to parse the body of an "application/json" type content is
|
|
59
|
-
|
|
68
|
+
The parser to parse the body of an "application/json" type content is
|
|
69
|
+
JSON.parse by default if available.
|
|
70
|
+
You can read the code and inspect `WebApi::PARSER`
|
|
71
|
+
to see the other parsers available by default.
|
|
60
72
|
|
|
61
|
-
If one does not want to parse the
|
|
73
|
+
If one does not want to parse the response's body,
|
|
62
74
|
one can set `parser: :none`. For example:
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
75
|
+
```ruby
|
|
76
|
+
body = webapi.resourse(data: {key: "value"}, parser: :none)
|
|
77
|
+
```
|
|
66
78
|
## LICENSE:
|
|
67
79
|
|
|
68
80
|
(The MIT License)
|
|
69
81
|
|
|
70
|
-
Copyright (c)
|
|
82
|
+
Copyright (c) 2021 CarlosJHR64
|
|
71
83
|
|
|
72
84
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
73
85
|
a copy of this software and associated documentation files (the
|
data/lib/web_api/web_api.rb
CHANGED
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
DUMPER = (defined? JSON)? JSON.method(:dump) : :none
|
|
1
|
+
class WebApi
|
|
2
|
+
DUMPER = (defined? JSON)? JSON.method(:dump) : :none
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
PARSER = Hash.new :none
|
|
5
|
+
PARSER['application/json'] = JSON.method(:parse) if defined? JSON
|
|
6
|
+
PARSER['text/csv'] = CSV.method(:parse) if defined? CSV
|
|
7
|
+
PARSER['text/html'] = Nokogiri::HTML.method(:parse) if defined? Nokogiri
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
9
|
+
def initialize(base = '',
|
|
10
|
+
type: :get,
|
|
11
|
+
data: {},
|
|
12
|
+
header: {},
|
|
13
|
+
dumper: DUMPER,
|
|
14
|
+
parser: PARSER,
|
|
15
|
+
&block)
|
|
16
|
+
@base, @type, @data, @header, @dumper, @parser, @block =
|
|
17
|
+
base, type, data, header, dumper, parser, block
|
|
18
|
+
@strict = false
|
|
19
|
+
@webmethods = {}
|
|
20
|
+
end
|
|
22
21
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
def strict!(t = true)
|
|
23
|
+
@strict = t
|
|
24
|
+
end
|
|
26
25
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
26
|
+
def add(method, target = method.to_s,
|
|
27
|
+
type: @type,
|
|
28
|
+
data: {},
|
|
29
|
+
header: {},
|
|
30
|
+
dumper: @dumper,
|
|
31
|
+
parser: @parser,
|
|
32
|
+
&block)
|
|
33
|
+
@webmethods[method] = WebApiMethod.new(@base+target,
|
|
34
|
+
type,
|
|
35
|
+
@data.merge(data),
|
|
36
|
+
@header.merge(header),
|
|
37
|
+
dumper,
|
|
38
|
+
parser,
|
|
39
|
+
&(block || @block))
|
|
40
|
+
end
|
|
42
41
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
end
|
|
54
|
-
extension, data = '', extension unless extension.is_a? String # user passed data as first argument
|
|
55
|
-
@webmethods[symbol].run(extension, type, data, header, dumper, parser, &block)
|
|
42
|
+
def method_missing(symbol , extension = '',
|
|
43
|
+
type: nil,
|
|
44
|
+
data: {},
|
|
45
|
+
header: {},
|
|
46
|
+
dumper: nil,
|
|
47
|
+
parser: nil,
|
|
48
|
+
&block)
|
|
49
|
+
unless @webmethods.has_key? symbol
|
|
50
|
+
super if @strict
|
|
51
|
+
add symbol
|
|
56
52
|
end
|
|
53
|
+
# user passed data as first argument?
|
|
54
|
+
extension, data = '', extension unless extension.is_a? String
|
|
55
|
+
@webmethods[symbol].run(extension, type, data, header, dumper, parser,
|
|
56
|
+
&block)
|
|
57
57
|
end
|
|
58
58
|
end
|
|
@@ -1,99 +1,96 @@
|
|
|
1
|
-
|
|
2
|
-
class
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
end
|
|
1
|
+
class WebApiMethod
|
|
2
|
+
class ResponseError < ::RuntimeError
|
|
3
|
+
end
|
|
4
|
+
class UnsupportedMethodError < ::RuntimeError
|
|
5
|
+
end
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
OK = 200..299
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
# Escapes value's string representation for query string use.
|
|
10
|
+
def escape(value)
|
|
11
|
+
#http://rosettacode.org/wiki/URL_encoding#Ruby
|
|
12
|
+
CGI.escape(value.to_s).gsub("+", "%20")
|
|
13
|
+
end
|
|
15
14
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
.join('&')
|
|
25
|
-
else
|
|
26
|
-
ah.to_s
|
|
27
|
-
end
|
|
15
|
+
def query_string(ah)
|
|
16
|
+
case ah
|
|
17
|
+
when Array
|
|
18
|
+
ah.select{!_1.empty?}.map{query_string _1}.join('&')
|
|
19
|
+
when Hash
|
|
20
|
+
ah.map{"#{_1}=#{escape _2}"}.join('&')
|
|
21
|
+
else
|
|
22
|
+
ah.to_s
|
|
28
23
|
end
|
|
24
|
+
end
|
|
29
25
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
26
|
+
def initialize(target, type, data, header, dumper, parser, &block)
|
|
27
|
+
@target, @type, @data, @header, @dumper, @parser, @block =
|
|
28
|
+
target, type, data, header, dumper, parser, block
|
|
29
|
+
end
|
|
34
30
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
else
|
|
44
|
-
payload = query_string(request.data)
|
|
45
|
-
end
|
|
31
|
+
def uri_parse(request)
|
|
32
|
+
uri = URI.parse(request.url)
|
|
33
|
+
request_uri = uri.request_uri
|
|
34
|
+
payload = ''
|
|
35
|
+
unless request.data.empty?
|
|
36
|
+
if request.type == :post
|
|
37
|
+
if dumper=request.dumper and (dumper!=:none)
|
|
38
|
+
payload = dumper.call(request.data)
|
|
46
39
|
else
|
|
47
40
|
payload = query_string(request.data)
|
|
48
|
-
request_uri += (uri.query ? '&' : '?') + payload
|
|
49
41
|
end
|
|
42
|
+
else
|
|
43
|
+
payload = query_string(request.data)
|
|
44
|
+
request_uri += (uri.query ? '&' : '?') + payload
|
|
50
45
|
end
|
|
51
|
-
request.scheme, request.host, request.port, request.uri, request.payload =
|
|
52
|
-
uri.scheme, uri.host, uri.port, request_uri, payload
|
|
53
46
|
end
|
|
47
|
+
request.scheme, request.host, request.port, request.uri, request.payload =
|
|
48
|
+
uri.scheme, uri.host, uri.port, request_uri, payload
|
|
49
|
+
end
|
|
54
50
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
51
|
+
def run(extension, type, data, header, dumper, parser, &block)
|
|
52
|
+
request = CRStruct::Open.new
|
|
53
|
+
request.url = @target+extension
|
|
54
|
+
request.type = type || @type
|
|
55
|
+
request.data = @data.merge(data)
|
|
56
|
+
request.header = @header.merge(header)
|
|
57
|
+
request.dumper = dumper || @dumper
|
|
62
58
|
|
|
63
|
-
|
|
64
|
-
|
|
59
|
+
parser = parser || @parser
|
|
60
|
+
block = block || @block
|
|
65
61
|
|
|
66
|
-
|
|
67
|
-
|
|
62
|
+
uri_parse(request)
|
|
63
|
+
block.call(request) if block
|
|
68
64
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
65
|
+
body, content = http_response_body(request)
|
|
66
|
+
parser = parser[content] if parser.is_a? Hash
|
|
67
|
+
body = parser.call body if parser and parser!=:none
|
|
68
|
+
return body
|
|
69
|
+
end
|
|
74
70
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
71
|
+
def http_response_body(request)
|
|
72
|
+
response = http_response(request)
|
|
73
|
+
raise ResponseError, response.message unless OK === response.code.to_i
|
|
74
|
+
return response.body, response['content-type'].sub(/;.*$/,'')
|
|
75
|
+
end
|
|
80
76
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
77
|
+
def http_response(request)
|
|
78
|
+
http = net_http(request)
|
|
79
|
+
case request.type
|
|
80
|
+
when :get, :delete, :head, :copy, :move, :options, :trace
|
|
81
|
+
http.method(request.type).call request.uri, request.header
|
|
82
|
+
when :post, :patch, :lock, :unlock, :mkcol, :propfind, :proppatch
|
|
83
|
+
http.method(request.type)
|
|
84
|
+
.call request.uri, request.payload, request.header
|
|
85
|
+
else
|
|
86
|
+
raise UnsupportedMethodError,
|
|
87
|
+
"Method type #{type}(#{type.class}) not supported by WebApi."
|
|
91
88
|
end
|
|
89
|
+
end
|
|
92
90
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
end
|
|
91
|
+
def net_http(request)
|
|
92
|
+
http = Net::HTTP.new(request.host, request.port)
|
|
93
|
+
http.use_ssl = request.scheme=='https'
|
|
94
|
+
return http
|
|
98
95
|
end
|
|
99
96
|
end
|
data/lib/web_api.rb
CHANGED
|
@@ -6,11 +6,10 @@ require 'cgi'
|
|
|
6
6
|
require 'crstruct'
|
|
7
7
|
|
|
8
8
|
# This Gem
|
|
9
|
-
|
|
10
|
-
VERSION = '0.
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
class WebApi
|
|
10
|
+
VERSION = '1.0.210927'
|
|
11
|
+
require_relative 'web_api/web_api_method.rb'
|
|
12
|
+
require_relative 'web_api/web_api.rb'
|
|
13
13
|
end
|
|
14
|
-
|
|
15
14
|
# Requires:
|
|
16
15
|
#`ruby`
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: web_api
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 1.0.210927
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
-
|
|
8
|
-
autorequire:
|
|
7
|
+
- CarlosJHR64
|
|
8
|
+
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2021-09-27 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: crstruct
|
|
@@ -16,14 +16,20 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - "~>"
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: 0.1
|
|
19
|
+
version: '0.1'
|
|
20
|
+
- - ">="
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: 0.1.210920
|
|
20
23
|
type: :runtime
|
|
21
24
|
prerelease: false
|
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
26
|
requirements:
|
|
24
27
|
- - "~>"
|
|
25
28
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: 0.1
|
|
29
|
+
version: '0.1'
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: 0.1.210920
|
|
27
33
|
description: 'Ruby library for web api''s.
|
|
28
34
|
|
|
29
35
|
'
|
|
@@ -40,7 +46,7 @@ homepage: https://github.com/carlosjhr64/web_api
|
|
|
40
46
|
licenses:
|
|
41
47
|
- MIT
|
|
42
48
|
metadata: {}
|
|
43
|
-
post_install_message:
|
|
49
|
+
post_install_message:
|
|
44
50
|
rdoc_options: []
|
|
45
51
|
require_paths:
|
|
46
52
|
- lib
|
|
@@ -55,9 +61,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
55
61
|
- !ruby/object:Gem::Version
|
|
56
62
|
version: '0'
|
|
57
63
|
requirements:
|
|
58
|
-
- 'ruby: ruby
|
|
59
|
-
rubygems_version: 3.
|
|
60
|
-
signing_key:
|
|
64
|
+
- 'ruby: ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux]'
|
|
65
|
+
rubygems_version: 3.2.22
|
|
66
|
+
signing_key:
|
|
61
67
|
specification_version: 4
|
|
62
68
|
summary: Ruby library for web api's.
|
|
63
69
|
test_files: []
|