Class: LCBO::CrawlKit::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/lcbo/crawlkit/response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/lcbo/crawlkit/response.rb', line 8

def initialize(response)
  params        = HashExt.symbolize_keys(response)
  @code         = params[:code]
  @uri          = params[:uri]
  @http_method  = params[:http_method]
  @time         = params[:time]
  @query_params = params[:query_params]
  @body_params  = params[:body_params]
  @body         = self.class.normalize_encoding(params[:body])
  ensure_success!
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



5
6
7
# File 'lib/lcbo/crawlkit/response.rb', line 5

def body
  @body
end

#body_paramsObject (readonly)

Returns the value of attribute body_params.



5
6
7
# File 'lib/lcbo/crawlkit/response.rb', line 5

def body_params
  @body_params
end

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/lcbo/crawlkit/response.rb', line 5

def code
  @code
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



5
6
7
# File 'lib/lcbo/crawlkit/response.rb', line 5

def http_method
  @http_method
end

#query_paramsObject (readonly)

Returns the value of attribute query_params.



5
6
7
# File 'lib/lcbo/crawlkit/response.rb', line 5

def query_params
  @query_params
end

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/lcbo/crawlkit/response.rb', line 5

def response
  @response
end

#timeObject (readonly)

Returns the value of attribute time.



5
6
7
# File 'lib/lcbo/crawlkit/response.rb', line 5

def time
  @time
end

#uriObject (readonly)

Returns the value of attribute uri.



5
6
7
# File 'lib/lcbo/crawlkit/response.rb', line 5

def uri
  @uri
end

Class Method Details

.normalize_encoding(html) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/lcbo/crawlkit/response.rb', line 20

def self.normalize_encoding(html)
  if html.force_encoding('ISO-8859-1').valid_encoding?
    html.encode('UTF-8', 'ISO-8859-1')
  else
    html.encode('UTF-8')
  end.gsub("\r\n", "\n")
end

Instance Method Details

#as_hashObject



28
29
30
31
32
33
34
35
36
# File 'lib/lcbo/crawlkit/response.rb', line 28

def as_hash
  { :code         => code,
    :uri          => uri,
    :http_method  => http_method,
    :time         => time,
    :query_params => query_params,
    :body_params  => body_params,
    :body         => body }
end