accuweather 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3f3ee685b1cd0ac59857ca0e30c05303a629b7e
4
- data.tar.gz: 5e37439f9fff9227179b52aca21a75670b2425d3
3
+ metadata.gz: bc7bfc4757609cb611775de025b9612c43913cbd
4
+ data.tar.gz: 630af49fdd6319f9016b1ae43955e979f19d1d34
5
5
  SHA512:
6
- metadata.gz: 4aee4ca65e116409bf9dde962658f5e188ed815241421a98fcb6941d802a749b092f30738dac251f4a46b932c0b3e47c4f64253ddbc7e33c558df83615cd477d
7
- data.tar.gz: 5e051ade5ab455035db25aeb4186001175bed0b03b959400355ebd0ba2b7f1af6aa5bcb6451ed32f213b2a61279ba3899500aea0c91512b9fceec4fd0af35182
6
+ metadata.gz: e7b5678dc93483e02ad3cc0f4b73ddeeff09266f43888ad663583684a9b21b5f2b6e6d7a7dc9eb24b3511f1f9d2a2626a7126efa54a934ee212a4664e4b305cf
7
+ data.tar.gz: d74ea8bf168345691bffeea22b9171911d3f180bf63f0b75603776c1f8afd881bdd25f4a9cbbd1930ef07f95a86eba795c79b0ba361cffcab421646d6c93b030
data/README.md CHANGED
@@ -24,7 +24,7 @@ Or install it yourself as:
24
24
  Search for an accuweather location:
25
25
 
26
26
  ```ruby
27
- location_array = Accuweather.city_search('vancouver') # returns an array
27
+ location_array = Accuweather.city_search(name: 'vancouver')
28
28
  vancouver = location_array.first
29
29
 
30
30
  vancouver.id # => 'cityId:53286'
@@ -37,7 +37,7 @@ vancouver.longitude # => '123.1154'
37
37
  Search for weather conditions for a given location id:
38
38
 
39
39
  ```ruby
40
- current_weather = Accuweather.get_conditions('cityId:53286').current
40
+ current_weather = Accuweather.get_conditions(location_id: 'cityId:53286').current
41
41
  current_weather.temperature # => '41'
42
42
  current_weather.weather_text # => 'Partly Sunny'
43
43
  current_weather.pressure # => '30.35'
@@ -48,27 +48,33 @@ current_weather.cloud_cover # => '40%'
48
48
  Get the units for the conditions:
49
49
 
50
50
  ```ruby
51
- units = Accuweather.get_conditions('cityId:53286').units
52
- units.temp # => 'F'
53
- units.dist # => 'MI'
54
- units.speed # => 'MPH'
51
+ units = Accuweather.get_conditions(location_id: 'cityId:53286').units
52
+ units.temperature # => 'F'
53
+ units.distance # => 'MI'
54
+ units.speed # => 'MPH'
55
55
  ```
56
56
 
57
57
  Get more information on the location including time and time zone:
58
58
 
59
59
  ```ruby
60
- local = Accuweather.get_conditions('cityId:53286').local
60
+ local = Accuweather.get_conditions(location_id: 'cityId:53286').local
61
61
  local.time # => '13:41'
62
62
  local.time_zone # => '-8'
63
63
  local.time_zone_abbreviation # => 'PST'
64
64
  ```
65
65
 
66
+ Imperial units are returned by default, but metric results are available:
67
+
68
+ ```ruby
69
+ Accuweather.get_conditions(location_id: 'cityId:53286', metric: true)
70
+ ```
71
+
66
72
  Each `Accuweather::Conditions` object implements a `to_s` method that displays all attribute
67
73
  name, value pairs. This makes it easy to explore the API. For example:
68
74
 
69
75
  ```ruby
70
- Accuweather.get_conditions('cityId:53286').local.to_s
71
- # => "city: Vancouver, state: British Columbia, lat: 49.2448, lon: -123.1154, time: 13:41, time_zone: -8, obs_daylight: 0, current_gmt_offset: -8, time_zone_abbreviation: PST"
76
+ Accuweather.get_conditions(location_id: 'cityId:53286').local.to_s
77
+ # => "city: Vancouver, state: British Columbia, latitude: 49.2448, longitude: -123.1154, time: 16:58, time_zone: -8, obs_daylight: 0, current_gmt_offset: -8, time_zone_abbreviation: PST"
72
78
  ```
73
79
 
74
80
  ## Development
@@ -1,5 +1,6 @@
1
1
  require 'accuweather/version'
2
- require 'accuweather/location'
2
+ require 'accuweather/location/city'
3
+ require 'accuweather/location/parser'
3
4
  require 'accuweather/conditions/parser'
