Skip to content

Commit

Permalink
Version 0.1.4 (#5)
Browse files Browse the repository at this point in the history
- Add rubocop rake task
- Run Github actions for ruby 3.2
- Change cache key format to `weather:<lat>:<lon>:<time}>` for better compatibility with redis
- Make `OpenWeatherClient::Caching#cache_key` public
  • Loading branch information
Luckeu committed Feb 1, 2023
1 parent 7d8ff30 commit c0b3ebd
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 43 deletions.
14 changes: 5 additions & 9 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,17 @@ on:

jobs:
test:

runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['3.0']
ruby-version: ['3.0', '3.2']

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up Ruby
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
# change this to (see https://github.com/ruby/setup-ruby#versioning):
# uses: ruby/setup-ruby@v1
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- name: Run tests
bundler-cache: true
- name: Run the default task
run: bundle exec rake
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ AllCops:
Metrics/BlockLength:
Exclude:
- spec/**/*

Layout/LineLength:
Exclude:
- spec/**/*
10 changes: 9 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# Open Weather Client Changelog
# OpenWeatherClient Changelog

## [Unreleased]

## 0.1.4
- Add rubocop rake task
- Run Github actions for ruby 3.2
- Change cache key format to `weather:<lat>:<lon>:<time}>` for better compatibility with redis
- Make `OpenWeatherClient::Caching#cache_key` public

## 0.1.3
Re-release of 0.1.2 since this version was released from an incorrect branch.
Expand Down
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@

source 'https://rubygems.org'

git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

# Specify your gem's dependencies in open_weather_client.gemspec
gemspec
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
[![Gem Version](https://badge.fury.io/rb/open_weather_client.svg)](https://badge.fury.io/rb/open_weather_client)
![RSpec](https://github.com/qurasoft/open_weather_client/actions/workflows/ruby.yml/badge.svg)

Welcome to Open Weather Client. This gem allows you to easily request weather information from OpenWeatherMap.
Welcome to Open Weather Client.
This gem allows you to easily request weather information from OpenWeatherMap.
It integrates in your rails project, when you are using bundler or even in plain ruby projects.

## Installation
Expand Down Expand Up @@ -69,12 +70,16 @@ Whether requests are only performed when the requested time is within the last h
If caching is enabled, requests with a time older than one hour might still be answered with a response.

```ruby
# OpenWeatherClient initializer
OpenWeatherClient.configure do |config|
config.caching = OpenWeatherClient::Caching::Memory
config.max_memory_entries = 100 # Maximum number of entries in memory cache
end
```

### Cache key
The cache key for a given latitute, longitude and time is calculated by `OpenWeatherClient::Caching#cache_key(lat, lon, time)`.

#### Memory Caching
`OpenWeatherClient` supports simple in memory caching.
A hash is used to store and look up the cached responses.
Expand All @@ -93,6 +98,7 @@ The quantization receives latitude and longitude.
The result is coerced back into latitude and longitude.

```ruby
# OpenWeatherClient initializer
OpenWeatherClient.configure do |config|
config.spatial_quantization = proc { |lat, lon| [lat.round(2), lon.round(2)] }
end
Expand Down
6 changes: 5 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task default: :spec
require 'rubocop/rake_task'

RuboCop::RakeTask.new

task default: %i[spec rubocop]
10 changes: 5 additions & 5 deletions lib/open_weather_client/caching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ def store(data:, lat:, lon:, time:)
data
end

private

##
# Calculate the key for storage in the cache
#
# @param lat[Float] latitude of the requests location
# @param lon[Float] longitude of the requests location
# @param time[Time] time of the request
def cache_key(lat, lon, time)
"#{lat}_#{lon}_#{time.strftime('%Y-%m-%dT%H')}"
"weather:#{lat}:#{lon}:#{time.strftime('%Y-%m-%dT%H')}"
end

private

##
# Read an entry out of the memory cache
#
Expand All @@ -71,11 +71,11 @@ def caching_get(key)
#
# Evicts the entry with the least recent access if the memory cache is full
#
# @param key[String] key into the cache. Is stored at the end of the key registry
# @param _key[String] key into the cache. Is stored at the end of the key registry
# @param data[Hash] data to be stored, must be able to be formatted and parsed as text
#
# @return [Hash] the input data
def caching_store(key, data)
def caching_store(_key, data)
data
end

Expand Down
44 changes: 27 additions & 17 deletions lib/open_weather_client/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,39 @@ class Request
def self.get(lat:, lon:, time: Time.now)
raise OpenWeatherClient::NotCurrentError if time < Time.now - 1 * 60 * 60

connection = Faraday.new(
begin
response = connection(lat, lon).get('2.5/weather')
OpenWeatherClient.cache.store(lat: lat, lon: lon, data: response.body, time: time)
rescue Faraday::UnauthorizedError
raise OpenWeatherClient::AuthenticationError
end
end

def self.connection(lat, lon)
Faraday.new(
url: OpenWeatherClient.configuration.url,
params: {
appid: OpenWeatherClient.configuration.appid,
lat: lat,
lon: lon,
lang: OpenWeatherClient.configuration.lang,
units: OpenWeatherClient.configuration.units
},
headers: {
'User-Agent': OpenWeatherClient.configuration.user_agent
}
params: params(lat, lon),
headers: headers
) do |f|
f.response :raise_error
f.response :json
end
end

begin
response = connection.get('2.5/weather')
OpenWeatherClient.cache.store(lat: lat, lon: lon, data: response.body, time: time)
rescue Faraday::UnauthorizedError
raise OpenWeatherClient::AuthenticationError
end
def self.headers
{
'User-Agent': OpenWeatherClient.configuration.user_agent
}
end

def self.params(lat, lon)
{
appid: OpenWeatherClient.configuration.appid,
lat: lat,
lon: lon,
lang: OpenWeatherClient.configuration.lang,
units: OpenWeatherClient.configuration.units
}
end
end
end
2 changes: 1 addition & 1 deletion lib/open_weather_client/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
module OpenWeatherClient
##
# Version of OpenWeatherClient
VERSION = '0.1.3'
VERSION = '0.1.4'
end
9 changes: 3 additions & 6 deletions open_weather_client.gemspec
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# frozen_string_literal: true

lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'open_weather_client/version'
require_relative 'lib/open_weather_client/version'

Gem::Specification.new do |spec|
spec.name = 'open_weather_client'
spec.version = OpenWeatherClient::VERSION
spec.required_ruby_version = '>= 3.0'
spec.required_rubygems_version = '>= 2'
spec.required_ruby_version = '>= 3.0.0'
spec.authors = ['Lucas Keune']
spec.email = ['[email protected]']

Expand All @@ -23,7 +20,7 @@ Gem::Specification.new do |spec|

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
spec.files = Dir.chdir(__dir__) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
end
spec.bindir = 'bin'
Expand Down
6 changes: 6 additions & 0 deletions spec/open_weather_client/caching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@
end
end
end

describe '#cache_key' do
it 'formats lat, lon and time' do
expect(subject.cache_key(0.5, 1.0, Time.new(2023, 2, 1, 9, 43))).to eq 'weather:0.5:1.0:2023-02-01T09'
end
end
end

0 comments on commit c0b3ebd

Please sign in to comment.