hg-weather 1.0.0 → 1.1.0

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: 90cd9bd02cbd13eed635c443f44bb5e8fcdfb81a
4
- data.tar.gz: f517d2c150624a901a2c915401be1bf8567c3abf
3
+ metadata.gz: f4fdfeecb4484bfd67647bcce854bd82892dc38f
4
+ data.tar.gz: 4b36946f59a97023f69607712cc2d0ea0877b2c8
5
5
  SHA512:
6
- metadata.gz: 574a8ea89dc7f2426eb28034b42b2a73a0ffbea635fd18b60bbc978e04224dfb75814527c454f7a2b5f9cbad94fb8122b7695c63c89770acdf6f539a0c832f01
7
- data.tar.gz: f583a3ee34023c08c0682bc42e7a1fbd3337c2916a0b19f5e7a98ddad769d7ec2d8eb02e2dab608f96e1a4c15f266ad395f19876b3ad9125e9b493c9f5a9c812
6
+ metadata.gz: 09aa3c8dffd9c81225f6ed0c853dcde9df95a5499db008d6b9603f0feb9249d3efac0d251f06a2777e6b8921b0f94c4f1cc3afb29ca6ddab4f475b83eb499e6b
7
+ data.tar.gz: 7adc4d68ba440bf90ee797e114d398a6dca054c0c592deb52cb6c1bf0a69ff1d09804aebba77645e52ce56031cb1cc82513bfd92e42ec716214b7a14d7411a45
@@ -11,24 +11,45 @@ module HG
11
11
  class << self
12
12
  # API Key
13
13
  attr_accessor :api_key
14
- @@api_key = nil
14
+
15
+ # Locale
16
+ attr_accessor :locale
17
+
18
+ # Default temperature format
19
+ attr_accessor :temperature
20
+
21
+ # Default speed format
22
+ attr_accessor :speed
15
23
 
16
24
  # API Key status
17
25
  attr_reader :api_key_status
18
- @@api_key_status = :unknown
19
26
 
20
27
  # Use SSL to access the API
21
28
  attr_accessor :use_ssl
22
- @@use_ssl = true
23
29
 
24
30
  # Use Rails cache for recieved data (realy recommended)
25
31
  attr_accessor :use_rails_cache
26
- @@use_rails_cache = true
27
32
 
28
- attr_accessor :cid, :city_name, :latitude, :longitude, :client_ip
33
+ attr_accessor :cid, :woeid, :city_name, :latitude, :longitude, :client_ip
29
34
  @@client_ip = :remote
30
35
  end
31
36
 
37
+ def self.locale
38
+ @locale || :en
39
+ end
40
+
41
+ def self.temperature
42
+ @temperature || Locale.get_format(:temperature)
43
+ end
44
+
45
+ def self.speed
46
+ @speed || Locale.get_format(:speed)
47
+ end
48
+
49
+ def self.use_ssl
50
+ @use_ssl || true
51
+ end
52
+
32
53
  def self.setup(&block)
33
54
  yield self
34
55
  end
@@ -37,7 +58,7 @@ module HG
37
58
  to_request = {
38
59
  lat: options[:latitude], lon: options[:longitude],
39
60
  cid: options[:cid], city_name: options[:city_name],
40
- user_ip: options[:client_ip]
61
+ user_ip: options[:client_ip], woeid: options[:woeid]
41
62
  }.delete_if{|k,v| v.nil?}
42
63
 
43
64
  process(to_request)
@@ -55,8 +76,10 @@ module HG
55
76
  lon: self.longitude,
56
77
  user_ip: self.client_ip,
57
78
  cid: self.cid,
79
+ woeid: self.woeid,
58
80
  city_name: self.city_name,
59
81
  key: self.api_key,
82
+ locale: self.locale,
60
83
  format: :json,
61
84
  sdk_version: "ruby_#{HG::Weather::VERSION}"
62
85
  }
@@ -1,5 +1,6 @@
1
1
  require 'hg/weather/temperature'
2
2
  require 'hg/weather/speed'
3
+ require 'hg/weather/locale'
3
4
 
4
5
  module HG
5
6
  module Weather
@@ -56,7 +57,7 @@ module HG
56
57
  @description = options[:description] if options[:description]