4
5
  require 'accuweather/conditions/units'
5
6
  require 'accuweather/conditions/local'
@@ -9,26 +10,19 @@ require 'net/http'
9
10
  require 'nokogiri'
10
11
 
11
12
  module Accuweather
12
- def self.city_search(name)
13
+ def self.city_search(name:)
13
14
  response = Net::HTTP.get('samsungmobile.accu-weather.com',
14
15
  "/widget/samsungmobile/city-find.asp?returnGeoPosition=1&location=#{name}")
15
16
 
16
-
17
- xml = Nokogiri::XML.parse(response)
18
- xml.css('location').map do |location|
19
- Accuweather::Location.new(id: location.attr('location'),
20
- city: location.attr('city'),
21
- state: location.attr('state'),
22
- latitude: location.attr('latitude'),
23
- longitude: location.attr('longitude'))
24
- end
17
+ Accuweather::Location::Parser.new(response).cities
25
18
  rescue StandardError
26
19
  []
27
20
  end
28
21
 
29
- def self.get_conditions(location_id)
22
+ def self.get_conditions(location_id:, metric: false)
23
+ metric_value = metric ? '1' : '0'
30
24
  response = Net::HTTP.get('samsungmobile.accu-weather.com',
31
- "/widget/samsungmobile/weather-data.asp?metric=0&location=#{location_id}")
25
+ "/widget/samsungmobile/weather-data.asp?metric=#{metric_value}&location=#{location_id}")
32
26
 
33
27
  Accuweather::Conditions::Parser.new(response)
34
28
  rescue StandardError
@@ -1,13 +1,13 @@
1
1
  module Accuweather
2
2
  module Conditions
3
3
  class Local
4
- attr_reader :city, :state, :lat, :lon, :time, :time_zone, :obs_daylight, :current_gmt_offset, :time_zone_abbreviation
4
+ attr_reader :city, :state, :latitude, :longitude, :time, :time_zone, :obs_daylight, :current_gmt_offset, :time_zone_abbreviation
5
5
 
6
- def initialize(city:, state:, lat:, lon:, time:, time_zone:, obs_daylight:, current_gmt_offset:, time_zone_abbreviation:)
6
+ def initialize(city:, state:, latitude:, longitude:, time:, time_zone:, obs_daylight:, current_gmt_offset:, time_zone_abbreviation:)
7
7
  @city = city
8
8
  @state = state
9
- @lat = lat
10
- @lon = lon
9
+ @latitude = latitude
10
+ @longitude = longitude
11
11
  @time = time
12
12
  @time_zone = time_zone
13
13
  @obs_daylight = obs_daylight
@@ -18,8 +18,8 @@ module Accuweather
18
18
  def ==(other)
19
19
  city == other.city &&
20
20
  state == other.state &&
21
- lat == other.lat &&
22
- lon == other.lon &&
21
+ latitude == other.latitude &&
22
+ longitude == other.longitude &&
23
23
  time == other.time &&
24
24
  time_zone == other.time_zone &&
25
25
  obs_daylight == other.obs_daylight &&
@@ -30,7 +30,7 @@ module Accuweather
30
30
  end
31
31
 
32
32
  def to_s
33
- "city: #{city}, state: #{state}, lat: #{lat}, lon: #{lon}, time: #{time}, time_zone: #{time_zone}, obs_daylight: #{obs_daylight}, current_gmt_offset: #{current_gmt_offset}, time_zone_abbreviation: #{time_zone_abbreviation}"
33
+ "city: #{city}, state: #{state}, latitude: #{latitude}, longitude: #{longitude}, time: #{time}, time_zone: #{time_zone}, obs_daylight: #{obs_daylight}, current_gmt_offset: #{current_gmt_offset}, time_zone_abbreviation: #{time_zone_abbreviation}"
34
34
  end
35
35
  end
36
36
  end
@@ -7,19 +7,19 @@ module Accuweather
7
7
 
8
8
  def units
9
9
  units = @doc.css('units').first
10
- Accuweather::Conditions::Units.new(temp: units.css('temp').text,
11
- dist: units.css('dist').text,
10
+ Accuweather::Conditions::Units.new(temperature: units.css('temp').text,
11
+ distance: units.css('dist').text,
12
12
  speed: units.css('speed').text,
13
- pres: units.css('pres').text,
14
- prec: units.css('prec').text)
13
+ pressure: units.css('pres').text,
14
+ precipitation: units.css('prec').text)
15
15
  end
16
16
 
17
17
  def local
18
18
  local = @doc.css('local').first
