owmo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6ac6a3db29e87019c86229bcfe4d0e3b7a001080
4
+ data.tar.gz: 067bec01b6055748a4844d8aa380fd06b0c37324
5
+ SHA512:
6
+ metadata.gz: 4f4d0980495978f29e7b4b93ffe6ea9904c82382b6e3325a88a7c401f7f971fed8148071029e1be68479515f273a5f12b6bd6b6ce514f679749dcfa9b7a825f4
7
+ data.tar.gz: db1d47014dd4d26e646826a1b7708a0c62542fd71d480377eac568cc4992b2bb0f7c3ba02bf3d2203d96bf2d56470e560f2d050a57d4fc70f41a0b623a1348fc
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+
15
+ *.gem
16
+ test.rb
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in owmo.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Robb
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/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # Owmo
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/owmo`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'owmo'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install owmo
22
+
23
+ ## Usage
24
+
25
+ You'll need and API key from OpenWeatherMap.org (http://openweathermap.org/appid).
26
+
27
+ Either instanciate the OWMO::Weather class:
28
+ ```ruby
29
+ require 'owmo'
30
+ api_key = ""
31
+
32
+ weather = OWMO::Weather.new api_key: api_key
33
+ ```
34
+
35
+ or through the OWMO::weather method block:
36
+ ```ruby
37
+ require 'owmo'
38
+ api_key = ""
39
+
40
+ OWMO::weather api_key: api_key do |weather|
41
+ puts weather.get :current, city_name: "London,uk"
42
+ end
43
+ ```
44
+
45
+ **Current weather data** (http://openweathermap.org/current)
46
+ ```ruby
47
+ params = {
48
+ city_name: "London,uk", # [city_name, city_id, zip, lat/lon]
49
+ mode: 'json', # [json, xml, html] Not required, but an option
50
+ units: 'imperial', # [imperial, metric] Not required, but an option
51
+ lang: 'en_US' # Not required, but an option
52
+ }
53
+
54
+ puts weather.get :current, params
55
+
56
+ ```
57
+ **5 day weather forecast** (http://openweathermap.org/forecast5)
58
+ ```ruby
59
+ params = {
60
+ zip: "90210", # [city_name, city_id, zip, lat/lon]
61
+ mode: 'json', # [json, xml, html] Not required, but an option
62
+ units: 'imperial', # [imperial, metric] Not required, but an option
63
+ lang: 'en_US' # Not required, but an option
64
+ }
65
+
66
+ puts weather.get :forecast, params
67
+ ```
68
+
69
+ **16 day weather forecast** (http://openweathermap.org/forecast16)
70
+ ```ruby
71
+ params = {
72
+ lat: "40.7128", lon: "74.0059", # [city_name, city_id, zip, lat/lon]
73
+ mode: 'json', # [json, xml, html] Not required, but an option
74
+ units: 'imperial', # [imperial, metric] Not required, but an option
75
+ lang: 'en_US' # Not required, but an option
76
+ }
77
+
78
+ puts weather.get :extended, params
79
+ ```
80
+
81
+ ## Development
82
+
83
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
84
+
85
+ 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).
86
+
87
+ ## Contributing
88
+
89
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/owmo.
90
+
91
+
92
+ ## License
93
+
94
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
95
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "owmo"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,18 @@
1
+ require 'owmo'
2
+
3
+ api_key = ""
4
+
5
+ OWMO::weather api_key: api_key do |weather|
6
+
7
+ # http://openweathermap.org/current#data
8
+ params = {
9
+ city_name: "London,uk", # [city_name, city_id, zip, lat/lon]
10
+ mode: 'json', # [json, xml, html] Not required, but an option
11
+ units: 'imperial', # [imperial, metric] Not required, but an option
12
+ lang: 'en_US' # Not required, but an option
13
+ }
14
+
15
+ current_condition = weather.get :current, params
16
+
17
+ puts current_condition
18
+ end
@@ -0,0 +1,18 @@
1
+ require 'owmo'
2
+
3
+ api_key = ""
4
+
5
+ OWMO::weather api_key: api_key do |weather|
6
+
7
+ # http://openweathermap.org/forecast16
8
+ params = {
9
+ lat: "40.7128", lon: "74.0059", # [city_name, city_id, zip, lat/lon]
10
+ mode: 'json', # [json, xml, html] Not required, but an option
11
+ units: 'imperial', # [imperial, metric] Not required, but an option
12
+ lang: 'en_US' # Not required, but an option
13
+ }
14
+
15
+ extended = weather.get :extended, params
16
+
17
+ puts extended
18
+ end
@@ -0,0 +1,18 @@
1
+ require 'owmo'
2
+
3
+ api_key = ""
4
+
5
+ OWMO::weather api_key: api_key do |weather|
6
+
7
+ # http://openweathermap.org/forecast5
8
+ params = {
9
+ zip: "90210", # [city_name, city_id, zip, lat/lon]
10
+ mode: 'json', # [json, xml, html] Not required, but an option
11
+ units: 'imperial', # [imperial, metric] Not required, but an option
12
+ lang: 'en_US' # Not required, but an option
13
+ }
14
+
15
+ forecast = weather.get :forecast, params
16
+
17
+ puts forecast
18
+ end
@@ -0,0 +1,96 @@
1
+ require 'net/http'
2
+
3
+ module OWMO
4
+ class Weather
5
+
6
+ attr_reader :url, :api_key
7
+
8
+ def initialize(**kwargs)
9
+ raise "Missing required api_key" if kwargs[:api_key].nil?
10
+ @url = kwargs[:url] || OWMO::URL
11
+ @api_key = kwargs[:api_key]
12
+ end # initialize
13
+
14
+ public
15
+ def get(path, **params)
16
+ # Parse the path
17
+ path = parse_path(path)
18
+
19
+ # Parse the params
20
+ params = parse_params(params)
21
+
22
+ # Format the URI
23
+ uri = build_uri(path, params)
24
+
25
+ # Get the weather data
26
+ get_weather(uri)
27
+ end # get
28
+
29
+ private
30
+ def parse_path(path)
31
+ case path
32
+ # Current weather data
33
+ when :current then 'weather'
34
+
35
+ # 5 day / 3 hour forecast
36
+ when :forecast then 'forecast'
37
+
38
+ # 16 day / daily forecast
39
+ when :extended then 'forecast/daily'
40
+
41
+ # Raise an error
42
+ else raise "Unknown path: '#{path}'"
43
+
44
+ end # case
45
+ end # path
46
+
47
+ private
48
+ def parse_params(**params)
49
+ case
50
+ # Search by city name
51
+ when params.key?(:city_name) then
52
+ params[:q] = params.delete :city_name
53
+
54
+ # Search by city ID
55
+ when params.key?(:city_id) then
56
+ params[:id] = params.delete :city_id
57
+
58
+ # Search by Zip
59
+ when params.key?(:zip) then
60
+
61
+ # Search by geo location
62
+ when params.key?(:lat) && params.key?(:lon) then
63
+
64
+ # Nothing is specified, raise an error
65
+ else raise "Missing location parameter: #{params}"
66
+ end # case
67
+
68
+ params
69
+ end # parse_params
70
+
71
+ private
72
+ def build_uri(path, **params)
73
+ URI "#{@url}/#{path}?#{format_parameters(params)}"
74
+ end # format_uri
75
+
76
+ private
77
+ def format_parameters(**params)
78
+ params[:APPID] = @api_key if params[:APPID].nil?
79
+ params.map {|k,v| "#{k}=#{v}"}.join('&')
80
+ end # format_params
81
+
82
+ private
83
+ def get_weather(uri)
84
+ Net::HTTP.start(uri.host, uri.port) do |http|
85
+ req = Net::HTTP::Get.new uri
86
+ resp = http.request req
87
+
88
+ # Error if the request was unsuccessful
89
+ raise "ERROR #{resp.code}: #{res.message} (#{res.class.name})" unless Net::HTTPSuccess
90
+
91
+ resp.body
92
+ end # http
93
+ end # get_weather
94
+
95
+ end # Weather
96
+ end # OWMO
@@ -0,0 +1,3 @@
1
+ module Owmo
2
+ VERSION = "0.1.0"
3
+ end
data/lib/owmo.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "owmo/version"
2
+ require "owmo/Weather"
3
+
4
+
5
+ module OWMO
6
+ URL = 'http://api.openweathermap.org/data/2.5'
7
+
8
+ public
9
+ def self.weather(**params)
10
+ w = Weather.new params
11
+ yield w
12
+ end # weather
13
+
14
+ end
data/owmo.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'owmo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "owmo"
8
+ spec.version = Owmo::VERSION
9
+ spec.authors = ["Robb"]
10
+ spec.email = ["robb.randall@gmail.com"]
11
+
12
+ spec.summary = %q{OpenWeatherMap.org client for current and forecasted weather conditions.}
13
+ spec.description = %q{OpenWeatherMap.org client for current and forecasted weather conditions.}
14
+ spec.homepage = "https://github.com/robb-randall/owmo"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.14"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: owmo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Robb
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: OpenWeatherMap.org client for current and forecasted weather conditions.
56
+ email:
57
+ - robb.randall@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - examples/current_conditions.rb
72
+ - examples/extended.rb
73
+ - examples/forecast.rb
74
+ - lib/owmo.rb
75
+ - lib/owmo/Weather.rb
76
+ - lib/owmo/version.rb
77
+ - owmo.gemspec
78
+ homepage: https://github.com/robb-randall/owmo
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.4.5.2
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: OpenWeatherMap.org client for current and forecasted weather conditions.
102
+ test_files: []