Class: HotOrNot::UrlResult

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hot_or_not/url_result.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{ :method => :get }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, response, error, latency) ⇒ UrlResult

Returns a new instance of UrlResult.



26
27
28
29
# File 'lib/hot_or_not/url_result.rb', line 26

def initialize(url, response, error, latency)
  @url, @error, @latency = url, error, latency
  @response = response || error.response
end

Instance Attribute Details

#latencyObject (readonly)

Returns the value of attribute latency.



23
24
25
# File 'lib/hot_or_not/url_result.rb', line 23

def latency
  @latency
end

#urlObject (readonly)

Returns the value of attribute url.



23
24
25
# File 'lib/hot_or_not/url_result.rb', line 23

def url
  @url
end

Class Method Details

.retrieve_for(url, options) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/hot_or_not/url_result.rb', line 8

def retrieve_for(url, options)
  options = DEFAULT_OPTIONS.merge options
  options[:url] = url
  response, error = nil, nil
  start = Time.now
  begin
    response = RestClient::Request.execute(options)
  rescue RestClient::Exception => e
    error = e
  end

  new url, response, error, Time.now - start
end

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/hot_or_not/url_result.rb', line 35

def error?
  !success?
end

#error_messageObject



39
40
41
# File 'lib/hot_or_not/url_result.rb', line 39

def error_message
  "The url #{@url} received error: #{@error.message}"
end

#success?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/hot_or_not/url_result.rb', line 31

def success?
  @error.nil?
end