forecast_io-cache 1.0.0 → 2.0.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: 4a3a830da7beaf6331d52777a3f4063cee109e2a
4
- data.tar.gz: 6520296466e74b33bc0bb9376cd13cdfd6402bae
3
+ metadata.gz: d991786947e787fb6e855ca1bafc6a463e93bc69
4
+ data.tar.gz: 4d367c34d9b6b4fe4c33cdfcc25bbee3f1825e08
5
5
  SHA512:
6
- metadata.gz: fb851a96237711c48c802ad39ce747453c3e2604a1c12ec07f55403477ade4d640e630911f38de3553c8b5449c23b1a9f731d4685e40482b89aba52be7bf5725
7
- data.tar.gz: c15589b920e5b570c6021b648fcc38f2add074dfa32d74c88f3b8bc95007b3b89444852c021fe1349e70d5f2c5975624daaa36790ffbc462aea5faeaf3b443be
6
+ metadata.gz: 7e5438f5eb36476ce5be126ad89b9260e54f95786038173b968e2b089cdf377af81450b74090c0a445a926210b931e0145ff7a64799b6ad1dde2acc7f2df34cf
7
+ data.tar.gz: 5c9bda5074194ec975414e9f1a15ba4c9425544dd493c3cc33ef109d538c4f41accc6ebfd92392d67c54baab5632d728642538d76496a882bdc86cd6bae3e368
@@ -1,18 +1,21 @@
1
1
  Given "I have cached this forecast recently" do
2
- @position = [151.238823,-33.857444]
3
- setup "position" => @position, "time" => 123456789
2
+ @latitude = -33.857444
3
+ @longitude = 151.238823
4
+ setup "latitude" => @latitude, "longitude" => @longitude, "time" => 123456789
4
5
  end
5
6
 
6
7
  Given "I have a cache result nearby" do
7
- @position = [151.238823,-33.857333]
8
- setup "position" => @position, "time" => 123456789
8
+ @latitude = -33.857333
9
+ @longitude = 151.238823
10
+ setup "latitude" => @latitude, "longitude" => @longitude, "time" => 123456789
9
11
  end
10
12
 
11
13
  Given "I have a cache result for close by" do
12
- @position = [151.238823,-33.957333]
13
- setup "position" => @position, "time" => 123456789
14
+ @latitude = -33.957333
15
+ @longitude = 151.238823
16
+ setup "latitude" => @latitude, "longitude" => @longitude, "time" => 123456789
14
17
  end
15
18
 
16
19
  Given "I have a cached result but for a time beyond the threshold" do
17
- setup "position" => [151.238823,-33.757444], "time" => 1234567890
20
+ setup "latitude" => -33.757444, "longitude" => 151.238823, "time" => 1234567890
18
21
  end
@@ -21,8 +21,8 @@ Then "I am given the cached forecast" do
21
21
  expect_ok { get @path }
22
22
  expect(parsed_response).to eq({
23
23
  "time" => 123456789,
24
- "latitude" => @position[1],
25
- "longitude" => @position[0],
24
+ "latitude" => @latitude,
25
+ "longitude" => @longitude,
26
26
  "wind_speed" => 6.66,
27
27
  "wind_bearing" => 200,
28
28
  "humidity" => 0.9,
@@ -2,7 +2,7 @@
2
2
  http_interactions:
3
3
  - request:
4
4
  method: get
5
- uri: https://api.forecast.io/forecast/API_KEY/-33.857444,151.238823,123456789
5
+ uri: https://api.darksky.net/forecast/API_KEY/-33.857444,151.238823,123456789
6
6
  body:
7
7
  encoding: US-ASCII
8
8
  string: ''
@@ -1,9 +1,8 @@
1
- require 'bson'
2
-
3
- Forecast::IO.configure do |config|
1
+ ForecastIO.configure do |config|
4
2
  config.api_key = ENV['API_KEY']
5
3
  end
6
4
 
7
5
  Forecast::IO::Cache.configure do |config|
8
- config.mongo_uri 'mongodb://localhost/forecast-test'
6
+ config.radius = 1
7
+ config.db_uri = 'postgres://localhost/forecast-test'
9
8
  end
@@ -1,11 +1,16 @@
1
- require './features/support/mongo'
1
+ require './features/support/pg'
2
2
  module Fixtures
3
3
 
4
+ def db
5
+ @db ||= Sequel.connect 'postgres://localhost/forecast-test'
6
+ end
7
+
4
8
  def setup attrs = {}
5
- MongoAdaptor.db.collection('forecasts').insert(
9
+ db[:forecasts].insert(
6
10
  {
7
11
  "time" => 123456789,
8
- "position" => [151.238823,-33.857444],
12
+ "latitude" => -33.857444,
13
+ "longitude" => 151.238823,
9
14
  "wind_speed" => 6.66,
10
15
  "wind_bearing" => 200,
11
16
  "humidity" => 0.9,
@@ -0,0 +1,6 @@
1
+ After do
2
+ PGAdaptor.db.tables.each do |table|
3
+ next if table == :schema_migrations
4
+ PGAdaptor.db.execute "TRUNCATE #{table};"
5
+ end
6
+ end
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_runtime_dependency "forecast_io", '~> 2.0.1'
22
22
  spec.add_runtime_dependency "sinatra"
23
- spec.add_runtime_dependency "mongo_adaptor"
23
+ spec.add_runtime_dependency "pg_adaptor"
24
24
 
25
25
  spec.add_development_dependency "bundler", "~> 1.3"
26
26
  spec.add_development_dependency "rake"
@@ -29,8 +29,4 @@ Gem::Specification.new do |spec|
29
29
  spec.add_development_dependency "rack-test"
30
30
  spec.add_development_dependency "vcr"
31
31
 
32
- spec.add_development_dependency "mongo"
33
- spec.add_development_dependency "bson"
34
- spec.add_development_dependency "bson_ext"
35
-
36
32
  end
@@ -13,11 +13,11 @@ module Forecast
13
13
  def self.configure
14
14
  yield configuration
15
15
  end
16
+
16
17
  def self.configuration
17
18
  @config ||= Configuration.new
18
19
  end
19
20
 
20
-
21
21
  def self.initialize!
22
22
  @app ||= App.new
23
23
  end
@@ -1,4 +1,4 @@
1
- require 'mongo-configure'
1
+ require 'pg_adaptor'
2
2
 
3
3
  module Forecast
4
4
  module IO
@@ -6,6 +6,7 @@ module Forecast
6
6
  class Configuration
7
7
 
8
8
  attr_writer :radius, :timeframe
9
+ attr_reader :db_uri, :db
9
10
 
10
11
  def radius
11
12
  @radius ||= 5
@@ -15,8 +16,10 @@ module Forecast
15
16
  @timeframe ||= 60
16
17
  end
17
18
 
18
- def mongo_uri uri
19
- Mongo::Configure.from_uri uri
19
+ def db_uri= value
20
+ @db_uri = value
21
+ @db = PGAdaptor.db = Sequel.connect(db_uri)
22
+ value
20
23
  end
21
24
 
22
25
  end
@@ -1,27 +1,28 @@
1
1
  module Forecast
2
2
  module IO
3
3
  module Cache
4
- class ForecastData < Struct.new(:position, :time, :wind_speed, :wind_bearing,
4
+ class ForecastData < Struct.new(:latitude, :longitude, :time, :wind_speed, :wind_bearing,
5
5
  :humidity, :pressure, :visibility, :temperature)
6
6
 
7
7
  def self.generate lat, lon, forecast
8
8
  new.tap do |data|
9
- data.position = [lon,lat].map(&:to_f)
10
- data.time = forecast['time']
11
- data.wind_speed = forecast['windSpeed']
12
- data.wind_bearing = forecast['windBearing']
13
- data.humidity = forecast['humidity']
14
- data.pressure = forecast['pressure']
15
- data.visibility = forecast['visibility']
16
- data.temperature = forecast['temperature']
9
+ data.latitude = lat.to_f
10
+ data.longitude = lon.to_f
11
+ data.time = forecast['time']
12
+ data.wind_speed = forecast['windSpeed']
13
+ data.wind_bearing = forecast['windBearing']
14
+ data.humidity = forecast['humidity']
15
+ data.pressure = forecast['pressure']
16
+ data.visibility = forecast['visibility']
17
+ data.temperature = forecast['temperature']
17
18
  end
18
19
  end
19
20
 
20
21
  def to_json
21
22
  {
22
23
  time: time,
23
- latitude: position[1],
24
- longitude: position[0],
24
+ latitude: latitude,
25
+ longitude: longitude,
25
26
  wind_speed: wind_speed,
26
27
  wind_bearing: wind_bearing,
27
28
  humidity: humidity,
@@ -9,7 +9,7 @@ module Forecast
9
9
  new(lat, lon, time, cache).forecasts.first
10
10
  end
11
11
 
12
- def initialize lat, lon, time, cache, api = ::Forecast::IO
12
+ def initialize lat, lon, time, cache, api = ::ForecastIO
13
13
  @lat, @lon, @time, @cache, @api = lat, lon, time, cache, api
14
14
  end
15
15
 
@@ -1,4 +1,4 @@
1
- require 'mongo_adaptor'
1
+ require 'pg_adaptor'
2
2
  require 'forecast_io/cache/forecast_data'
3
3
 
4
4
  module Forecast
@@ -8,19 +8,23 @@ module Forecast
8
8
 
9
9
  def initialize config = Cache.configuration
10
10
  @config = config
11
- @backend = MongoAdaptor.new('forecasts',ForecastData)
11
+ @backend = PGAdaptor.new('forecasts', ForecastData)
12
12
  end
13
13
 
14
14
  def fetch lat, lon, time
15
- @backend.fetch 'position' => { '$near' => [lon,lat], '$maxDistance' => radius },
16
- 'time' => { '$lt' => time+offset, '$gt' => time-offset }
17
- rescue Mongo::OperationFailure => error
18
- if index_error? error
19
- ensure_index!
20
- retry
21
- else
22
- raise
23
- end
15
+ @backend.fetch Sequel.lit(<<-SQL),
16
+ time >= :start AND time <= :end AND earth_box(
17
+ ll_to_earth(:lat, :lon), :radius
18
+ ) @> ll_to_earth(
19
+ latitude,
20
+ longitude
21
+ )
22
+ SQL
23
+ start: time - offset,
24
+ end: time + offset,
25
+ lat: lat,
26
+ lon: lon,
27
+ radius: radius
24
28
  end
25
29
 
26
30
  def store forecast
@@ -28,22 +32,17 @@ module Forecast
28
32
  forecast
29
33
  end
30
34
 
31
- private
32
- def radius
33
- @config.radius * (1/111.694)
34
- end
35
+ private
35
36
 
36
- def offset
37
- @config.timeframe*60
38
- end
37
+ def radius
38
+ # nm to m
39
+ @config.radius * 1852
40
+ end
39
41
 
40
- def index_error? error
41
- error.message =~ /can't find any special indices/ || error.message =~ /unable to find index/
42
- end
42
+ def offset
43
+ @config.timeframe * 60
44
+ end
43
45
 
44
- def ensure_index!
45
- MongoAdaptor.db.collection('forecasts').ensure_index position: Mongo::GEO2D
46
- end
47
46
  end
48
47
  end
49
48
  end
@@ -1,7 +1,7 @@
1
1
  module Forecast
2
2
  module IO
3
3
  module Cache
4
- VERSION = "1.0.0"
4
+ VERSION = "2.0.0"
5
5
  end
6
6
  end
7
7
  end
@@ -22,9 +22,11 @@ describe 'service configuration' do
22
22
  expect(config.timeframe).to eq 12
23
23
  end
24
24
 
25
- it 'loads mongo config urls' do
26
- expect(Mongo::Configure).to receive(:from_uri).with 'mongodb://example.com'
27
- config.mongo_uri 'mongodb://example.com'
25
+ it 'loads db from db_uri' do
26
+ config.db_uri = 'postgres://localhost/forecast-test'
27
+ expect(config.db).to be_a Sequel::Database
28
+ expect(config.db.opts[:database]).to eq 'forecast-test'
29
+ expect(PGAdaptor.db).to eq config.db
28
30
  end
29
31
 
30
32
  end
@@ -24,7 +24,8 @@ describe 'representing data from the api' do
24
24
  expect(forecast.time).to eq time
25
25
  end
26
26
  it "exposes position" do
27
- expect(forecast.position).to eq [-1.2, 1.1]
27
+ expect(forecast.latitude).to eq 1.1
28
+ expect(forecast.longitude).to eq -1.2
28
29
  end
29
30
  it "exposes wind_speed" do
30
31
  expect(forecast.wind_speed).to eq wind_speed
@@ -1,14 +1,14 @@
1
- require 'support/mongo'
1
+ require 'support/pg'
2
2
  require 'forecast_io/cache/store'
3
3
 
4
- describe database: true do
4
+ RSpec.describe 'Forecast::IO::Cache::Store', database: true do
5
5
  let(:config) { double radius: 1, timeframe: 3 }
6
6
  let(:store) { Forecast::IO::Cache::Store.new config }
7
7
  let(:data) { Forecast::IO::Cache::ForecastData }
8
8
 
9
9
  describe 'retrieving forecasts from the store' do
10
- let(:forecast_1) { data.new [151.23775,-33.858264], 1 }
11
- let(:forecast_2) { data.new [151.23675,-33.858664], 3 }
10
+ let(:forecast_1) { data.new -33.858264, 151.23775, 1, 0, 0, 0, 0, 0, 0 }
11
+ let(:forecast_2) { data.new -33.858664, 151.23675, 3, 0, 0, 0, 0, 0, 0 }
12
12
 
13
13
  before do
14
14
  store.store forecast_1
@@ -27,11 +27,10 @@ describe database: true do
27
27
  end
28
28
 
29
29
  describe 'inserting forecasts into the store' do
30
- let(:forecast) { data.new [151.23775,-33.858264], 1 }
30
+ let(:forecast) { data.new 151.23775, -33.858264, 1, 0, 0, 0, 0, 0, 0 }
31
31
 
32
32
  it 'returns the forecast' do
33
33
  expect(store.store forecast).to eq forecast
34
34
  end
35
35
  end
36
-
37
36
  end
@@ -0,0 +1,13 @@
1
+ require 'pg_adaptor'
2
+ PGAdaptor.db = Sequel.connect 'postgres://localhost/forecast-test'
3
+
4
+ RSpec.configure do |config|
5
+
6
+ config.after :example, database: true do
7
+ PGAdaptor.db.tables.each do |table|
8
+ next if table == :schema_migrations
9
+ PGAdaptor.db.execute "TRUNCATE #{table};"
10
+ end
11
+ end
12
+
13
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forecast_io-cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Rowe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-21 00:00:00.000000000 Z
11
+ date: 2016-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: forecast_io
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: mongo_adaptor
42
+ name: pg_adaptor
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -136,48 +136,6 @@ dependencies:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
- - !ruby/object:Gem::Dependency
140
- name: mongo
141
- requirement: !ruby/object:Gem::Requirement
142
- requirements:
143
- - - ">="
144
- - !ruby/object:Gem::Version
145
- version: '0'
146
- type: :development
147
- prerelease: false
148
- version_requirements: !ruby/object:Gem::Requirement
149
- requirements:
150
- - - ">="
151
- - !ruby/object:Gem::Version
152
- version: '0'
153
- - !ruby/object:Gem::Dependency
154
- name: bson
155
- requirement: !ruby/object:Gem::Requirement
156
- requirements:
157
- - - ">="
158
- - !ruby/object:Gem::Version
159
- version: '0'
160
- type: :development
161
- prerelease: false
162
- version_requirements: !ruby/object:Gem::Requirement
163
- requirements:
164
- - - ">="
165
- - !ruby/object:Gem::Version
166
- version: '0'
167
- - !ruby/object:Gem::Dependency
168
- name: bson_ext
169
- requirement: !ruby/object:Gem::Requirement
170
- requirements:
171
- - - ">="
172
- - !ruby/object:Gem::Version
173
- version: '0'
174
- type: :development
175
- prerelease: false
176
- version_requirements: !ruby/object:Gem::Requirement
177
- requirements:
178
- - - ">="
179
- - !ruby/object:Gem::Version
180
- version: '0'
181
139
  description: Caching layer for forecast_io
182
140
  email:
183
141
  - hello@jonrowe.co.uk
@@ -199,8 +157,8 @@ files:
199
157
  - features/support/env.rb
200
158
  - features/support/expect.rb
201
159
  - features/support/fixtures.rb
202
- - features/support/mongo.rb
203
160
  - features/support/parse.rb
161
+ - features/support/pg.rb
204
162
  - features/support/time.rb
205
163
  - features/support/vcr.rb
206
164
  - forecast_io-cache.gemspec
@@ -219,7 +177,7 @@ files:
219
177
  - spec/forecast_io/cache/generate_spec.rb
220
178
  - spec/forecast_io/cache/store_spec.rb
221
179
  - spec/forecast_io/cache_spec.rb
222
- - spec/support/mongo.rb
180
+ - spec/support/pg.rb
223
181
  homepage: https://github.com/Tacktical/forecast_io-cache
224
182
  licenses:
225
183
  - MIT
@@ -257,8 +215,8 @@ test_files:
257
215
  - features/support/env.rb
258
216
  - features/support/expect.rb
259
217
  - features/support/fixtures.rb
260
- - features/support/mongo.rb
261
218
  - features/support/parse.rb
219
+ - features/support/pg.rb
262
220
  - features/support/time.rb
263
221
  - features/support/vcr.rb
264
222
  - spec/forecast_io/cache/configuration_spec.rb
@@ -267,4 +225,4 @@ test_files:
267
225
  - spec/forecast_io/cache/generate_spec.rb
268
226
  - spec/forecast_io/cache/store_spec.rb
269
227
  - spec/forecast_io/cache_spec.rb
270
- - spec/support/mongo.rb
228
+ - spec/support/pg.rb
@@ -1,5 +0,0 @@
1
- require 'mongo-configure'
2
-
3
- After do
4
- Mongo::Configure.current.load.collections.select { |c| c.name !~ /^system\./ }.each &:remove
5
- end
@@ -1,11 +0,0 @@
1
- require 'mongo-configure'
2
-
3
- Mongo::Configure.from_database 'forecast-test-spec'
4
-
5
- RSpec.configure do |config|
6
-
7
- config.after :each, database: true do
8
- Mongo::Configure.current.load.collections.select { |c| c.name !~ /^system\./ }.each &:remove
9
- end
10
-
11
- end