Skip to content

Commit

Permalink
chore: Add tests (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-developer authored Apr 30, 2024
1 parent 34af380 commit 0fb4c78
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ Style/StringLiteralsInInterpolation:

Layout/LineLength:
Max: 120

Metrics/BlockLength:
AllowedMethods: ['describe', 'context']
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ gem "rake", "~> 13.0"
gem "rspec", "~> 3.0"

gem "rubocop", "~> 1.21"

gem 'pry', '~> 0.14.2'
70 changes: 70 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
PATH
remote: .
specs:
open_weather_aemet (0.1.0)
rake (~> 13.0)
rspec (~> 3.0)
rubocop (~> 1.21)

GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
coderay (1.1.3)
diff-lcs (1.5.1)
json (2.7.2)
language_server-protocol (3.17.0.3)
method_source (1.1.0)
parallel (1.24.0)
parser (3.3.1.0)
ast (~> 2.4.1)
racc
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
racc (1.7.3)
rainbow (3.1.1)
rake (13.2.1)
regexp_parser (2.9.0)
rexml (3.2.6)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
rubocop (1.63.4)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.3)
parser (>= 3.3.1.0)
ruby-progressbar (1.13.0)
unicode-display_width (2.5.0)

PLATFORMS
arm64-darwin-21

DEPENDENCIES
open_weather_aemet!
pry (~> 0.14.2)
rake (~> 13.0)
rspec (~> 3.0)
rubocop (~> 1.21)

BUNDLED WITH
2.3.7
15 changes: 8 additions & 7 deletions lib/api_aemet/connector.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
require 'uri'
require 'net/http'

require "uri"
require "net/http"

module ApiAemet
class Connector
Expand All @@ -14,18 +15,18 @@ def initialize(api_url:, api_key:)

def call
url = URI("#{api_url}/?api_key=#{api_key}")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Get.new(url)
request["cache-control"] = 'no-cache'
request["cache-control"] = "no-cache"

http.request(request)
end

private
private

attr_reader :api_url, :api_key
end
Expand Down
13 changes: 7 additions & 6 deletions lib/api_aemet/data_response.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true
require 'uri'
require 'net/http'

require "uri"
require "net/http"
require "api_aemet/connector"
require 'json'
require "json"

module ApiAemet
class DataResponse
Expand All @@ -14,15 +15,15 @@ def initialize(api_url:, api_key:)
def call
aemet_response = ApiAemet::Connector.new(api_url: api_url, api_key: api_key).call
data = JSON.parse(aemet_response.read_body)
resource_url = data['datos']
resource_url = data["datos"]

return {} unless resource_url

response = Net::HTTP.get(URI(resource_url))
JSON.parse(response)
end

private
private

attr_reader :api_url, :api_key
end
Expand Down
8 changes: 4 additions & 4 deletions lib/open_weather_aemet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

module OpenWeatherAemet
class Error < StandardError; end

class Weather
def self.forecast_by_api_url(api_url, api_key)
return 'Invalid inputs' unless api_url || api_key
return "Invalid inputs" unless api_url || api_key

ApiAemet::DataResponse.new(api_url: api_url, api_key: api_key).call
end
Expand All @@ -18,11 +18,11 @@ def self.forecast_by_city(city_code, api_key)
api_url = "https://opendata.aemet.es/opendata/api/prediccion/especifica/municipio/diaria/#{city_code}"
response = ApiAemet::DataResponse.new(api_url: api_url, api_key: api_key).call

return 'No data' unless response
return "Invalid data" unless response.is_a?(Hash)

three_forecast_days = response.first["prediccion"]["dia"][0..2]
forecast_result = []
three_forecast_days.each do|forecast_day|
three_forecast_days.each do |forecast_day|
forecast_result << {
date: forecast_day["fecha"],
max_temperature: forecast_day["temperatura"]["maxima"],
Expand Down
9 changes: 6 additions & 3 deletions lib/utilities/sky_state.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Utilities
class SkyState
attr_reader :state
Expand All @@ -16,11 +18,12 @@ def format_state
return state_00_24["value"] unless state_00_24["value"]&.strip&.empty?
return state_00_12["value"] unless state_00_12["value"]&.strip&.empty?
return state_12_24["value"] unless state_12_24["value"]&.strip&.empty?
return DEFAULT_SKY_STATE

DEFAULT_SKY_STATE
end

def find
result = list_sky_states.find { |key, val| key == format_state.to_s }&.last
result = list_sky_states.find { |key, _val| key == format_state.to_s }&.last
result.nil? ? "not_found" : result
end

Expand Down Expand Up @@ -82,4 +85,4 @@ def list_sky_states
}
end
end
end
end
Binary file modified open_weather_aemet-0.1.0.gem
Binary file not shown.
38 changes: 38 additions & 0 deletions spec/open_weather_aemet/weather_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require "open_weather_aemet"

RSpec.describe OpenWeatherAemet::Weather do
describe ".forecast_by_api_url" do
context "with valid API URL and API key" do
let(:api_url) { "https://api.example.com" }
let(:api_key) { "API_KEY" }

it "calls the DataResponse service" do
expect_any_instance_of(ApiAemet::DataResponse).to receive(:call).and_return("Response data")
response = described_class.forecast_by_api_url(api_url, api_key)
expect(response).to eq("Response data")
end
end

context "with invalid inputs" do
it 'returns "Invalid inputs"' do
response = described_class.forecast_by_api_url(nil, nil)
expect(response).to eq("Invalid inputs")
end
end
end

describe ".forecast_by_city" do
context "when city_code or API key is not valid for Aemet API" do
let(:city_code) { 5890 }
let(:api_key) { "FAKE_API_KEY" }

it "calls the DataResponse service" do
expect_any_instance_of(ApiAemet::DataResponse).to receive(:call).and_return("Response data")
response = described_class.forecast_by_city(city_code, api_key)
expect(response).to eq("Invalid data")
end
end
end
end
11 changes: 0 additions & 11 deletions spec/open_weather_aemet_spec.rb

This file was deleted.

0 comments on commit 0fb4c78

Please sign in to comment.