Class: Weatherzone::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/weatherzone/connection.rb

Constant Summary collapse

DEFAULT_TIMEOUT_AFTER =
1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username = nil, password = nil, keygen = nil, options = {}) ⇒ Connection

Returns a new instance of Connection.



25
26
27
28
29
30
31
32
33
34
# File 'lib/weatherzone/connection.rb', line 25

def initialize(username=nil, password=nil, keygen=nil, options={})
  @logger        = Logger.new(STDOUT)
  @logger.level  = Logger::DEBUG
  @username      = username
  @password      = password
  @url           = options[:url]
  @keygen        = keygen
  @logger        = options[:logger]
  @timeout_after = options[:timeout_after] || DEFAULT_TIMEOUT_AFTER
end

Instance Attribute Details

#keygenObject

Returns the value of attribute keygen.



23
24
25
# File 'lib/weatherzone/connection.rb', line 23

def keygen
  @keygen
end

#loggerObject

Returns the value of attribute logger.



23
24
25
# File 'lib/weatherzone/connection.rb', line 23

def logger
  @logger
end

#passwordObject

Returns the value of attribute password.



23
24
25
# File 'lib/weatherzone/connection.rb', line 23

def password
  @password
end

#timeout_afterObject

Returns the value of attribute timeout_after.



23
24
25
# File 'lib/weatherzone/connection.rb', line 23

def timeout_after
  @timeout_after
end

#urlObject

Returns the value of attribute url.



23
24
25
# File 'lib/weatherzone/connection.rb', line 23

def url
  @url
end

#usernameObject

Returns the value of attribute username.



23
24
25
# File 'lib/weatherzone/connection.rb', line 23

def username
  @username
end

Class Method Details

.settingsObject



36
37
38
# File 'lib/weatherzone/connection.rb', line 36

def self.settings
  Weatherzone::Settings.instance
end

Instance Method Details

#base_urlObject



48
49
50
# File 'lib/weatherzone/connection.rb', line 48

def base_url
  @base_url ||= "#{self.url}?u=#{username}&k=#{key}"
end

#debug(message) ⇒ Object



67
68
69
# File 'lib/weatherzone/connection.rb', line 67

def debug(message) 
  @logger.debug("[weatherzone] [DEBUG] #{message}") if @logger
end

#error(message) ⇒ Object



75
76
77
# File 'lib/weatherzone/connection.rb', line 75

def error(message)
  @logger.error("[weatherzone] [ERROR] #{message}") if @logger
end

#info(message) ⇒ Object



71
72
73
# File 'lib/weatherzone/connection.rb', line 71

def info(message)
  @logger.info("[weatherzone] [INFO] #{message}") if @logger
end

#keyObject



44
45
46
# File 'lib/weatherzone/connection.rb', line 44

def key
  instance_eval &@keygen
end

#request(params) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/weatherzone/connection.rb', line 56

def request(params)
  uri = URI.parse(wz_url_for(params))
  info("GET #{uri}")
  timeout(self.timeout_after) do
    uri.read
  end
rescue Timeout::Error, SocketError => e
  error("webservice connection failed #{e}")
  raise RequestFailed.new(url, e)
end

#settingsObject



40
41
42
# File 'lib/weatherzone/connection.rb', line 40

def settings
  self.class.settings
end

#wz_url_for(params) ⇒ Object



52
53
54
# File 'lib/weatherzone/connection.rb', line 52

def wz_url_for(params)
  "#{base_url}#{params}"
end