Class: ContentOMatic::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, status, body = '') ⇒ Response

Returns a new instance of Response.



10
11
12
13
14
15
16
17
# File 'lib/content_o_matic/response.rb', line 10

def initialize(url, status, body = '')
  self.url = url
  self.status = status.to_i
  self.body = body
  if success?
    self.hpricot_doc = Hpricot(self.body)
  end
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



6
7
8
# File 'lib/content_o_matic/response.rb', line 6

def body
  @body
end

#hpricot_docObject

Returns the value of attribute hpricot_doc.



8
9
10
# File 'lib/content_o_matic/response.rb', line 8

def hpricot_doc
  @hpricot_doc
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/content_o_matic/response.rb', line 7

def url
  @url
end

Instance Method Details

#exceptionObject



43
44
45
# File 'lib/content_o_matic/response.rb', line 43

def exception
  ContentOMatic::InvalidResponseError.new("URL: '#{self.url}' did not return a valid response! Status: '#{self.status}' Body: '#{self.body}'") unless self.success?
end

#has_layout?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/content_o_matic/response.rb', line 31

def has_layout?
  return self.body != self.html_body
end

#html_bodyObject



35
36
37
38
39
40
41
# File 'lib/content_o_matic/response.rb', line 35

def html_body
  unless @__doc_body
    @__doc_body = self.hpricot_doc.at('body')
    @__doc_body = @__doc_body.nil? ? self.body : @__doc_body.inner_html
  end
  return @__doc_body
end

#success?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/content_o_matic/response.rb', line 19

def success?
  self.status == 200
end

#to_sObject



23
24
25
# File 'lib/content_o_matic/response.rb', line 23

def to_s
  self.success? ? self.body : ''
end

#to_strObject



27
28
29
# File 'lib/content_o_matic/response.rb', line 27

def to_str
  self.to_s
end