Class: ActiveScraper::ResponseObject::Basic

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/active_scraper/response_object/basic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Basic

Returns a new instance of Basic.



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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/active_scraper/response_object/basic.rb', line 10

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::CachedResponse)
    @body = obj.body
    @content_type = obj.content_type
    @headers = obj.headers
    @code = obj.code.to_i
  elsif obj.is_a?(StringIO) && obj.respond_to?(:meta) # OpenURI.open
    @body = obj.read
    @content_type = obj.content_type
    @headers = obj.meta
    @code = obj.status[0].to_i
  elsif obj.nil?
    # just do nothing
  else
    # other types have to raise an Error
    raise ArgumentError, 'Improper class type'
  end

  super(ActiveSupport::HashWithIndifferentAccess.new() )

  def empty?
    @body.empty?
  end

  def nil?
    @body.nil?
  end


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

Instance Attribute Details

#bodyObject (readonly)

I don’t really know what to name this but this is passed between the various classes, including the Fetcher, and is expected to behave the same in those interactions



8
9
10
# File 'lib/active_scraper/response_object/basic.rb', line 8

def body
  @body
end

#codeObject (readonly)

I don’t really know what to name this but this is passed between the various classes, including the Fetcher, and is expected to behave the same in those interactions



8
9
10
# File 'lib/active_scraper/response_object/basic.rb', line 8

def code
  @code
end

#content_typeObject (readonly)

I don’t really know what to name this but this is passed between the various classes, including the Fetcher, and is expected to behave the same in those interactions



8
9
10
# File 'lib/active_scraper/response_object/basic.rb', line 8

def content_type
  @content_type
end

#headersObject (readonly)

I don’t really know what to name this but this is passed between the various classes, including the Fetcher, and is expected to behave the same in those interactions



8
9
10
# File 'lib/active_scraper/response_object/basic.rb', line 8

def headers
  @headers
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/active_scraper/response_object/basic.rb', line 40

def empty?
  @body.empty?
end

#nil?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/active_scraper/response_object/basic.rb', line 44

def nil?
  @body.nil?
end