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.



9
10
11
12
13
# File 'lib/content_o_matic/response.rb', line 9

def initialize(url, status, body = '')
  self.url = url
  self.status = status.to_i
  self.body = body
end

Instance Attribute Details

#body(normalize_assets = false) ⇒ Object

Returns the value of attribute body.



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

def body
  @body
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



62
63
64
# File 'lib/content_o_matic/response.rb', line 62

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)


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

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

#html_body(normalize_assets = false) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/content_o_matic/response.rb', line 53

def html_body(normalize_assets = false)
  unless @__doc_body
    doc = Nokogiri::HTML(self.body(normalize_assets))
    @__doc_body = doc.at('body')
    @__doc_body = @__doc_body.nil? ? self.body(normalize_assets) : @__doc_body.inner_html
  end
  return @__doc_body
end

#success?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/content_o_matic/response.rb', line 15

def success?
  self.status == 200
end

#to_sObject



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

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

#to_strObject



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

def to_str
  self.to_s
end