web_api 0.3.191208 → 2.0.221220

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d807c131626709ce9df0f7e5fa92c96fd7a0df6868fc96a823be2ec823ef26aa
4
- data.tar.gz: a9c6ecf0c3a73b2a4d4018eb7882054c6029c85cc3decf7bca28a452f38313ee
3
+ metadata.gz: 45268acfd02c273b2cdc3287a654dbd673b6e1851d101ae35ec2cf50d8f24a55
4
+ data.tar.gz: 48566e13c223421ededbcfcc8433daa76e9f59b180c2cbd77069d0f63b71e9cf
5
5
  SHA512:
6
- metadata.gz: ef7a923eee894aa4f3346ccbc5a1b89601e58a17aac35e11821ed604ebc0c1354286466a3b4dbee9e5de78642bf646dc2e5fbb7319f5a06bd04d8c8df2609454
7
- data.tar.gz: cd959864b8da19658c3a54afb46b0bdd39bffb5a24658be664a51cfa2eb85cbe1a6da0d16085be7c6eaf354461f654de69b460a3dfa39ffce4383d6ab06f6bf4
6
+ metadata.gz: 5401701566ec8629ddf63a4bdd046b086505ad196ce5f7cf0766db9f281b5c49a430d84b14f4f7fffc1877d966287d281381b25a843724d57852df0f7f9649fd
7
+ data.tar.gz: 332efd0ad4edfa7d7c7734686c7b728831e655f0bc75fcb9e5255cf360c23c6f12ec4d70f599bf286e5c1f50d74e2b22567a090cb654d71ed170680cffab9797
data/README.md CHANGED
@@ -1,44 +1,52 @@
1
- # web_api
1
+ # WebApi
2
2
 
