Class: Flying::Bot::Up

Inherits:
Object
  • Object
show all
Defined in:
lib/flying/bots/up.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(referer, options = {}) ⇒ Up

Returns a new instance of Up.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/flying/bots/up.rb', line 22

def initialize(referer, options = {})
  @referer = referer
  @options = options
  @message = "Ok."
  @error = false
  @name = options[:as] if options.include?(:as)
  @dependency = []
  
  # Dependency can be one or an array of symbols, whereas @dependency 
  # must be only a flat array
  if options.include?(:depends_on)
    @dependency << options[:depends_on] if options[:depends_on].kind_of? Symbol
    @dependency = options[:depends_on] if options[:depends_on].kind_of? Array
  end
end

Instance Attribute Details

#dependencyObject

> Name

How a service is known. Useful reusing and setting dependencies.

e.g. `site "etc.com", :as => :google`
name is :google.

> Dependency

When a service depends on another, it has dependencies. This is an array with the Name of each service.



20
21
22
# File 'lib/flying/bots/up.rb', line 20

def dependency
  @dependency
end

#errorObject (readonly)

Returns the value of attribute error.



6
7
8
# File 'lib/flying/bots/up.rb', line 6

def error
  @error
end

#messageObject (readonly)

Returns the value of attribute message.



6
7
8
# File 'lib/flying/bots/up.rb', line 6

def message
  @message
end

#nameObject

> Name

How a service is known. Useful reusing and setting dependencies.

e.g. `site "etc.com", :as => :google`
name is :google.

> Dependency

When a service depends on another, it has dependencies. This is an array with the Name of each service.



20
21
22
# File 'lib/flying/bots/up.rb', line 20

def name
  @name
end

#refererObject (readonly)

Returns the value of attribute referer.



6
7
8
# File 'lib/flying/bots/up.rb', line 6

def referer
  @referer
end

Instance Method Details

#assessObject

Starts assessment of the current service using variables set during initialization



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/flying/bots/up.rb', line 40

def assess
  @error = false
  begin
    response_code = get_http_response_code(@referer)
  rescue
    Flying.an_error_ocurred(true)
    set_error_message(@referer, false, $!)
    return false
  end
  return true if ["200", "302"].include? response_code
  Flying.an_error_ocurred(true)
  set_error_message(@referer, response_code.to_s)
  false
end

#get_http_response_code(referer) ⇒ Object



70
71
72
# File 'lib/flying/bots/up.rb', line 70

def get_http_response_code referer
  Net::HTTP.get_response(URI(referer)).code
end

#message_not_foundObject



83
84
85
# File 'lib/flying/bots/up.rb', line 83

def message_not_found
  "The target was simply not found (404)."
end

#message_server_errorObject



87
88
89
# File 'lib/flying/bots/up.rb', line 87

def message_server_error
  "We got a response saying there's a server error (501)"
end

#message_unknown_errorObject

Messages



75
76
77
# File 'lib/flying/bots/up.rb', line 75

def message_unknown_error
  "An unknown error ocurred."
end

#message_unreachableObject



79
80
81
# File 'lib/flying/bots/up.rb', line 79

def message_unreachable
  "It is unreachable (is the url correct?)."
end

#set_error_message(referer, response_code, error_details = '') ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/flying/bots/up.rb', line 55

def set_error_message(referer, response_code, error_details = '')
  @error = true
  case response_code
  when false
    @message = message_unreachable
  when "404"
    @message = message_not_found
  when "501"
    @message = message_server_error
  else
    @message = message_unknown_error + "(#{error_details})"
  end
  @message = "\e[31m" + referer + ": " + @message + "\e[0m"
end