hg-weather 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/hg/weather.rb +66 -0
- data/lib/hg/weather/condition.rb +104 -0
- data/lib/hg/weather/data.rb +71 -0
- data/lib/hg/weather/temperature.rb +23 -0
- data/lib/hg/weather/version.rb +5 -0
- metadata +49 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c7d4ec3fd09af7a164322701fa97a41c42175df8
|
4
|
+
data.tar.gz: 0ceb34a84d0615f0066ee6fc436b269f03c357aa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 59d305e2b8af1144dc45abcffb5ef27edbb3521fbf8a5faf0945dae92124241085fd32c0df2d487d914db46af1b412e7c5fd415cff1a3404fc87e8c552315705
|
7
|
+
data.tar.gz: f941ba9d787daaf439193e0649ffc80d273c5a026a2c2aa943920fa29390ed8e9357499569f37a97ec12696f9fec16da00fc55c8dbee7939fd8276eafde70272
|
data/lib/hg/weather.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
require 'hg/weather/version'
|
5
|
+
require 'hg/weather/data'
|
6
|
+
|
7
|
+
module HG
|
8
|
+
module Weather
|
9
|
+
HOST_NAME = '://api.hgbrasil.com/weather'
|
10
|
+
|
11
|
+
class << self
|
12
|
+
# API Key
|
13
|
+
attr_accessor :api_key
|
14
|
+
@@api_key = nil
|
15
|
+
|
16
|
+
# API Key status
|
17
|
+
attr_reader :api_key_status
|
18
|
+
@@api_key_status = :unknown
|
19
|
+
|
20
|
+
# Use SSL to access the API
|
21
|
+
attr_accessor :use_ssl
|
22
|
+
@@use_ssl = true
|
23
|
+
|
24
|
+
# Use Rails cache for recieved data (realy recommended)
|
25
|
+
attr_accessor :use_rails_cache
|
26
|
+
@@use_rails_cache = true
|
27
|
+
|
28
|
+
attr_accessor :cid, :city_name, :latitude, :longitude, :client_ip
|
29
|
+
@@client_ip = :remote
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.setup(&block)
|
33
|
+
yield self
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.get(options = {})
|
37
|
+
to_request = {
|
38
|
+
lat: options[:latitude], lon: options[:longitude],
|
39
|
+
cid: options[:cid], city_name: options[:city_name],
|
40
|
+
user_ip: options[:client_ip]
|
41
|
+
}.delete_if{|k,v| v.nil?}
|
42
|
+
|
43
|
+
process(to_request)
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.process params
|
47
|
+
params = defaults.merge(params).delete_if{|k,v| v.nil?}
|
48
|
+
|
49
|
+
return HG::Weather::Data.new(params, HOST_NAME, self.use_ssl)
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.defaults
|
53
|
+
{
|
54
|
+
lat: self.latitude,
|
55
|
+
lon: self.longitude,
|
56
|
+
user_ip: self.client_ip,
|
57
|
+
cid: self.cid,
|
58
|
+
city_name: self.city_name,
|
59
|
+
key: self.api_key,
|
60
|
+
format: :json,
|
61
|
+
sdk_version: "ruby_#{HG::Weather::VERSION}"
|
62
|
+
}
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'hg/weather/temperature'
|
2
|
+
|
3
|
+
module HG
|
4
|
+
module Weather
|
5
|
+
|
6
|
+
class Condition
|
7
|
+
ONE_DAY = 86_400
|
8
|
+
|
9
|
+
# Public: Temperature
|
10
|
+
attr_accessor :temperature
|
11
|
+
|
12
|
+
# Public: Max Temperature
|
13
|
+
attr_accessor :max_temperature
|
14
|
+
|
15
|
+
# Public: Min Temperature
|
16
|
+
attr_accessor :min_temperature
|
17
|
+
|
18
|
+
# Public: Humidity
|
19
|
+
attr_accessor :humidity
|
20
|
+
|
21
|
+
# Public: Image ID
|
22
|
+
attr_accessor :image_id
|
23
|
+
|
24
|
+
# Public: Description
|
25
|
+
attr_accessor :description
|
26
|
+
|
27
|
+
# Public: Slug
|
28
|
+
attr_accessor :slug
|
29
|
+
|
30
|
+
# Public: Wind speedy
|
31
|
+
attr_accessor :wind_speed
|
32
|
+
|
33
|
+
# Public: Sunrise
|
34
|
+
attr_accessor :sunrise
|
35
|
+
|
36
|
+
# Public: Sunset
|
37
|
+
attr_accessor :sunset
|
38
|
+
|
39
|
+
# Public: Currently, day or night
|
40
|
+
attr_accessor :currently
|
41
|
+
|
42
|
+
# Public: Datetime, if forecast time is always midnight
|
43
|
+
attr_accessor :datetime
|
44
|
+
|
45
|
+
# Public: Is forecast
|
46
|
+
attr_accessor :is_forecast
|
47
|
+
|
48
|
+
def initialize(options = {})
|
49
|
+
if options.count != 0
|
50
|
+
@temperature = Temperature.new(options[:temperature]) if options[:temperature]
|
51
|
+
@max_temperature = Temperature.new(options[:max_temperature]) if options[:max_temperature]
|
52
|
+
@min_temperature = Temperature.new(options[:min_temperature]) if options[:min_temperature]
|
53
|
+
@humidity = options[:humidity].to_i if options[:humidity]
|
54
|
+
@image_id = options[:image_id] if options[:image_id]
|
55
|
+
@description = options[:description] if options[:description]
|
56
|
+
@slug = options[:slug].to_sym if options[:slug]
|
57
|
+
@wind_speed = options[:wind_speed] if options[:wind_speed]
|
58
|
+
@currently = (options[:currently] == 'dia' ? :day : :night) if options[:currently]
|
59
|
+
@datetime = process_datetime(options[:date], options[:time]) if options[:date]
|
60
|
+
@sunrise = process_sunrise(options[:sunrise]) if options[:sunrise]
|
61
|
+
@sunset = process_sunset(options[:sunset]) if options[:sunset]
|
62
|
+
@is_forecast = options[:is_forecast] if options[:is_forecast]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def is_day?
|
67
|
+
return nil if self.currently.nil?
|
68
|
+
self.currently == :day
|
69
|
+
end
|
70
|
+
|
71
|
+
def is_night?
|
72
|
+
return nil if self.currently.nil?
|
73
|
+
self.currently == :night
|
74
|
+
end
|
75
|
+
|
76
|
+
protected
|
77
|
+
def process_datetime date, time = nil
|
78
|
+
return Time.now if date.nil?
|
79
|
+
|
80
|
+
return Time.strptime((date + ' ' + (time ? time : '00:00')), '%d/%m/%Y %H:%M')
|
81
|
+
end
|
82
|
+
|
83
|
+
def process_sunset sunset
|
84
|
+
return nil if sunrise.nil? || self.datetime.nil?
|
85
|
+
sunset = Time.parse(sunset)
|
86
|
+
|
87
|
+
return (sunset > midnight_of(self.datetime + ONE_DAY) ? sunset - ONE_DAY : sunset)
|
88
|
+
end
|
89
|
+
|
90
|
+
def process_sunrise sunrise
|
91
|
+
return nil if sunrise.nil? || self.datetime.nil?
|
92
|
+
sunrise = Time.parse(sunrise)
|
93
|
+
|
94
|
+
return (sunrise < midnight_of(self.datetime + ONE_DAY) ? sunrise + ONE_DAY : sunrise)
|
95
|
+
end
|
96
|
+
|
97
|
+
def midnight_of time
|
98
|
+
time = Time.now if time.nil?
|
99
|
+
Time.new(time.year, time.month, time.day)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'hg/weather/condition'
|
2
|
+
|
3
|
+
module HG
|
4
|
+
module Weather
|
5
|
+
|
6
|
+
class Data
|
7
|
+
|
8
|
+
attr_accessor :request, :requested_at, :key_status
|
9
|
+
attr_accessor :condition, :forecast
|
10
|
+
attr_accessor :cid, :city_name, :search_method
|
11
|
+
|
12
|
+
def initialize params, host_name, use_ssl = true
|
13
|
+
query_params = params.map{|k,v| "#{k.to_s}=#{v.to_s}"}.join('&')
|
14
|
+
@request = (use_ssl ? 'https' : 'http') + host_name + '?' + query_params
|
15
|
+
@requested_at = Time.now
|
16
|
+
|
17
|
+
request_data = JSON.parse(open(self.request).read)
|
18
|
+
|
19
|
+
if request_data['results']
|
20
|
+
results = request_data['results']
|
21
|
+
|
22
|
+
@key_status = (params[:key] ? (request_data['key_status'] == 'valida' ? :valid : :invalid) : :empty)
|
23
|
+
|
24
|
+
@city_name = results['city_name']
|
25
|
+
@search_method = request_data['by']
|
26
|
+
@cid = results['cid']
|
27
|
+
|
28
|
+
@condition = Condition.new(to_current(results))
|
29
|
+
|
30
|
+
@forecast = []
|
31
|
+
results['forecast'].each do |forecast|
|
32
|
+
@forecast << Condition.new(to_forecast(forecast))
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
return self
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
def to_current r
|
41
|
+
{
|
42
|
+
temperature: r['temp'],
|
43
|
+
description: r['description'],
|
44
|
+
slug: r['condition_slug'],
|
45
|
+
wind_speed: r['wind_speedy'],
|
46
|
+
humidity: r['humidity'],
|
47
|
+
image_id: r['image_id'],
|
48
|
+
sunrise: r['sunrise'],
|
49
|
+
sunset: r['sunset'],
|
50
|
+
currently: r['currently'],
|
51
|
+
date: r['date'],
|
52
|
+
time: r['time'],
|
53
|
+
is_forecast: false
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
# TODO: Improve to get year of date
|
58
|
+
def to_forecast r
|
59
|
+
{
|
60
|
+
date: r['date'] + '/' + Time.now.year.to_s,
|
61
|
+
max_temperature: r['max'],
|
62
|
+
min_temperature: r['min'],
|
63
|
+
description: r['description'],
|
64
|
+
condition_slug: r['condition'],
|
65
|
+
is_forecast: true
|
66
|
+
}
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module HG
|
2
|
+
module Weather
|
3
|
+
|
4
|
+
class Temperature
|
5
|
+
attr_accessor :celsius
|
6
|
+
attr_accessor :fahrenheit
|
7
|
+
|
8
|
+
def initialize temperature, format = :c
|
9
|
+
return if temperature.nil?
|
10
|
+
temperature = temperature.to_f
|
11
|
+
|
12
|
+
if format == :c
|
13
|
+
@celsius = temperature
|
14
|
+
@fahrenheit = (temperature * 1.8) + 32
|
15
|
+
else
|
16
|
+
@fahrenheit = temperature
|
17
|
+
@celsius = (temperature - 32) / 1.8
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hg-weather
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hugo Demiglio
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-05 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Get current and forecast weather using HG Weather API.
|
14
|
+
email:
|
15
|
+
- hugodemiglio@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/hg/weather.rb
|
21
|
+
- lib/hg/weather/condition.rb
|
22
|
+
- lib/hg/weather/data.rb
|
23
|
+
- lib/hg/weather/temperature.rb
|
24
|
+
- lib/hg/weather/version.rb
|
25
|
+
homepage: http://hgbrasil.com/weather
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 2.4.8
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: Simple gem to get weather data from HG Weather.
|
49
|
+
test_files: []
|