Class: Webclient::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/webclient/webclient.rb

Overview

nested class - wrap Net::HTTP::Response

Defined Under Namespace

Classes: Headers, Status

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



5
6
7
# File 'lib/webclient/webclient.rb', line 5

def initialize( response )
  @response = response
end

Instance Method Details

#bodyObject Also known as: blob



45
# File 'lib/webclient/webclient.rb', line 45

def body() @response.body.to_s; end

#content_lengthObject



68
69
70
# File 'lib/webclient/webclient.rb', line 68

def content_length
  @response.content_length
end

#content_typeObject

add some predefined/built-in header(s) convenience shortcuts



64
65
66
67
# File 'lib/webclient/webclient.rb', line 64

def content_type
  ## check: change to headers['content-type'] or such - why? why not?
  @response.content_type
end

#headersObject



60
# File 'lib/webclient/webclient.rb', line 60

def headers() @headers ||= Headers.new( @response ); end

#image_gif?Boolean Also known as: gif?

Returns:

  • (Boolean)


78
79
80
# File 'lib/webclient/webclient.rb', line 78

def image_gif?
  content_type =~ %r{image/gif}i
end

#image_jpg?Boolean Also known as: image_jpeg?, jpeg?, jpg?

Returns:

  • (Boolean)


72
73
74
# File 'lib/webclient/webclient.rb', line 72

def image_jpg?
  content_type =~ %r{image/jpeg}i
end

#image_png?Boolean Also known as: png?

Returns:

  • (Boolean)


75
76
77
# File 'lib/webclient/webclient.rb', line 75

def image_png?
  content_type =~ %r{image/png}i
end

#jsonObject

convenience helper; returns parsed json data; note: always assume utf-8 (text) encoding



42
# File 'lib/webclient/webclient.rb', line 42

def json() JSON.parse( text ); end

#rawObject



8
# File 'lib/webclient/webclient.rb', line 8

def raw() @response; end

#statusObject



99
# File 'lib/webclient/webclient.rb', line 99

def status() @status ||= Status.new( @response ); end

#text(encoding: 'UTF-8') ⇒ Object

todo/check: rename encoding to html/http-like charset - why? why not?



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/webclient/webclient.rb', line 12

def text( encoding: 'UTF-8' )
  # note: Net::HTTP will NOT set encoding UTF-8 etc.
  # will be set to ASCII-8BIT == BINARY == Encoding Unknown; Raw Bytes Here
  # thus, set/force encoding to utf-8
  text = @response.body.to_s
  if encoding.downcase == 'utf-8'
     text = text.force_encoding( Encoding::UTF_8 )
  else
## [debug] GET=http://www.football-data.co.uk/mmz4281/0405/SC0.csv
##    Encoding::UndefinedConversionError: "\xA0" from ASCII-8BIT to UTF-8
##     note:  0xA0 (160) is NBSP (non-breaking space) in Windows-1252

## note: assume windows encoding (for football-data.uk)
##   use "Windows-1252" for input and convert to utf-8
##
##    see https://www.justinweiss.com/articles/3-steps-to-fix-encoding-problems-in-ruby/
##    see https://en.wikipedia.org/wiki/Windows-1252
## txt = txt.force_encoding( 'Windows-1252' )
## txt = txt.encode( 'UTF-8' )
##   Encoding::UTF_8 => 'UTF-8'
    puts " [debug] converting response.text encoding from >#{encoding}< to >UTF-8<"

    text = text.force_encoding( encoding )
    text = text.encode( Encoding::UTF_8 )
  end

  text
end