57
58
  @slug = options[:slug].to_sym if options[:slug]
58
59
  @wind_speed = Speed.new(options[:wind_speed]) if options[:wind_speed]
59
- @currently = (options[:currently] == 'dia' ? :day : :night) if options[:currently]
60
+ @currently = (options[:currently] == Locale.get_format(:day).to_s ? :day : :night) if options[:currently]
60
61
  @datetime = process_datetime(options[:date], options[:time]) if options[:date]
61
62
  @sunrise = process_sunrise(options[:sunrise]) if options[:sunrise]
62
63
  @sunset = process_sunset(options[:sunset]) if options[:sunset]
@@ -77,7 +78,7 @@ module HG
77
78
  def to_s separator = ' - '
78
79
  to_return = []
79
80
 
80
- to_return << self.datetime.strftime('%d/%m') if self.datetime && self.datetime.kind_of?(Time) && self.is_forecast
81
+ to_return << self.datetime.strftime(Locale.get_format(:short_date)) if self.datetime && self.datetime.kind_of?(Time) && self.is_forecast
81
82
 
82
83
  to_return << self.temperature.to_s if self.temperature
83
84
  to_return << 'Max: ' + self.max_temperature.to_s if self.max_temperature
@@ -86,19 +87,23 @@ module HG
86
87
  to_return << self.humidity.to_s + ' %' if self.humidity
87
88
  to_return << self.wind_speed.to_s if self.wind_speed
88
89
 
89
- to_return << 'Sunrise: ' + self.sunrise.strftime('%H:%M') if self.sunrise && self.sunrise.kind_of?(Time)
90
- to_return << 'Sunset: ' + self.sunset.strftime('%H:%M') if self.sunset && self.sunset.kind_of?(Time)
90
+ to_return << "#{Locale.get_format(:sunrise).to_s.capitalize}: " + self.sunrise.strftime('%H:%M') if self.sunrise && self.sunrise.kind_of?(Time)
91
+ to_return << "#{Locale.get_format(:sunset).to_s.capitalize}: " + self.sunset.strftime('%H:%M') if self.sunset && self.sunset.kind_of?(Time)
91
92
 
92
93
  to_return << self.description.to_s if self.description
93
94
 
94
95
  return to_return.join(separator)
95
96
  end
96
97
 
98
+ def inspect
99
+ self.to_s
100
+ end
101
+
97
102
  protected
98
103
  def process_datetime date, time = nil
99
104
  return Time.now if date.nil?
100
105
 
101
- return Time.strptime((date + ' ' + (time ? time : '00:00')), '%d/%m/%Y %H:%M')
106
+ return Time.strptime((date + ' ' + (time ? time : '00:00')), Locale.get_format(:datetime))
102
107
  end
103
108
 
104
109
  def process_sunset sunset
@@ -6,7 +6,7 @@ module HG
6
6
  class Data
7
7
 
8
8
  attr_accessor :request, :requested_at, :key_status
9
- attr_accessor :condition, :forecast
9
+ attr_accessor :condition, :forecast, :woeid
10
10
  attr_accessor :cid, :city_name, :search_method
11
11
 
12
12
  def initialize params, host_name, use_ssl = true
@@ -19,11 +19,12 @@ module HG
19
19
  if request_data['results']
20
20
  results = request_data['results']
21
21
 
22
- @key_status = (params[:key] ? (request_data['key_status'] == 'valida' ? :valid : :invalid) : :empty)
22
+ @key_status = (params[:key] ? (request_data['key_status'] ? :valid : :invalid) : :empty)
23
23
 
24
24
  @city_name = results['city_name']
25
25
  @search_method = request_data['by']
26
26
  @cid = results['cid']
27
+ @woeid = params[:woeid] if request_data['by'] == 'woeid'
27
28
 
28
29
  @condition = Condition.new(to_current(results))
29
30
 
@@ -57,7 +58,7 @@ module HG
57
58
  # TODO: Improve to get year of date
58
59
  def to_forecast r
