webclient 0.1.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
- SHA1:
3
- metadata.gz: 6e556a9e6949903368d71f7b0eec721c772edbf5
4
- data.tar.gz: bf7fa4bf5f66eba5a31d1b5be692d09b4693ee7e
2
+ SHA256:
3
+ metadata.gz: 8db40d5a48638657362e50374418f3f911079afbdf9852a6e99038a6dd46f549
4
+ data.tar.gz: 334f393fc27e197a685200c5f9a85f34c7940ac7c37bce7a80dcef971d642daf
5
5
  SHA512:
6
- metadata.gz: 575883f59d791b4326d9dc88184875fb2e120a5676d946088dffc59aa9f4e493412621dc7b77e9b15c9cbb8edd611f0ad0ce25fdeae33b6248f574f9d3962a28
7
- data.tar.gz: 40a53603700a3d12a55bca74071d8dbd48ebfea04bda68b0889c5cac135aca17e82aab9ab08cdf5c24f0c749fb9f24ea3a7ca64ad154d267881ef3406897dae2
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 = 1
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}]"
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,96 +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
- def text
12
- # note: Net::HTTP will NOT set encoding UTF-8 etc.
13
- # will be set to ASCII-8BIT == BINARY == Encoding Unknown; Raw Bytes Here
14
- # thus, set/force encoding to utf-8
15
- text = @response.body.to_s
16
- text = text.force_encoding( Encoding::UTF_8 )
17
- text
18
- end
19
-
20
- ## convenience helper; returns parsed json data
21
- def json() JSON.parse( text ); end
22
-
23
-
24
-
25
- class Headers # nested (nested) class
26
- def initialize( response )
27
- @response = response
28
- end
29
- def each( &blk )
30
- @response.each_header do |key, value| # Iterate all response headers
31
- blk.call( key, value )
32
- end
33
- end
34
- end
35
- def headers() @headers ||= Headers.new( @response ); end
36
-
37
- class Status # nested (nested) class
38
- def initialize( response )
39
- @response = response
40
- end
41
- def code() @response.code.to_i; end
42
- def ok?() code == 200; end
43
- def nok?() code != 200; end
44
- def message() @response.message; end
45
- end
46
- def status() @status ||= Status.new( @response ); end
47
- end # (nested) class Response
48
-
49
-
50
- def self.get( url, headers: {}, auth: [] )
51
-
52
- uri = URI.parse( url )
53
- http = Net::HTTP.new( uri.host, uri.port )
54
-
55
- if uri.instance_of? URI::HTTPS
56
- http.use_ssl = true
57
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
58
- end
59
-
60
- request = Net::HTTP::Get.new( uri.request_uri )
61
-
62
- ### add (custom) headers if any
63
- ## check/todo: is there are more idiomatic way for Net::HTTP ???
64
- ## use
65
- ## request = Net::HTTP::Get.new( uri.request_uri, headers )
66
- ## why? why not?
67
- ## instead of e.g.
68
- ## request['X-Auth-Token'] = 'xxxxxxx'
69
- ## request['User-Agent'] = 'ruby'
70
- ## request['Accept'] = '*/*'
71
- if headers && headers.size > 0
72
- headers.each do |key,value|
73
- request[ key ] = value
74
- end
75
- end
76
-
77
-
78
- if auth.size == 2 ## e.g. ['user', 'password']
79
- ## always assume basic auth for now
80
- ## auth[0] => user
81
- ## auth[1] => password
82
- request.basic_auth( auth[0], auth[1] )
83
- puts " using basic auth - user: #{auth[0]}, password: ***"
84
- end
85
-
86
-
87
- puts "GET #{uri}..."
88
-
89
- response = http.request( request )
90
-
91
- ## note: return "unified" wrapped response
92
- Response.new( response )
93
- end # method self.get
94
-
95
- end # class Webclient
96
-
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.1.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-07 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
@@ -65,7 +65,7 @@ homepage: https://github.com/rubycoco/webclient
65
65
  licenses:
66
66
  - Public Domain
67
67
  metadata: {}
68
- post_install_message:
68
+ post_install_message:
69
69
  rdoc_options:
70
70
  - "--main"
71
71
  - README.md
@@ -82,9 +82,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
84
  requirements: []
85
- rubyforge_project:
86
- rubygems_version: 2.5.2
87
- signing_key:
85
+ rubygems_version: 3.3.7
86
+ signing_key:
88
87
  specification_version: 4
89
88
  summary: webclient gem - yet (another) universal network client interface for world
90
89
  wide web (www) requests via HTTP