web_api 1.0.210927 → 2.0.221220
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 +13 -15
- data/lib/web_api/web_api.rb +12 -7
- data/lib/web_api/web_api_method.rb +15 -8
- data/lib/web_api.rb +1 -4
- metadata +13 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 45268acfd02c273b2cdc3287a654dbd673b6e1851d101ae35ec2cf50d8f24a55
|
4
|
+
data.tar.gz: 48566e13c223421ededbcfcc8433daa76e9f59b180c2cbd77069d0f63b71e9cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5401701566ec8629ddf63a4bdd046b086505ad196ce5f7cf0766db9f281b5c49a430d84b14f4f7fffc1877d966287d281381b25a843724d57852df0f7f9649fd
|
7
|
+
data.tar.gz: 332efd0ad4edfa7d7c7734686c7b728831e655f0bc75fcb9e5255cf360c23c6f12ec4d70f599bf286e5c1f50d74e2b22567a090cb654d71ed170680cffab9797
|
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# WebApi
|
2
2
|
|
3
|
-
* [VERSION
|
3
|
+
* [VERSION 2.0.221220](https://www.github.com/carlosjhr64/web_api)
|
4
4
|
* [github](https://www.github.com/carlosjhr64/web_api)
|
5
5
|
* [rubygems](https://rubygems.org/gems/web_api)
|
6
6
|
|
7
7
|
## DESCRIPTION:
|
8
8
|
|
9
|
-
Ruby library for web
|
9
|
+
Ruby library for web API's.
|
10
10
|
|
11
11
|
## SYNOPSIS:
|
12
12
|
```ruby
|
@@ -25,18 +25,18 @@ $ gem install web_api
|
|
25
25
|
## MORE:
|
26
26
|
|
27
27
|
There's not that much code here...
|
28
|
-
|
28
|
+
Under 200 lines in `lib/**.rb` at the time of this writing.
|
29
29
|
Take a look at the examples given at [github](https://github.com/carlosjhr64/web_api/tree/master/examples)
|
30
30
|
for use cases.
|
31
31
|
|
32
32
|
The model is that at each step...
|
33
33
|
|
34
|
-
1. instantiation of a WebApi object
|
35
|
-
2. addition of a WebApi method
|
36
|
-
3. call to a WebApi method
|
34
|
+
1. instantiation of a `WebApi` object
|
35
|
+
2. addition of a `WebApi` method
|
36
|
+
3. call to a `WebApi` method
|
37
37
|
|
38
|
-
...one builds up the
|
39
|
-
The WebApi methods `#new`, `#add`, and `#<method>`
|
38
|
+
...one builds up the URL, type, data, and header of the HTTP request.
|
39
|
+
The `WebApi` methods `#new`, `#add`, and `#<method>`
|
40
40
|
all have the same argument signature:
|
41
41
|
|
42
42
|
extension String,
|
@@ -62,13 +62,11 @@ it only gets a hash:
|
|
62
62
|
#<method>({"a"=>"ABC","x"=>"XYZ"})
|
63
63
|
#=> #<method>('', data: {"a"=>"ABC","x"=>"XYZ"})
|
64
64
|
|
65
|
-
The dumper to dump the data in a post request is JSON.dump by default
|
66
|
-
if JSON is available.
|
65
|
+
The dumper to dump the data in a post request is `JSON.dump` by default
|
66
|
+
if `JSON` is available.
|
67
67
|
|
68
|
-
The parser to parse the body of an
|
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.
|
68
|
+
The parser to parse the body of an `application/json` type content is
|
69
|
+
`JSON.parse` by default if available.
|
72
70
|
|
73
71
|
If one does not want to parse the response's body,
|
74
72
|
one can set `parser: :none`. For example:
|
@@ -79,7 +77,7 @@ body = webapi.resourse(data: {key: "value"}, parser: :none)
|
|
79
77
|
|
80
78
|
(The MIT License)
|
81
79
|
|
82
|
-
Copyright (c)
|
80
|
+
Copyright (c) 2022 CarlosJHR64
|
83
81
|
|
84
82
|
Permission is hereby granted, free of charge, to any person obtaining
|
85
83
|
a copy of this software and associated documentation files (the
|
data/lib/web_api/web_api.rb
CHANGED
@@ -1,17 +1,22 @@
|
|
1
1
|
class WebApi
|
2
|
-
|
2
|
+
def WebApi.get_dumper
|
3
|
+
(defined? JSON)? JSON.method(:dump) : :none
|
4
|
+
end
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
def WebApi.get_parsers
|
7
|
+
parsers = Hash.new :none
|
8
|
+
parsers['application/json'] = JSON.method(:parse) if defined? JSON
|
9
|
+
parsers['text/csv'] = CSV.method(:parse) if defined? CSV
|
10
|
+
parsers['text/html'] = Nokogiri::HTML.method(:parse) if defined? Nokogiri
|
11
|
+
return parsers
|
12
|
+
end
|
8
13
|
|
9
14
|
def initialize(base = '',
|
10
15
|
type: :get,
|
11
16
|
data: {},
|
12
17
|
header: {},
|
13
|
-
dumper:
|
14
|
-
parser:
|
18
|
+
dumper: WebApi.get_dumper,
|
19
|
+
parser: WebApi.get_parsers,
|
15
20
|
&block)
|
16
21
|
@base, @type, @data, @header, @dumper, @parser, @block =
|
17
22
|
base, type, data, header, dumper, parser, block
|
@@ -7,9 +7,9 @@ class WebApiMethod
|
|
7
7
|
OK = 200..299
|
8
8
|
|
9
9
|
# Escapes value's string representation for query string use.
|
10
|
-
def escape(
|
11
|
-
#
|
12
|
-
|
10
|
+
def escape(s)
|
11
|
+
# Contraction of ERB#url_encode method
|
12
|
+
s.to_s.b.gsub(/[^\w\-.~]/n){sprintf("%%%02X",_1.unpack1("C"))}
|
13
13
|
end
|
14
14
|
|
15
15
|
def query_string(ah)
|
@@ -28,6 +28,9 @@ class WebApiMethod
|
|
28
28
|
target, type, data, header, dumper, parser, block
|
29
29
|
end
|
30
30
|
|
31
|
+
# uri_parse(request: CRStruct) -> N/A
|
32
|
+
# Expects request to have url, data, type, and dumper.
|
33
|
+
# Sets the request's scheme, host, port, uri, and payload
|
31
34
|
def uri_parse(request)
|
32
35
|
uri = URI.parse(request.url)
|
33
36
|
request_uri = uri.request_uri
|
@@ -56,21 +59,25 @@ class WebApiMethod
|
|
56
59
|
request.header = @header.merge(header)
|
57
60
|
request.dumper = dumper || @dumper
|
58
61
|
|
59
|
-
parser
|
60
|
-
block
|
62
|
+
parser ||= @parser
|
63
|
+
block ||= @block
|
61
64
|
|
62
65
|
uri_parse(request)
|
63
66
|
block.call(request) if block
|
64
67
|
|
65
68
|
body, content = http_response_body(request)
|
66
|
-
parser = parser[content]
|
67
|
-
body
|
69
|
+
parser = parser[content] if parser.is_a? Hash
|
70
|
+
body = parser.call(body) if parser and parser!=:none
|
68
71
|
return body
|
69
72
|
end
|
70
73
|
|
71
74
|
def http_response_body(request)
|
72
75
|
response = http_response(request)
|
73
|
-
|
76
|
+
unless OK === response.code.to_i
|
77
|
+
$stderr.puts response.code
|
78
|
+
$stderr.puts response.body
|
79
|
+
raise ResponseError, response.message
|
80
|
+
end
|
74
81
|
return response.body, response['content-type'].sub(/;.*$/,'')
|
75
82
|
end
|
76
83
|
|
data/lib/web_api.rb
CHANGED
@@ -1,13 +1,10 @@
|
|
1
1
|
# Standard Libraries
|
2
2
|
require 'net/http'
|
3
|
-
require 'cgi'
|
4
|
-
|
5
3
|
# External Gems
|
6
4
|
require 'crstruct'
|
7
|
-
|
8
5
|
# This Gem
|
9
6
|
class WebApi
|
10
|
-
VERSION = '
|
7
|
+
VERSION = '2.0.221220'
|
11
8
|
require_relative 'web_api/web_api_method.rb'
|
12
9
|
require_relative 'web_api/web_api.rb'
|
13
10
|
end
|
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:
|
4
|
+
version: 2.0.221220
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- CarlosJHR64
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: crstruct
|
@@ -16,21 +16,21 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0
|
19
|
+
version: '1.0'
|
20
20
|
- - ">="
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: 0.
|
22
|
+
version: 1.0.221218
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - "~>"
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: '0
|
29
|
+
version: '1.0'
|
30
30
|
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: 0.
|
33
|
-
description: 'Ruby library for web
|
32
|
+
version: 1.0.221218
|
33
|
+
description: 'Ruby library for web API''s.
|
34
34
|
|
35
35
|
'
|
36
36
|
email: carlosjhr64@gmail.com
|
@@ -46,7 +46,7 @@ homepage: https://github.com/carlosjhr64/web_api
|
|
46
46
|
licenses:
|
47
47
|
- MIT
|
48
48
|
metadata: {}
|
49
|
-
post_install_message:
|
49
|
+
post_install_message:
|
50
50
|
rdoc_options: []
|
51
51
|
require_paths:
|
52
52
|
- lib
|
@@ -61,9 +61,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
61
|
- !ruby/object:Gem::Version
|
62
62
|
version: '0'
|
63
63
|
requirements:
|
64
|
-
- 'ruby: ruby 3.
|
65
|
-
rubygems_version: 3.
|
66
|
-
signing_key:
|
64
|
+
- 'ruby: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [aarch64-linux]'
|
65
|
+
rubygems_version: 3.3.7
|
66
|
+
signing_key:
|
67
67
|
specification_version: 4
|
68
|
-
summary: Ruby library for web
|
68
|
+
summary: Ruby library for web API's.
|
69
69
|
test_files: []
|