59
60
  {
60
- date: r['date'] + '/' + Time.now.year.to_s,
61
+ date: (Weather.locale == :en ? Time.now.year.to_s + '-' + r['date'] : r['date'] + '/' + Time.now.year.to_s),
61
62
  max_temperature: r['max'],
62
63
  min_temperature: r['min'],
63
64
  description: r['description'],
@@ -0,0 +1,38 @@
1
+ module HG
2
+ module Weather
3
+
4
+ class Locale
5
+
6
+ @formats = {
7
+ en: {
8
+ date: '%Y-%m-%d',
9
+ short_date: '%m-%d',
10
+ datetime: '%Y-%m-%d %H:%M',
11
+ temperature: :fahrenheit,
12
+ speed: :miles,
13
+ day: :day,
14
+ sunrise: 'sunrise',
15
+ sunset: 'sunset'
16
+ },
17
+ :'pt-br' => {
18
+ date: '%d/%m/%Y',
19
+ short_date: '%d/%m',
20
+ datetime: '%d/%m/%Y %H:%M',
21
+ temperature: :celsius,
22
+ speed: :km,
23
+ day: :dia,
24
+ sunrise: 'nascer do sol',
25
+ sunset: 'por do sol'
26
+ }
27
+ }
28
+
29
+ def self.get_format item
30
+ Weather.locale = :en unless @formats.has_key?(Weather.locale.to_sym)
31
+
32
+ return @formats[Weather.locale.to_sym][item]
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+ end
@@ -13,10 +13,19 @@ module HG
13
13
 
14
14
  if format == :km
15
15
  @km_h = speed
16
- @miles_h = speed * 0.621371
16
+ @miles_h = (speed * 0.621371).round(2)
17
17
  else
18
18
  @miles_h = speed
19
- @km_h = speed * 1.60934
19
+ @km_h = (speed * 1.60934).round(2)
20
+ end
21
+ end
22
+
23
+ def get_with_format
24
+ case Weather.speed
25
+ when :km
26
+ @km_h
27
+ when :miles
28
+ @miles_h
20
29
  end
21
30
  end
22
31
 
@@ -25,7 +34,15 @@ module HG
25
34
  end
26
35
 
27
36
  def to_s
28
- "#{@km_h} km/h"
37
+ "#{self.get_with_format} #{Weather.speed.to_s.downcase}/h"
38
+ end
39
+
40
+ def to_f
41
+ self.get_with_format
42
+ end
43
+
44
+ def to_i
45
+ self.get_with_format.to_i
29
46
  end
30
47
  end
31
48
 
@@ -11,10 +11,19 @@ module HG
11
11
 
12
12
  if format == :c
13
13
  @celsius = temperature
14
- @fahrenheit = (temperature * 1.8) + 32
14
+ @fahrenheit = ((temperature * 1.8) + 32).round(2)
15
15
  else
16
16
  @fahrenheit = temperature
17
- @celsius = (temperature - 32) / 1.8
17
+ @celsius = ((temperature - 32) / 1.8).round(2)
18
+ end
19
+ end
20
+
21
+ def get_with_format
22
+ case Weather.temperature
23
+ when :celsius
24
+ @celsius
25
+ when :fahrenheit
26
+ @fahrenheit
18
27
  end
19
28
  end
20
29
 
@@ -23,7 +32,15 @@ module HG
23
32
  end
24
33
 
25
34
  def to_s
26
- "#{@celsiusC"
35
+ "#{self.get_with_format#{Weather.temperature.to_s[0].upcase}"
36
+ end
37
+
38
+ def to_f
39
+ self.get_with_format
40
+ end
41
+
42
+ def to_i
43
+ self.get_with_format.to_i
27
44
  end
28
45
  end
29
46
 
@@ -1,5 +1,5 @@
1
1
  module HG
2
2
  module Weather
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hg-weather
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hugo Demiglio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-06 00:00:00.000000000 Z
11
+ date: 2016-07-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Get current and forecast weather using HG Weather API.
14
14
  email:
@@ -20,6 +20,7 @@ files:
20
20
  - lib/hg/weather.rb
21
21
  - lib/hg/weather/condition.rb
22
22
  - lib/hg/weather/data.rb
23
+ - lib/hg/weather/locale.rb
23
24
  - lib/hg/weather/speed.rb
24
25
  - lib/hg/weather/temperature.rb
25
26
  - lib/hg/weather/version.rb