webclient 0.2.1 → 0.2.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 19d01c484b19fd633a6e73b3710d38dad056bd066e7c2dc9c613ddd1eae4c28f
4
- data.tar.gz: 7140effe816c143e7ff9e336dadd9b1bd7b424ee6ca0c1b4ff75a12021fe0979
3
+ metadata.gz: 8db40d5a48638657362e50374418f3f911079afbdf9852a6e99038a6dd46f549
4
+ data.tar.gz: 334f393fc27e197a685200c5f9a85f34c7940ac7c37bce7a80dcef971d642daf
5
5
  SHA512:
6
- metadata.gz: aa8feda707d1e1c79bcaf9205e768465e21d2c39b8cba9143387816bd17d5df34f7e871d43ece9a3780a1a3577b701017633823e5e695635eb73d4f8ec954f0d
7
- data.tar.gz: af08cb829652c2ba30600dd7163bba31dd9bc1e5bdbfc8d5f9d289ac285fcde5f878de541f4523e07906cdbb41bb25ebca632bae28df4113e741aa4b9e160512
6
+ metadata.gz: 81d41206af0b7c517318f908f4ce7482bb11849470ef1631c949a6479509d177ac2027c79f7cf18eb7b007cb0150f45d73e267f270b7e0a717482f002c4076fa
7
+ data.tar.gz: 2312bfe78c5c621a5b58e4498c9f5c0adbc2ecca929bbf73ccaf0f3c79f07d5ec823f234536bcfd22773506dfd1b891a29ac202081a05551b39b4b3f4ac7aabe
data/CHANGELOG.md CHANGED
@@ -1,4 +1,4 @@
1
- ### 0.0.1 / 2020-10-03
2
-
3
- * Everything is new. First release.
4
-
1
+ ### 0.0.1 / 2020-10-03
2
+
3
+ * Everything is new. First release.
4
+
data/README.md CHANGED
@@ -1,22 +1,22 @@
1
- # webclient
2
-
3
- webclient gem - yet (another) universal network client interface for world wide web (www) requests via HTTP
4
-
5
-
6
- * home :: [github.com/rubycoco/webclient](https://github.com/rubycoco/webclient)
7
- * bugs :: [github.com/rubycoco/webclient/issues](https://github.com/rubycoco/webclient/issues)
8
- * gem :: [rubygems.org/gems/webclient](https://rubygems.org/gems/webclient)
9
- * rdoc :: [rubydoc.info/gems/webclient](http://rubydoc.info/gems/webclient)
10
- * forum :: [groups.google.com/group/wwwmake](https://groups.google.com/group/wwwmake)
11
-
12
-
13
-
14
- ## Usage
15
-
16
- TBD
17
-
18
-
19
- ## License
20
-
21
- The `webclient` scripts are dedicated to the public domain.
22
- Use it as you please with no restrictions whatsoever.
1
+ # webclient
2
+
3
+ webclient gem - yet (another) universal network client interface for world wide web (www) requests via HTTP
4
+
5
+
6
+ * home :: [github.com/rubycoco/webclient](https://github.com/rubycoco/webclient)
7
+ * bugs :: [github.com/rubycoco/webclient/issues](https://github.com/rubycoco/webclient/issues)
8
+ * gem :: [rubygems.org/gems/webclient](https://rubygems.org/gems/webclient)
9
+ * rdoc :: [rubydoc.info/gems/webclient](http://rubydoc.info/gems/webclient)
10
+ * forum :: [groups.google.com/group/wwwmake](https://groups.google.com/group/wwwmake)
11
+
12
+
13
+
14
+ ## Usage
15
+
16
+ TBD
17
+
18
+
19
+ ## License
20
+
21
+ The `webclient` scripts are dedicated to the public domain.
22
+ Use it as you please with no restrictions whatsoever.
data/Rakefile CHANGED
@@ -1,28 +1,28 @@
1
- require 'hoe'
2
- require './lib/webclient/version.rb'
3
-
4
- Hoe.spec 'webclient' do
5
-
6
- self.version = Webclient::VERSION
7
-
8
- self.summary = 'webclient gem - yet (another) universal network client interface for world wide web (www) requests via HTTP'
9
- self.description = summary
10
-
11
- self.urls = { home: 'https://github.com/rubycoco/webclient' }
12
-
13
- self.author = 'Gerald Bauer'
14
- self.email = 'ruby-talk@ruby-lang.org'
15
-
16
- # switch extension to .markdown for gihub formatting
17
- self.readme_file = 'README.md'
18
- self.history_file = 'CHANGELOG.md'
19
-
20
- self.extra_deps = []
21
-
22
- self.licenses = ['Public Domain']
23
-
24
- self.spec_extras = {
25
- required_ruby_version: '>= 2.2.2'
26
- }
27
-
28
- end
1
+ require 'hoe'
2
+ require './lib/webclient/version.rb'
3
+
4
+ Hoe.spec 'webclient' do
5
+
6
+ self.version = Webclient::VERSION
7
+
8
+ self.summary = 'webclient gem - yet (another) universal network client interface for world wide web (www) requests via HTTP'
9
+ self.description = summary
10
+
11
+ self.urls = { home: 'https://github.com/rubycoco/webclient' }
12
+
13
+ self.author = 'Gerald Bauer'
14
+ self.email = 'ruby-talk@ruby-lang.org'
15
+
16
+ # switch extension to .markdown for gihub formatting
17
+ self.readme_file = 'README.md'
18
+ self.history_file = 'CHANGELOG.md'
19
+
20
+ self.extra_deps = []
21
+
22
+ self.licenses = ['Public Domain']
23
+
24
+ self.spec_extras = {
25
+ required_ruby_version: '>= 2.2.2'
26
+ }
27
+
28
+ end
@@ -1,20 +1,20 @@
1
-
2
- class Webclient
3
- MAJOR = 0 ## todo: namespace inside version or something - why? why not??
4
- MINOR = 2
5
- PATCH = 1
6
- VERSION = [MAJOR,MINOR,PATCH].join('.')
7
-
8
- def self.version
9
- VERSION
10
- end
11
-
12
- def self.banner
13
- "webclient/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}] in (#{root})"
14
- end
15
-
16
- def self.root
17
- File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )
18
- end
19
- end # module Webclient
20
-
1
+
2
+ class Webclient
3
+ MAJOR = 0 ## todo: namespace inside version or something - why? why not??
4
+ MINOR = 2
5
+ PATCH = 2
6
+ VERSION = [MAJOR,MINOR,PATCH].join('.')
7
+
8
+ def self.version
9
+ VERSION
10
+ end
11
+
12
+ def self.banner
13
+ "webclient/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}] in (#{root})"
14
+ end
15
+
16
+ def self.root
17
+ File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )
18
+ end
19
+ end # module Webclient
20
+
@@ -1,170 +1,202 @@
1
-
2
- class Webclient
3
-
4
- class Response # nested class - wrap Net::HTTP::Response
5
- def initialize( response )
6
- @response = response
7
- end
8
- def raw() @response; end
9
-
10
-
11
- ## todo/check: rename encoding to html/http-like charset - why? why not?
12
- def text( encoding: 'UTF-8' )
13
- # note: Net::HTTP will NOT set encoding UTF-8 etc.
14
- # will be set to ASCII-8BIT == BINARY == Encoding Unknown; Raw Bytes Here
15
- # thus, set/force encoding to utf-8
16
- text = @response.body.to_s
17
- if encoding.downcase == 'utf-8'
18
- text = text.force_encoding( Encoding::UTF_8 )
19
- else
20
- ## [debug] GET=http://www.football-data.co.uk/mmz4281/0405/SC0.csv
21
- ## Encoding::UndefinedConversionError: "\xA0" from ASCII-8BIT to UTF-8
22
- ## note: 0xA0 (160) is NBSP (non-breaking space) in Windows-1252
23
-
24
- ## note: assume windows encoding (for football-data.uk)
25
- ## use "Windows-1252" for input and convert to utf-8
26
- ##
27
- ## see https://www.justinweiss.com/articles/3-steps-to-fix-encoding-problems-in-ruby/
28
- ## see https://en.wikipedia.org/wiki/Windows-1252
29
- ## txt = txt.force_encoding( 'Windows-1252' )
30
- ## txt = txt.encode( 'UTF-8' )
31
- ## Encoding::UTF_8 => 'UTF-8'
32
- puts " [debug] converting response.text encoding from >#{encoding}< to >UTF-8<"
33
-
34
- text = text.force_encoding( encoding )
35
- text = text.encode( Encoding::UTF_8 )
36
- end
37
-
38
- text
39
- end
40
-
41
- ## convenience helper; returns parsed json data; note: always assume utf-8 (text) encoding
42
- def json() JSON.parse( text ); end
43
-
44
-
45
-
46
- class Headers # nested (nested) class
47
- def initialize( response )
48
- @response = response
49
- end
50
- def each( &blk )
51
- @response.each_header do |key, value| # Iterate all response headers
52
- blk.call( key, value )
53
- end
54
- end
55
- end
56
- def headers() @headers ||= Headers.new( @response ); end
57
-
58
- class Status # nested (nested) class
59
- def initialize( response )
60
- @response = response
61
- end
62
- def code() @response.code.to_i; end
63
- def ok?() code == 200; end
64
- def nok?() code != 200; end
65
- def message() @response.message; end
66
- end
67
- def status() @status ||= Status.new( @response ); end
68
- end # (nested) class Response
69
-
70
-
71
- def self.get( url, headers: {}, auth: [] )
72
-
73
- uri = URI.parse( url )
74
- http = Net::HTTP.new( uri.host, uri.port )
75
-
76
- if uri.instance_of? URI::HTTPS
77
- http.use_ssl = true
78
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
79
- end
80
-
81
- request = Net::HTTP::Get.new( uri.request_uri )
82
-
83
- ### add (custom) headers if any
84
- ## check/todo: is there are more idiomatic way for Net::HTTP ???
85
- ## use
86
- ## request = Net::HTTP::Get.new( uri.request_uri, headers )
87
- ## why? why not?
88
- ## instead of e.g.
89
- ## request['X-Auth-Token'] = 'xxxxxxx'
90
- ## request['User-Agent'] = 'ruby'
91
- ## request['Accept'] = '*/*'
92
- if headers && headers.size > 0
93
- headers.each do |key,value|
94
- request[ key ] = value
95
- end
96
- end
97
-
98
-
99
- if auth.size == 2 ## e.g. ['user', 'password']
100
- ## always assume basic auth for now
101
- ## auth[0] => user
102
- ## auth[1] => password
103
- request.basic_auth( auth[0], auth[1] )
104
- puts " using basic auth - user: #{auth[0]}, password: ***"
105
- end
106
-
107
-
108
- puts "GET #{uri}..."
109
-
110
- response = http.request( request )
111
-
112
- ## note: return "unified" wrapped response
113
- Response.new( response )
114
- end # method self.get
115
-
116
-
117
- def self.post( url, headers: {},
118
- body: nil,
119
- json: nil ## json - convenience shortcut (for body & encoding)
120
- )
121
-
122
- uri = URI.parse( url )
123
- http = Net::HTTP.new( uri.host, uri.port )
124
-
125
- if uri.instance_of? URI::HTTPS
126
- http.use_ssl = true
127
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
128
- end
129
-
130
- request = Net::HTTP::Post.new( uri.request_uri )
131
-
132
- ### add (custom) headers if any
133
- ## check/todo: is there are more idiomatic way for Net::HTTP ???
134
- ## use
135
- ## request = Net::HTTP::Get.new( uri.request_uri, headers )
136
- ## why? why not?
137
- ## instead of e.g.
138
- ## request['X-Auth-Token'] = 'xxxxxxx'
139
- ## request['User-Agent'] = 'ruby'
140
- ## request['Accept'] = '*/*'
141
- if headers && headers.size > 0
142
- headers.each do |key,value|
143
- request[ key ] = value
144
- end
145
- end
146
-
147
- if body
148
- request.body = body.to_s
149
- end
150
-
151
- if json
152
- # note: the body needs to be a JSON string - use pretty generate and NOT "compact" style - why? why not?
153
- request.body = JSON.pretty_generate( json )
154
-
155
- ## move (auto-set) header content-type up (before custom headers) - why? why not?
156
- request['Content-Type'] = 'application/json'
157
- end
158
-
159
-
160
- puts "POST #{uri}..."
161
-
162
- response = http.request( request )
163
-
164
- ## note: return "unified" wrapped response
165
- Response.new( response )
166
- end # method self.post
167
-
168
-
169
- end # class Webclient
170
-
1
+
2
+ class Webclient
3
+
4
+ class Response # nested class - wrap Net::HTTP::Response
5
+ def initialize( response )
6
+ @response = response
7
+ end
8
+ def raw() @response; end
9
+
10
+
11
+ ## todo/check: rename encoding to html/http-like charset - why? why not?
12
+ def text( encoding: 'UTF-8' )
13
+ # note: Net::HTTP will NOT set encoding UTF-8 etc.
14
+ # will be set to ASCII-8BIT == BINARY == Encoding Unknown; Raw Bytes Here
15
+ # thus, set/force encoding to utf-8
16
+ text = @response.body.to_s
17
+ if encoding.downcase == 'utf-8'
18
+ text = text.force_encoding( Encoding::UTF_8 )
19
+ else
20
+ ## [debug] GET=http://www.football-data.co.uk/mmz4281/0405/SC0.csv
21
+ ## Encoding::UndefinedConversionError: "\xA0" from ASCII-8BIT to UTF-8
22
+ ## note: 0xA0 (160) is NBSP (non-breaking space) in Windows-1252
23
+
24
+ ## note: assume windows encoding (for football-data.uk)
25
+ ## use "Windows-1252" for input and convert to utf-8
26
+ ##
27
+ ## see https://www.justinweiss.com/articles/3-steps-to-fix-encoding-problems-in-ruby/
28
+ ## see https://en.wikipedia.org/wiki/Windows-1252
29
+ ## txt = txt.force_encoding( 'Windows-1252' )
30
+ ## txt = txt.encode( 'UTF-8' )
31
+ ## Encoding::UTF_8 => 'UTF-8'
32
+ puts " [debug] converting response.text encoding from >#{encoding}< to >UTF-8<"
33
+
34
+ text = text.force_encoding( encoding )
35
+ text = text.encode( Encoding::UTF_8 )
36
+ end
37
+
38
+ text
39
+ end
40
+
41
+ ## convenience helper; returns parsed json data; note: always assume utf-8 (text) encoding
42
+ def json() JSON.parse( text ); end
43
+
44
+
45
+ def body() @response.body.to_s; end
46
+ alias_method :blob, :body
47
+
48
+
49
+
50
+ class Headers # nested (nested) class
51
+ def initialize( response )
52
+ @response = response
53
+ end
54
+ def each( &blk )
55
+ @response.each_header do |key, value| # Iterate all response headers
56
+ blk.call( key, value )
57
+ end
58
+ end
59
+ end
60
+ def headers() @headers ||= Headers.new( @response ); end
61
+
62
+
63
+ ## add some predefined/built-in header(s) convenience shortcuts
64
+ def content_type
65
+ ## check: change to headers['content-type'] or such - why? why not?
66
+ @response.content_type
67
+ end
68
+ def content_length
69
+ @response.content_length
70
+ end
71
+
72
+ def image_jpg?
73
+ content_type =~ %r{image/jpeg}i
74
+ end
75
+ def image_png?
76
+ content_type =~ %r{image/png}i
77
+ end
78
+ def image_gif?
79
+ content_type =~ %r{image/gif}i
80
+ end
81
+
82
+ alias_method :image_jpeg?, :image_jpg?
83
+ alias_method :jpeg?, :image_jpg?
84
+ alias_method :jpg?, :image_jpg?
85
+ alias_method :png?, :image_png?
86
+ alias_method :gif?, :image_gif?
87
+
88
+
89
+
90
+ class Status # nested (nested) class
91
+ def initialize( response )
92
+ @response = response
93
+ end
94
+ def code() @response.code.to_i; end
95
+ def ok?() code == 200; end
96
+ def nok?() code != 200; end
97
+ def message() @response.message; end
98
+ end
99
+ def status() @status ||= Status.new( @response ); end
100
+ end # (nested) class Response
101
+
102
+
103
+ def self.get( url, headers: {}, auth: [] )
104
+
105
+ uri = URI.parse( url )
106
+ http = Net::HTTP.new( uri.host, uri.port )
107
+
108
+ if uri.instance_of? URI::HTTPS
109
+ http.use_ssl = true
110
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
111
+ end
112
+
113
+ request = Net::HTTP::Get.new( uri.request_uri )
114
+
115
+ ### add (custom) headers if any
116
+ ## check/todo: is there are more idiomatic way for Net::HTTP ???
117
+ ## use
118
+ ## request = Net::HTTP::Get.new( uri.request_uri, headers )
119
+ ## why? why not?
120
+ ## instead of e.g.
121
+ ## request['X-Auth-Token'] = 'xxxxxxx'
122
+ ## request['User-Agent'] = 'ruby'
123
+ ## request['Accept'] = '*/*'
124
+ if headers && headers.size > 0
125
+ headers.each do |key,value|
126
+ request[ key ] = value
127
+ end
128
+ end
129
+
130
+
131
+ if auth.size == 2 ## e.g. ['user', 'password']
132
+ ## always assume basic auth for now
133
+ ## auth[0] => user
134
+ ## auth[1] => password
135
+ request.basic_auth( auth[0], auth[1] )
136
+ puts " using basic auth - user: #{auth[0]}, password: ***"
137
+ end
138
+
139
+
140
+ puts "GET #{uri}..."
141
+
142
+ response = http.request( request )
143
+
144
+ ## note: return "unified" wrapped response
145
+ Response.new( response )
146
+ end # method self.get
147
+
148
+
149
+ def self.post( url, headers: {},
150
+ body: nil,
151
+ json: nil ## json - convenience shortcut (for body & encoding)
152
+ )
153
+
154
+ uri = URI.parse( url )
155
+ http = Net::HTTP.new( uri.host, uri.port )
156
+
157
+ if uri.instance_of? URI::HTTPS
158
+ http.use_ssl = true
159
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
160
+ end
161
+
162
+ request = Net::HTTP::Post.new( uri.request_uri )
163
+
164
+ ### add (custom) headers if any
165
+ ## check/todo: is there are more idiomatic way for Net::HTTP ???
166
+ ## use
167
+ ## request = Net::HTTP::Get.new( uri.request_uri, headers )
168
+ ## why? why not?
169
+ ## instead of e.g.
170
+ ## request['X-Auth-Token'] = 'xxxxxxx'
171
+ ## request['User-Agent'] = 'ruby'
172
+ ## request['Accept'] = '*/*'
173
+ if headers && headers.size > 0
174
+ headers.each do |key,value|
175
+ request[ key ] = value
176
+ end
177
+ end
178
+
179
+ if body
180
+ request.body = body.to_s
181
+ end
182
+
183
+ if json
184
+ # note: the body needs to be a JSON string - use pretty generate and NOT "compact" style - why? why not?
185
+ request.body = JSON.pretty_generate( json )
186
+
187
+ ## move (auto-set) header content-type up (before custom headers) - why? why not?
188
+ request['Content-Type'] = 'application/json'
189
+ end
190
+
191
+
192
+ puts "POST #{uri}..."
193
+
194
+ response = http.request( request )
195
+
196
+ ## note: return "unified" wrapped response
197
+ Response.new( response )
198
+ end # method self.post
199
+
200
+
201
+ end # class Webclient
202
+
data/lib/webclient.rb CHANGED
@@ -1,25 +1,25 @@
1
- require 'pp'
2
- require 'time'
3
- require 'date'
4
- require 'fileutils'
5
-
6
- require 'uri'
7
- require 'net/http'
8
- require 'net/https'
9
-
10
- require 'json'
11
- require 'yaml'
12
-
13
-
14
- # our own code
15
- require 'webclient/version' # note: let version always go first
16
- require 'webclient/webclient'
17
-
18
-
19
- ############
20
- ## add convenience alias for camel case / alternate different spelling
21
- WebClient = Webclient
22
-
23
-
24
- # say hello
25
- puts Webclient.banner ## if defined?( $RUBYLIBS_DEBUG )
1
+ require 'pp'
2
+ require 'time'
3
+ require 'date'
4
+ require 'fileutils'
5
+
6
+ require 'uri'
7
+ require 'net/http'
8
+ require 'net/https'
9
+
10
+ require 'json'
11
+ require 'yaml'
12
+
13
+
14
+ # our own code
15
+ require 'webclient/version' # note: let version always go first
16
+ require 'webclient/webclient'
17
+
18
+
19
+ ############
20
+ ## add convenience alias for camel case / alternate different spelling
21
+ WebClient = Webclient
22
+
23
+
24
+ # say hello
25
+ puts Webclient.banner ## if defined?( $RUBYLIBS_DEBUG )
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webclient
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-05 00:00:00.000000000 Z
11
+ date: 2022-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rdoc
@@ -36,14 +36,14 @@ dependencies:
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '3.22'
39
+ version: '3.23'
40
40
  type: :development
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: '3.22'
46
+ version: '3.23'
47
47
  description: webclient gem - yet (another) universal network client interface for
48
48
  world wide web (www) requests via HTTP
49
49
  email: ruby-talk@ruby-lang.org
@@ -82,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
84
  requirements: []
85
- rubygems_version: 3.1.4
85
+ rubygems_version: 3.3.7
86
86
  signing_key:
87
87
  specification_version: 4
88
88
  summary: webclient gem - yet (another) universal network client interface for world