open_weather 0.0.2 → 0.0.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: be1327553a2fbe1bca7dd99c412460c8fa4b7845
4
- data.tar.gz: 113644f7a050013f6665ec37bc1719d8cc3a6a2a
3
+ metadata.gz: 001deca9d50d6b07ab8284a3ec5c038ee5143fcc
4
+ data.tar.gz: 2a18341ff8cf95e807ddad88d9cfe48bb9b712cf
5
5
  SHA512:
6
- metadata.gz: 0373959b51de904817bae07728a8274cdd4223747869eab547cd381e8c45b5f9a4e8b6d680a006723934e74e138a6bf55749fd638a6056d60127e9c28cfaaad1
7
- data.tar.gz: fb43d1d32607256a4feeffc4f782bcc7a4467add7d88f871bcf8b0b99d4b984182998e450d5a069f71192b3536c7cf9cecb59c6134fe6a637edafc687ef72bc3
6
+ metadata.gz: 36d5b56d5402bd32130489c3e7792a62a865b13c9ba253cddaf09514aadf5376be52e120754fb829fd99897108bff7110bacc95fbd4ecf92cce55b698efd9e11
7
+ data.tar.gz: 10170e99eb833b90d8521dbf66d84d6eb7cd035ad9446bbaa6f5111144cc2e00d7e87385c6192b76453c93000f964cd943b3c9d47a38aaac9390de4c0d5ed909
data/lib/open_weather.rb CHANGED
@@ -6,5 +6,23 @@ require "open_weather/version"
6
6
  require "open_weather/client"
7
7
 
8
8
  module OpenWeather
9
- # Your code goes here...
9
+ def self.current(*location)
10
+ client.weather(*location)
11
+ end
12
+
13
+ def self.forecast(*location)
14
+ client.forecast(*location)
15
+ end
16
+
17
+ def self.daily(*location)
18
+ client.daily_forecast(*location)
19
+ end
20
+
21
+ def self.find(*location)
22
+ client.find(*location)
23
+ end
24
+
25
+ def self.client
26
+ Client.new
27
+ end
10
28
  end
@@ -1,3 +1,5 @@
1
+ require 'open_weather/query_builder'
2
+
1
3
  module OpenWeather
2
4
  class Client
3
5
  include HTTParty
@@ -6,16 +8,24 @@ module OpenWeather
6
8
 
7
9
  format :json
8
10
 
9
- def weather(lat, lon)
10
- get('/weather', lat: lat, lon: lon )
11
+ def weather(*args)
12
+ get '/weather', build_query(args)
13
+ end
14
+
15
+ def daily_forecast(*args)
16
+ get '/forecast/daily', build_query(args)
17
+ end
18
+
19
+ def forecast(*args)
20
+ get '/forecast', build_query(args)
11
21
  end
12
22
 
13
- def forecast(lat, lon)
14
- get('/forecast', lat: lat, lon: lon )
23
+ def find(*args)
24
+ get '/find', build_query(args, false)
15
25
  end
16
26
 
17
- def find(lat, lon)
18
- get('/find', lat: lat, lon: lon )
27
+ def build_query(args, use_id=true)
28
+ QueryBuilder.new(args, use_id).build
19
29
  end
20
30
 
21
31
  def get(path, query)