19
19
  Accuweather::Conditions::Local.new(city: local.css('city').text,
20
20
  state: local.css('state').text,
21
- lat: local.css('lat').text,
22
- lon: local.css('lon').text,
21
+ latitude: local.css('lat').text,
22
+ longitude: local.css('lon').text,
23
23
  time: local.css('time').text,
24
24
  time_zone: local.css('timeZone').text,
25
25
  obs_daylight: local.css('obsDaylight').text.strip,
@@ -1,28 +1,28 @@
1
1
  module Accuweather
2
2
  module Conditions
3
3
  class Units
4
- attr_reader :temp, :dist, :speed, :pres, :prec
4
+ attr_reader :temperature, :distance, :speed, :pressure, :precipitation
5
5
 
6
- def initialize(temp:, dist:, speed:, pres:, prec:)
7
- @temp = temp
8
- @dist = dist
6
+ def initialize(temperature:, distance:, speed:, pressure:, precipitation:)
7
+ @temperature = temperature
8
+ @distance = distance
9
9
  @speed = speed
10
- @pres = pres
11
- @prec = prec
10
+ @pressure = pressure
11
+ @precipitation = precipitation
12
12
  end
13
13
 
14
14
  def ==(other)
15
- temp == other.temp &&
16
- dist == other.dist &&
15
+ temperature == other.temperature &&
16
+ distance == other.distance &&
17
17
  speed == other.speed &&
18
- pres == other.pres &&
19
- prec == other.prec
18
+ pressure == other.pressure &&
19
+ precipitation == other.precipitation
20
20
  rescue NoMethodError
21
21
  false
22
22
  end
23
23
 
24
24
  def to_s
25
- "temp: #{temp}, dist: #{dist}, speed: #{speed}, pres: #{pres}, prec: #{prec}"
25
+ "temperature: #{temperature}, distance: #{distance}, speed: #{speed}, pressure: #{pressure}, precipitation: #{precipitation}"
26
26
  end
27
27
  end
28
28
  end
@@ -0,0 +1,29 @@
1
+ module Accuweather
2
+ module Location
3
+ class City
4
+ attr_reader :id, :city, :state, :latitude, :longitude
5
+
6
+ def initialize(id:, city:, state:, latitude:, longitude:)
7
+ @id = id
8
+ @city = city
9
+ @state = state
10
+ @latitude = latitude
11
+ @longitude = longitude
12
+ end
13
+
14
+ def ==(other)
15
+ id == other.id &&
16
+ city == other.city &&
17
+ state == other.state &&
18
+ latitude == other.latitude &&
19
+ longitude == other.longitude
20
+ rescue NoMethodError
21
+ false
22
+ end
23
+
24
+ def to_s
25
+ "id: #{id}, city: #{city}, state: #{state}, latitude: #{latitude}, longitude: #{longitude}"
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,19 @@
1
+ module Accuweather
2
+ module Location
3
+ class Parser
4
+ def initialize(response)
5
+ @doc = Nokogiri::XML.parse(response)
6
+ end
7
+
8
+ def cities
9
+ @doc.css('location').map do |location|
10
+ Accuweather::Location::City.new(id: location.attr('location'),
11
+ city: location.attr('city'),
12
+ state: location.attr('state'),
13
+ latitude: location.attr('latitude'),
14
+ longitude: location.attr('longitude'))
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module Accuweather
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: accuweather
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Aschenbach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-25 00:00:00.000000000 Z
11
+ date: 2015-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -91,7 +91,8 @@ files:
91
91
  - lib/accuweather/conditions/local.rb
92
92
  - lib/accuweather/conditions/parser.rb
93
93
  - lib/accuweather/conditions/units.rb
94
- - lib/accuweather/location.rb
94
+ - lib/accuweather/location/city.rb
95
+ - lib/accuweather/location/parser.rb
95
96
  - lib/accuweather/version.rb
96
97
  homepage: https://github.com/nick-aschenbach/accuweather
97
98
  licenses: []
@@ -1,27 +0,0 @@
1
- module Accuweather
2
- class Location
3
- attr_reader :id, :city, :state, :latitude, :longitude
4
-
5
- def initialize(id:, city:, state:, latitude:, longitude:)
6
- @id = id
7
- @city = city
8
- @state = state
9
- @latitude = latitude
10
- @longitude = longitude
11
- end
12
-
13
- def ==(other)
14
- id == other.id &&
15
- city == other.city &&
16
- state == other.state &&
17
- latitude == other.latitude &&
18
- longitude == other.longitude
19
- rescue NoMethodError
20
- false
21
- end
22
-
23
- def to_s
24
- "id: #{id}, city: #{city}, state: #{state}, latitude: #{latitude}, longitude: #{longitude}"
25
- end
26
- end
27
- end