Class: Weatherzone::Connection
Constant Summary collapse
- DEFAULT_TIMEOUT_AFTER =
1
Instance Attribute Summary collapse
-
#keygen ⇒ Object
Returns the value of attribute keygen.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#password ⇒ Object
Returns the value of attribute password.
-
#timeout_after ⇒ Object
Returns the value of attribute timeout_after.
-
#url ⇒ Object
Returns the value of attribute url.
-
#username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
Instance Method Summary collapse
- #base_url ⇒ Object
- #debug(message) ⇒ Object
- #error(message) ⇒ Object
- #info(message) ⇒ Object
-
#initialize(username = nil, password = nil, keygen = nil, options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #key ⇒ Object
- #request(params) ⇒ Object
- #settings ⇒ Object
- #wz_url_for(params) ⇒ Object
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, ={}) @logger = Logger.new(STDOUT) @logger.level = Logger::DEBUG @username = username @password = password @url = [:url] @keygen = keygen @logger = [:logger] @timeout_after = [:timeout_after] || DEFAULT_TIMEOUT_AFTER end |
Instance Attribute Details
#keygen ⇒ Object
Returns the value of attribute keygen.
23 24 25 |
# File 'lib/weatherzone/connection.rb', line 23 def keygen @keygen end |
#logger ⇒ Object
Returns the value of attribute logger.
23 24 25 |
# File 'lib/weatherzone/connection.rb', line 23 def logger @logger end |
#password ⇒ Object
Returns the value of attribute password.
23 24 25 |
# File 'lib/weatherzone/connection.rb', line 23 def password @password end |
#timeout_after ⇒ Object
Returns the value of attribute timeout_after.
23 24 25 |
# File 'lib/weatherzone/connection.rb', line 23 def timeout_after @timeout_after end |
#url ⇒ Object
Returns the value of attribute url.
23 24 25 |
# File 'lib/weatherzone/connection.rb', line 23 def url @url end |
#username ⇒ Object
Returns the value of attribute username.
23 24 25 |
# File 'lib/weatherzone/connection.rb', line 23 def username @username end |
Class Method Details
.settings ⇒ Object
36 37 38 |
# File 'lib/weatherzone/connection.rb', line 36 def self.settings Weatherzone::Settings.instance end |
Instance Method Details
#base_url ⇒ Object
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() @logger.debug("[weatherzone] [DEBUG] #{}") if @logger end |
#error(message) ⇒ Object
75 76 77 |
# File 'lib/weatherzone/connection.rb', line 75 def error() @logger.error("[weatherzone] [ERROR] #{}") if @logger end |
#info(message) ⇒ Object
71 72 73 |
# File 'lib/weatherzone/connection.rb', line 71 def info() @logger.info("[weatherzone] [INFO] #{}") if @logger end |
#key ⇒ Object
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 |
#settings ⇒ Object
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 |