3
+ * [VERSION 2.0.221220](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
 
6
7
  ## DESCRIPTION:
7
8
 
8
- Ruby library for web api's.
9
+ Ruby library for web API's.
9
10
 
10
11
  ## SYNOPSIS:
11
-
12
- require 'web_api'
13
- webapi = WEB_API::WebApi.new "https://api.site.org/path-to-base/", header: {Authorization: "Bearer ABC123XYZ"}
14
- # for a post to https://api.site.org/path-to-base/resource...
15
- webapi.add(:resource, type: :post)
16
- # You can pass the post's (or query's) key value pairs in a hash.
17
- body = webapi.resource(data: {key: "value"})
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
- $ gem install web_api
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 writting.
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
 
30
32
  The model is that at each step...
31
33
 
32
- 1. instantiation of a WebApi object
33
- 2. addition of a WebApi method
34
- 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
35
37
 
36
- ...one builds up the url, type, data, and header of the http request.
37
- The WebApi methods `#new`, `#add`, and `#<method>` all have the same argument signature:
38
+ ...one builds up the URL, type, data, and header of the HTTP request.
39
+ The `WebApi` methods `#new`, `#add`, and `#<method>`
40
+ all have the same argument signature:
38
41
 
39
- extension String, type: Symbol, data: Hash, header: Hash, dumper: Proc, Parser: Proc|Hash(String, Proc)
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 concatanation.
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,30 @@ 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 extenstion is given.
57
+ no extension is given.
50
58
 
51
- Note that `#<method>` will assume the user meant to pass data if it only gets a hash, but
52
- the hash must then not have any `Symbol` for its keys:
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"}) #=> #<method>('', data: {"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 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.
57
67
 
58
- The parser to parse the body of an "application/json" type content is JSON.parse by default if available.
59
- You can read the code and inspect `WEB_API::WebApi::PARSER` 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.
60
70
 
61
- If one does not want to parse the reponse's body,
71
+ If one does not want to parse the response's body,
62
72
  one can set `parser: :none`. For example:
63
-
64
- body = webapi.resourse(data: {key: "value"}, parser: :none)
65
-
73
+ ```ruby
74
+ body = webapi.resourse(data: {key: "value"}, parser: :none)
75
+ ```
66
76
  ## LICENSE:
67
77
 
68
78
  (The MIT License)
69
79
 
70
- Copyright (c) 2019 CarlosJHR64
80
+ Copyright (c) 2022 CarlosJHR64
71
81
 
72
82
  Permission is hereby granted, free of charge, to any person obtaining
73
83
  a copy of this software and associated documentation files (the
@@ -1,58 +1,63 @@
1
- module WEB_API
2
- class WebApi
3
- DUMPER = (defined? JSON)? JSON.method(:dump) : :none
1
+ class WebApi
2
+ def WebApi.get_dumper
3
+ (defined? JSON)? JSON.method(:dump) : :none
4
+ end
4
5
 
5
- PARSER = Hash.new :none
6
- PARSER['application/json'] = JSON.method(:parse) if defined? JSON
7
- PARSER['text/csv'] = CSV.method(:parse) if defined? CSV
8
- PARSER['text/html'] = Nokogiri::HTML.method(:parse) if defined? Nokogiri
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
9
13
 
10
- def initialize(base = '',
11
- type: :get,
12
- data: {},
13
- header: {},
14
- dumper: DUMPER,
15
- parser: PARSER,
16
- &block)
17
- @base, @type, @data, @header, @dumper, @parser, @block =
18
- base, type, data, header, dumper, parser, block
19
- @strict = false
20
- @webmethods = {}
21
- end
14
+ def initialize(base = '',
15
+ type: :get,
16
+ data: {},
17
+ header: {},
18
+ dumper: WebApi.get_dumper,
19
+ parser: WebApi.get_parsers,
20
+ &block)
21
+ @base, @type, @data, @header, @dumper, @parser, @block =
22
+ base, type, data, header, dumper, parser, block
23
+ @strict = false
24
+ @webmethods = {}
25
+ end
22
26
 
23
- def strict!(t = true)
24
- @strict = t
25
- end
27
+ def strict!(t = true)
28
+ @strict = t
29
+ end
26
30
 
27
- def add(method, target = method.to_s,
28
- type: @type,
29
- data: {},
30
- header: {},
31
- dumper: @dumper,
32
- parser: @parser,
33
- &block)
34
- @webmethods[method] = WebApiMethod.new(@base+target,
35
- type,
36
- @data.merge(data),
37
- @header.merge(header),
38
- dumper,
39
- parser,
40
- &(block || @block))
41
- end
31
+ def add(method, target = method.to_s,
32
+ type: @type,
33
+ data: {},
34
+ header: {},
35
+ dumper: @dumper,
36
+ parser: @parser,
37
+ &block)
38
+ @webmethods[method] = WebApiMethod.new(@base+target,
39
+ type,
40
+ @data.merge(data),
41
+ @header.merge(header),
42
+ dumper,
43
+ parser,
44
+ &(block || @block))
45
+ end
42
46
 
43
- def method_missing(symbol , extension = '',
44
- type: nil,
45
- data: {},
46
- header: {},
47
- dumper: nil,
48
- parser: nil,
49
- &block)
50
- unless @webmethods.has_key? symbol
51
- super if @strict
52
- add symbol
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)
47
+ def method_missing(symbol , extension = '',
48
+ type: nil,
49
+ data: {},
50
+ header: {},
51
+ dumper: nil,
52
+ parser: nil,
53
+ &block)
54
+ unless @webmethods.has_key? symbol
55
+ super if @strict
56
+ add symbol
56
57
  end
58
+ # user passed data as first argument?
59
+ extension, data = '', extension unless extension.is_a? String
60
+ @webmethods[symbol].run(extension, type, data, header, dumper, parser,
61
+ &block)
57
62
  end
58
63
  end
@@ -1,99 +1,103 @@
1
- module WEB_API
2
- class WebApiMethod
3
- class ResponseError < ::RuntimeError
4
- end
5
- class UnsupportedMethodError < ::RuntimeError
6
- end
1
+ class WebApiMethod
2
+ class ResponseError < ::RuntimeError
3
+ end
4
+ class UnsupportedMethodError < ::RuntimeError
5
+ end
7
6
 
8
- OK = 200..299
7
+ OK = 200..299
9
8
 
10
- # Escapes value's string representation for query string use.
11
- def escape(value)
12
- #http://rosettacode.org/wiki/URL_encoding#Ruby
13
- CGI.escape(value.to_s).gsub("+", "%20")
14
- end
9
+ # Escapes value's string representation for query string use.
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
+ end
15
14
 
16
- def query_string(ah)
17
- case ah
18
- when Array
19
- ah.select{|h| !h.empty?}.map{|h| query_string(h)}.join('&')
20
- when Hash
21
- ah.map do |k,v|
22
- "#{k}=#{escape v}"
23
- end
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
- def initialize(target, type, data, header, dumper, parser, &block)
31
- @target, @type, @data, @header, @dumper, @parser, @block =
32
- target, type, data, header, dumper, parser, block
33
- end
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
- def uri_parse(request)
36
- uri = URI.parse(request.url)
37
- request_uri = uri.request_uri
38
- payload = ''
39
- unless request.data.empty?
40
- if request.type == :post
41
- if dumper=request.dumper and (dumper!=:none)
42
- payload = dumper.call(request.data)
43
- else
44
- payload = query_string(request.data)
45
- end
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
34
+ def uri_parse(request)
35
+ uri = URI.parse(request.url)
36
+ request_uri = uri.request_uri
37
+ payload = ''
38
+ unless request.data.empty?
39
+ if request.type == :post
40
+ if dumper=request.dumper and (dumper!=:none)
41
+ payload = dumper.call(request.data)
46
42
  else
47
43
  payload = query_string(request.data)
48
- request_uri += (uri.query ? '&' : '?') + payload
49
44
  end
45
+ else
46
+ payload = query_string(request.data)
47
+ request_uri += (uri.query ? '&' : '?') + payload
50
48
  end
51
- request.scheme, request.host, request.port, request.uri, request.payload =
52
- uri.scheme, uri.host, uri.port, request_uri, payload
53
49
  end
50
+ request.scheme, request.host, request.port, request.uri, request.payload =
51
+ uri.scheme, uri.host, uri.port, request_uri, payload
52
+ end
54
53
 
55
- def run(extension, type, data, header, dumper, parser, &block)
56
- request = CRStruct::Open.new
57
- request.url = @target+extension
58
- request.type = type || @type
59
- request.data = @data.merge(data)
60
- request.header = @header.merge(header)
61
- request.dumper = dumper || @dumper
54
+ def run(extension, type, data, header, dumper, parser, &block)
55
+ request = CRStruct::Open.new
56
+ request.url = @target+extension
57
+ request.type = type || @type
58
+ request.data = @data.merge(data)
59
+ request.header = @header.merge(header)
60
+ request.dumper = dumper || @dumper
62
61
 
63
- parser = parser || @parser
64
- block = block || @block
62
+ parser ||= @parser
63
+ block ||= @block
65
64
 
66
- uri_parse(request)
67
- block.call(request) if block
65
+ uri_parse(request)
66
+ block.call(request) if block
68
67
 
69
- body, content = http_response_body(request)
70
- parser = parser[content] if parser.is_a? Hash
71
- body = parser.call body if parser and parser!=:none
72
- return body
73
- end
68
+ body, content = http_response_body(request)
69
+ parser = parser[content] if parser.is_a? Hash
70
+ body = parser.call(body) if parser and parser!=:none
71
+ return body
72
+ end
74
73
 
75
- def http_response_body(request)
76
- response = http_response(request)
77
- raise ResponseError, response.message unless OK === response.code.to_i
78
- return response.body, response['content-type'].sub(/;.*$/,'')
74
+ def http_response_body(request)
75
+ response = http_response(request)
76
+ unless OK === response.code.to_i
77
+ $stderr.puts response.code
78
+ $stderr.puts response.body
79
+ raise ResponseError, response.message
79
80
  end
81
+ return response.body, response['content-type'].sub(/;.*$/,'')
82
+ end
80
83
 
81
- def http_response(request)
82
- http = net_http(request)
83
- case request.type
84
- when :get, :delete, :head, :copy, :move, :options, :trace
85
- http.method(request.type).call request.uri, request.header
86
- when :post, :patch, :lock, :unlock, :mkcol, :propfind, :proppatch
87
- http.method(request.type).call request.uri, request.payload, request.header
88
- else
89
- raise UnsupportedMethodError, "Method type #{type}(#{type.class}) not supported by WEB_API::WebApi."
90
- end
84
+ def http_response(request)
85
+ http = net_http(request)
86
+ case request.type
87
+ when :get, :delete, :head, :copy, :move, :options, :trace
88
+ http.method(request.type).call request.uri, request.header
89
+ when :post, :patch, :lock, :unlock, :mkcol, :propfind, :proppatch
90
+ http.method(request.type)
91
+ .call request.uri, request.payload, request.header
92
+ else
93
+ raise UnsupportedMethodError,
94
+ "Method type #{type}(#{type.class}) not supported by WebApi."
91
95
  end
96
+ end
92
97
 
93
- def net_http(request)
94
- http = Net::HTTP.new(request.host, request.port)
95
- http.use_ssl = request.scheme=='https'
96
- return http
97
- end
98
+ def net_http(request)
99
+ http = Net::HTTP.new(request.host, request.port)
100
+ http.use_ssl = request.scheme=='https'
101
+ return http
98
102
  end
99
103
  end
data/lib/web_api.rb CHANGED
@@ -1,16 +1,12 @@
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
- module WEB_API
10
- VERSION = '0.3.191208'
11
- require 'web_api/web_api_method.rb'
12
- require 'web_api/web_api.rb'
6
+ class WebApi
7
+ VERSION = '2.0.221220'
8
+ require_relative 'web_api/web_api_method.rb'
9
+ require_relative 'web_api/web_api.rb'
13
10
  end
14
-
15
11
  # Requires:
16
12
  #`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.3.191208
4
+ version: 2.0.221220
5
5
  platform: ruby
6
6
  authors:
7
- - carlosjhr64
7
+ - CarlosJHR64
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-08 00:00:00.000000000 Z
11
+ date: 2022-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: crstruct
@@ -16,15 +16,21 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.191207
19
+ version: '1.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.0.221218
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.191207
27
- description: 'Ruby library for web api''s.
29
+ version: '1.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.221218
33
+ description: 'Ruby library for web API''s.
28
34
 
29
35
  '
30
36
  email: carlosjhr64@gmail.com
@@ -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 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]'
59
- rubygems_version: 3.0.3
64
+ - 'ruby: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [aarch64-linux]'
65
+ rubygems_version: 3.3.7
60
66
  signing_key:
61
67
  specification_version: 4
62
- summary: Ruby library for web api's.
68
+ summary: Ruby library for web API's.
63
69
  test_files: []