oweather 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +24 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/OW.gemspec +26 -0
- data/README.md +90 -0
- data/Rakefile +9 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/OW.rb +3 -0
- data/lib/OW/client.rb +77 -0
- data/lib/OW/version.rb +3 -0
- data/lib/OW/weather.rb +21 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8a0f5221cebf4676607e1653e3696720567ce8ba
|
4
|
+
data.tar.gz: 3d4864eac797cd15c2c93f1db68230c3826b1cf5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b9662019bc5a3c0c2321f97c5fe704855e4e07e8eed636932b411ba2e73eee2a3ff977627e8176f973e1c78fd924dffb3d20523b1dcac8477924ed189f6108da
|
7
|
+
data.tar.gz: f8340cc7741d6147114e1e6164d59571745cace41e70fc1f2ddc58ab4b8141cb618b2fa95a0d32397a13d06336ac2f5f15e2444eaaca6f8a2ce386a95f01d739
|
data/.gitignore
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
.ruby-gemset
|
24
|
+
.ruby-version
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Lucas Ocon
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/OW.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'OW/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "oweather"
|
8
|
+
spec.version = OW::VERSION
|
9
|
+
spec.authors = ["Lucas Ocon"]
|
10
|
+
spec.email = ["lucas_masterclas@hotmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Wrapper the OpenWeatherMap API}
|
13
|
+
spec.description = %q{A minimal Ruby Gem to wrap the OpenWeatherMap API http://openweathermap.org}
|
14
|
+
spec.homepage = "https://github.com/lucasocon/oweather"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "minitest", "~> 5.5.1"
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_dependency "json"
|
26
|
+
end
|
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# Oweather [](http://badge.fury.io/rb/openweather2)
|
2
|
+
|
3
|
+
This gem is an abstraction to the OpenWeatherMap API.
|
4
|
+
|
5
|
+
This gem provide the weather from numerous regions around the world.
|
6
|
+
|
7
|
+
Also you can get some data on the gem response:
|
8
|
+
* City name
|
9
|
+
* Longitude
|
10
|
+
* Latitude
|
11
|
+
* Temperature
|
12
|
+
* Pressure
|
13
|
+
* Humidity
|
14
|
+
* Min temperature
|
15
|
+
* Max temperature
|
16
|
+
* Clouds
|
17
|
+
* Wind speed
|
18
|
+
* Wind angle
|
19
|
+
|
20
|
+
You can get your APP ID from the OpenWeatherMap website.
|
21
|
+
|
22
|
+
http://openweathermap.org/appid#get
|
23
|
+
|
24
|
+
## Installation
|
25
|
+
|
26
|
+
Add this line to your application's Gemfile:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
gem 'oweather'
|
30
|
+
```
|
31
|
+
|
32
|
+
And then execute:
|
33
|
+
|
34
|
+
$ bundle
|
35
|
+
|
36
|
+
Or install it yourself as:
|
37
|
+
|
38
|
+
$ gem install oweather
|
39
|
+
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
Usage:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
require "oweather"
|
46
|
+
|
47
|
+
OW.configure do |config|
|
48
|
+
config.endpoint = 'http://api.openweathermap.org/data/2.5/weather'
|
49
|
+
config.apikey = YOUR APP ID
|
50
|
+
end
|
51
|
+
|
52
|
+
OW.get_weather(city: 'london')
|
53
|
+
|
54
|
+
|
55
|
+
OW.get_weather(zip: xxxx)
|
56
|
+
|
57
|
+
|
58
|
+
OW.get_weather(lat: xxxx, lon: xxxx)
|
59
|
+
|
60
|
+
```
|
61
|
+
|
62
|
+
Also you can provide a second parameter for the units to convert the temperature to 'metric' or 'imperial'
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
|
66
|
+
OW.get_weather(city: 'london', units: 'metric')
|
67
|
+
OW.get_weather(city: 'london', units: 'imperial')
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
|
72
|
+
## Development
|
73
|
+
|
74
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
75
|
+
|
76
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
77
|
+
|
78
|
+
## Contributing
|
79
|
+
|
80
|
+
Be free to report and send your pull request.
|
81
|
+
|
82
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/lucasocon/oweather.
|
83
|
+
|
84
|
+
Contributors:
|
85
|
+
* Lucas Ocon (lucasocon)
|
86
|
+
|
87
|
+
## License
|
88
|
+
|
89
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
90
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "OW"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/OW.rb
ADDED
data/lib/OW/client.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/http'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module OW
|
6
|
+
|
7
|
+
class << self
|
8
|
+
attr_accessor :configuration
|
9
|
+
end
|
10
|
+
|
11
|
+
class Configuration
|
12
|
+
attr_accessor :endpoint, :apikey
|
13
|
+
end
|
14
|
+
|
15
|
+
class UnprocessableError < RuntimeError; end
|
16
|
+
class ForbiddenError < RuntimeError; end
|
17
|
+
class UnknownResponse < RuntimeError; end
|
18
|
+
|
19
|
+
extend self
|
20
|
+
|
21
|
+
def configure
|
22
|
+
self.configuration ||= Configuration.new
|
23
|
+
yield(configuration)
|
24
|
+
end
|
25
|
+
|
26
|
+
def get_weather(options={})
|
27
|
+
check_configuration!
|
28
|
+
uri = set_params(options)
|
29
|
+
response = send_request(uri)
|
30
|
+
parse_json(response)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
def parse_json(response)
|
35
|
+
case response
|
36
|
+
when Net::HTTPSuccess
|
37
|
+
check_response(response)
|
38
|
+
when Net::HTTPUnprocessableEntity
|
39
|
+
raise UnprocessableError, 'Bad URI param!'
|
40
|
+
else
|
41
|
+
raise UnknownResponse, 'Something was wrong!'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def check_response(response)
|
46
|
+
json = JSON.parse(response.body)
|
47
|
+
if json['cod'] == 200
|
48
|
+
OW::Weather.new(json)
|
49
|
+
else
|
50
|
+
nil
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def send_request(uri)
|
55
|
+
req = Net::HTTP::Get.new(uri.request_uri)
|
56
|
+
http_params = [uri.hostname, uri.port, use_ssl: uri.scheme == 'https']
|
57
|
+
Net::HTTP.start(*http_params) {|http| http.request(req)}
|
58
|
+
end
|
59
|
+
|
60
|
+
def set_params(options)
|
61
|
+
uri = URI(OW.configuration.endpoint)
|
62
|
+
uri.query = URI.encode_www_form(default_params.merge(lat: options[:lat], lon: options[:lon], units: options[:units])) if options[:lat] && options[:lon]
|
63
|
+
uri.query = URI.encode_www_form(default_params.merge(zip: options[:zip], units: options[:units])) if options[:zip]
|
64
|
+
uri.query = URI.encode_www_form(default_params.merge(q: options[:city], units: options[:units])) if options[:city]
|
65
|
+
uri
|
66
|
+
end
|
67
|
+
|
68
|
+
def check_configuration!
|
69
|
+
if OW.configuration.instance_variables.size < 2
|
70
|
+
raise ArgumentError, 'You must configure OW'
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def default_params
|
75
|
+
{APPID: OW.configuration.apikey}
|
76
|
+
end
|
77
|
+
end
|
data/lib/OW/version.rb
ADDED
data/lib/OW/weather.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module OW
|
2
|
+
class Weather
|
3
|
+
|
4
|
+
attr_reader :city, :longitude, :latitude, :pressure, :humidity, :clouds,
|
5
|
+
:temperature, :min_temperature, :max_temperature, :wind_speed, :wind_angle
|
6
|
+
|
7
|
+
def initialize(json)
|
8
|
+
@city = json['name']
|
9
|
+
@longitude = json['coord'] && json['coord']['lon']
|
10
|
+
@latitude = json['coord'] && json['coord']['lat']
|
11
|
+
@temperature = json['main']['temp']
|
12
|
+
@pressure = json['main']['pressure']
|
13
|
+
@humidity = json['main']['humidity']
|
14
|
+
@min_temperature = json['main']['temp_min']
|
15
|
+
@max_temperature = json['main']['temp_max']
|
16
|
+
@clouds = json['clouds']['all']
|
17
|
+
@wind_speed = json['wind']['speed']
|
18
|
+
@wind_angle = json['wind']['deg']
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: oweather
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lucas Ocon
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.5.1
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.5.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: json
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: A minimal Ruby Gem to wrap the OpenWeatherMap API http://openweathermap.org
|
70
|
+
email:
|
71
|
+
- lucas_masterclas@hotmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- OW.gemspec
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- lib/OW.rb
|
86
|
+
- lib/OW/client.rb
|
87
|
+
- lib/OW/version.rb
|
88
|
+
- lib/OW/weather.rb
|
89
|
+
homepage: https://github.com/lucasocon/oweather
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.4.8
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Wrapper the OpenWeatherMap API
|
113
|
+
test_files: []
|