@@ -0,0 +1,92 @@
1
+ module OpenWeather
2
+ class QueryError < StandardError
3
+ end
4
+
5
+ class QueryBuilder
6
+
7
+ attr_reader :arguments, :query_options
8
+
9
+ def initialize(args, use_id=true)
10
+ clean_args = Array(args)
11
+ @query_options = clean_args.extract_options!
12
+ @arguments = clean_args
13
+ end
14
+
15
+ def build
16
+ query = _from_array(arguments)
17
+ query.merge(query_options)
18
+ end
19
+
20
+ private
21
+
22
+ def _from_array(args)
23
+ query_keys = [:lat, :lon]
24
+ args.size > 1 ? Hash[query_keys.zip(args)] : _from_object(args[0])
25
+ end
26
+
27
+ def _from_string(str)
28
+ key = str =~ /\A\d+\Z/ ? :id : :q
29
+ { key => str }
30
+ end
31
+
32
+ def _from_object(obj)
33
+ case obj
34
+ when NilClass
35
+ raise QueryError, 'you should provide at least one location'
36
+ when Array
37
+ _from_array(obj)
38
+ when Hash
39
+ _from_methods(OpenStruct.new(obj))
40
+ when String
41
+ _from_string(obj)
42
+ when Integer
43
+ { id: obj }
44
+ else
45
+ _from_methods(obj)
46
+ end
47
+ end
48
+
49
+ def _get_lat(obj)
50
+ _fetch_value(obj, :lat) ||
51
+ _fetch_value(obj, :latitude)
52
+ end
53
+
54
+ def _get_lon(obj)
55
+ _fetch_value(obj, :lon) ||
56
+ _fetch_value(obj, :lng) ||
57
+ _fetch_value(obj, :latitude)
58
+ end
59
+
60
+ def _from_lat_lon(obj)
61
+ lat, lon = _get_lat(obj), _get_lon(obj)
62
+ (lat && lon) ? _from_array([ lat, lon ]) : nil
63
+ end
64
+
65
+ def _from_coordinates(obj)
66
+ _from_array(obj.to_coordinates) if obj.respond_to?(:to_coordinates)
67
+ end
68
+
69
+ def _from_other(obj)
70
+ _fetch_array(obj, :location) ||
71
+ _fetch_array(obj, :address) ||
72
+ _fetch_array(obj, :region) ||
73
+ _fetch_array(obj, :city) ||
74
+ _fetch_array(obj, :attributes)
75
+ end
76
+
77
+ def _fetch_value(obj, meth)
78
+ obj.send(meth) if obj.respond_to?(meth)
79
+ end
80
+
81
+ def _fetch_array(obj, meth)
82
+ _from_array(Array(obj.send(meth))) if obj.respond_to?(meth)
83
+ end
84
+
85
+ def _from_methods(obj)
86
+ _from_lat_lon(obj) ||
87
+ _from_coordinates(obj) ||
88
+ _from_other(obj) ||
89
+ raise(QueryError, "Don't know how to obtain weather from #{obj.class.name}")
90
+ end
91
+ end
92
+ end
@@ -1,3 +1,3 @@
1
1
  module OpenWeather
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ module OpenWeather
4
+ describe QueryBuilder do
5
+
6
+ def build_query(*args)
7
+ options = args.extract_options!
8
+ use_id = options.delete(:use_id) || true
9
+ args << options
10
+ QueryBuilder.new(args, use_id).build
11
+ end
12
+
13
+ it "generates a coordinates query when lat and lng are given" do
14
+ build_query(1, 2).should == { lat: 1, lon: 2 }
15
+ end
16
+
17
+ it "generates a coordinates query when object with to_coordinates method is given" do
18
+ waypoint = OpenStruct.new(to_coordinates: [1, 2])
19
+ build_query(waypoint).should == { lat: 1, lon: 2 }
20
+ end
21
+
22
+ it "generates a coordinates query when object with :lat, :lng method available" do
23
+ waypoint = OpenStruct.new(lat: 1, lon: 2)
24
+ build_query(waypoint).should == { lat: 1, lon: 2 }
25
+ end
26
+
27
+ it "generates a city query when an string is given" do
28
+ build_query("Madrid").should == { q: "Madrid" }
29
+ end
30
+
31
+ it "generates an id query when a numeric string is given" do
32
+ build_query("123").should == { id: "123" }
33
+ end
34
+
35
+ it "generates an id query when a numeric id is given" do
36
+ build_query(123).should == { id: 123 }
37
+ end
38
+
39
+ it "parses internal attributes" do
40
+ team = OpenStruct.new(location: "Madrid")
41
+ build_query(team).should == { q: "Madrid" }
42
+ end
43
+
44
+ it "raise query error if location is nil" do
45
+ team = OpenStruct.new(location: nil)
46
+ expect {
47
+ build_query(team)
48
+ }.to raise_error(QueryError, 'you should provide at least one location')
49
+ end
50
+
51
+ it { build_query({lat: 1, lng: 2}, use_id: true).should == { lat: 1, lon: 2 } }
52
+ it { build_query([1, 2]).should == { lat: 1, lon: 2 } }
53
+ it { build_query([1, 2, 3]).should == { lat: 1, lon: 2 } }
54
+ it { build_query(1, 2, 3).should == { lat: 1, lon: 2 } }
55
+
56
+ it "raise query error if no arguments are given" do
57
+ expect {
58
+ build_query
59
+ }.to raise_error(OpenWeather::QueryError, 'you should provide at least one location')
60
+ end
61
+
62
+ it "raise query error if can't figure out the query" do
63
+ ::School = Class.new
64
+ expect {
65
+ build_query(School.new)
66
+ }.to raise_error(QueryError, "Don't know how to obtain weather from School")
67
+ end
68
+
69
+ end
70
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_weather
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jorge Calás Lozano
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-09 00:00:00.000000000 Z
11
+ date: 2013-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -112,8 +112,10 @@ files:
112
112
  - bin/open_weather
113
113
  - lib/open_weather.rb
114
114
  - lib/open_weather/client.rb
115
+ - lib/open_weather/query_builder.rb
115
116
  - lib/open_weather/version.rb
116
117
  - open_weather.gemspec
118
+ - spec/open_weather/query_builder_spec.rb
117
119
  - spec/open_weather_spec.rb
118
120
  - spec/spec_helper.rb
119
121
  homepage: ''
@@ -141,6 +143,7 @@ signing_key:
141
143
  specification_version: 4
142
144
  summary: Ruby client for the Open Weather Map API
143
145
  test_files:
146
+ - spec/open_weather/query_builder_spec.rb
144
147
  - spec/open_weather_spec.rb
145
148
  - spec/spec_helper.rb
146
149
  has_rdoc: