Skip to content

Commit

Permalink
Move off Dark Sky and onto PirateWeather 🏴‍☠️
Browse files Browse the repository at this point in the history
Fixes #57
  • Loading branch information
aaronbrethorst committed Jan 16, 2023
1 parent 945ca83 commit 79cbc3a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/weather_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def show

begin
@forecast = Rails.cache.fetch("regions/#{@region.to_param}/weather", expires_in: 30.minutes, race_condition_ttl: 30.seconds) do
dark_sky.forecast(DarkSky.geohash_from(@region))
dark_sky.forecast(@region)
end
respond_to do |format|
format.json
Expand Down
15 changes: 5 additions & 10 deletions app/lib/dark_sky.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ class DarkSky
attr_accessor :api_key

def self.client
DarkSky.new(ENV['DARKSKY_API_KEY'])
DarkSky.new(Rails.application.credentials.dig(:pirate_weather_api_key))
end

def initialize(api_key)
self.api_key = api_key
end

def forecast(geohash)
def forecast(region)
geohash = GeoHash.encode(region.region_center.lat, region.region_center.lon, 4)
lat,lon = geohash_to_lat_lon(geohash)
url = build_forecast_url(lat, lon)
response = RestClient.get(url) rescue nil
Expand All @@ -23,23 +24,17 @@ def forecast(geohash)
end
end

###### Public Helpers

def self.geohash_from(region)
GeoHash.encode(region.region_center.lat, region.region_center.lon, 4)
end

#####################

private

DARK_SKY_BASE_URL = 'https://api.darksky.net/forecast'.freeze
DARK_SKY_BASE_URL = 'https://api.pirateweather.net'.freeze

def geohash_to_lat_lon(geohash)
GeoHash.decode(geohash).first
end

def build_forecast_url(lat, lon)
"#{DARK_SKY_BASE_URL}/#{self.api_key}/#{lat},#{lon}?exclude=minutely,daily"
"#{DARK_SKY_BASE_URL}/forecast/#{self.api_key}/#{lat},#{lon}?exclude=minutely,daily"
end
end
2 changes: 1 addition & 1 deletion config/credentials.yml.enc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
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