Class: RWeather

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

Constant Summary collapse

NOT_AUTHENTICATED =
'You should set partner_id and key values on RWeather before calling data request actions.'
SEARCH_URL =
'http://xoap.weather.com/search/search?where=%s'
LOCAL_URL =
'http://xoap.weather.com/weather/local/%s?par=%s&key=%s&link=xoap&prod=xoap'
VALID_UNITS =
['s', 'm']
DEFAULT_UNIT =

standard

's'

Class Method Summary collapse

Class Method Details

.check_authenticationObject

Raises:

  • (Exception)


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

def check_authentication
  raise Exception.new(NOT_AUTHENTICATED) if @@partner_id.nil? || @@partner_id.empty? || @@key.nil? || @@key.empty?
end

.current_conditions(location_id, unit = DEFAULT_UNIT) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/r_weather.rb', line 28

def self.current_conditions(location_id, unit = DEFAULT_UNIT)
  check_authentication
  url = sprintf(LOCAL_URL, location_id, partner_id, key)
  url << "&cc=true"
  url << "&unit=#{unit}" if VALID_UNITS.include?(unit)
  if data = get_and_parse(URI.encode(url))
    RWeatherCurrentCondition.parse(data)
  end
end

.get_and_parse(url) ⇒ Object



48
49
50
51
# File 'lib/r_weather.rb', line 48

def get_and_parse(url)
  xml_data = Net::HTTP.get_response(URI.parse(url)).body
  XmlSimple.xml_in(xml_data)
end

.keyObject



19
# File 'lib/r_weather.rb', line 19

def self.key; @@key; end

.key=(value) ⇒ Object



20
# File 'lib/r_weather.rb', line 20

def self.key=(value); @@key = value; end

.local(partner_id, key) ⇒ Object



38
39
40
# File 'lib/r_weather.rb', line 38

def self.local(partner_id, key)
  check_authentication
end

.partner_idObject



16
# File 'lib/r_weather.rb', line 16

def self.partner_id; @@partner_id; end

.partner_id=(value) ⇒ Object



17
# File 'lib/r_weather.rb', line 17

def self.partner_id=(value); @@partner_id = value; end

.search(where) ⇒ Object



22
23
24
25
26
# File 'lib/r_weather.rb', line 22

def self.search(where)
  if data = get_and_parse(sprintf(SEARCH_URL, URI.encode(where)))
    data['loc'].map{|loc| RWeatherLocation.new(loc['type'], loc['id'], loc['content'])}
  end
end