Class: ActiveScraper::AgnosticResponseObject

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
app/models/active_scraper/agnostic_response_object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ AgnosticResponseObject

Returns a new instance of AgnosticResponseObject.



6
7
8
9
10
11
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
# File 'app/models/active_scraper/agnostic_response_object.rb', line 6

def initialize(obj)
  if obj.class == (HTTParty::Response)
    # use the Net::HTTPResponse instead
    obj = obj.response
  end

  response_obj = if obj.is_a?(Net::HTTPResponse)
    @body = obj.body
    @content_type = obj.content_type
    @headers = obj.each_header.inject({}){|h, (k, v)| h[k] = v; h }
    @code = obj.code.to_i
  elsif obj.is_a?(ActiveScraper::Request)
    @body = obj.body
    @content_type = obj.content_type
    @headers = obj.headers
    @code = obj.code.to_i
  else
    # this is probably not used
    @body = obj.to_s
    @headers = {}
    @content_type = nil
    @code = nil
  end

  super({})

  # now set its values
  [:body, :headers, :content_type, :code].each do |a|
    self[a] = self.send(a)
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



4
5
6
# File 'app/models/active_scraper/agnostic_response_object.rb', line 4

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



4
5
6
# File 'app/models/active_scraper/agnostic_response_object.rb', line 4

def code
  @code
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



4
5
6
# File 'app/models/active_scraper/agnostic_response_object.rb', line 4

def content_type
  @content_type
end

#headersObject (readonly)

Returns the value of attribute headers.



4
5
6
# File 'app/models/active_scraper/agnostic_response_object.rb', line 4

def headers
  @headers
end