weather_gov_api 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ef2d712f1eb48bd665d2026b525fa1350b6df2074d708404ab909c6e7cc07157
4
- data.tar.gz: 85c59f8fc33f121b29ef6a8f0470afd9ac80e794f1502aa97ab317d8e702a6a2
3
+ metadata.gz: 1774def1c5d0e09a3fb74a4d66b0f2c63c6c18fd97c02871690db6bfdac5c38c
4
+ data.tar.gz: 35336afbc96f2517d96b993de9e73501b427843cc649b0b1217d621c7b9be8dc
5
5
  SHA512:
6
- metadata.gz: e0bc42ef1eb84d7071f11c5125fe97fa13b140a060a312a464f7fdeeb79f40bc0a094804d73a73182698a03cbe5f7b1717c2981325854875ec8a0bbcec1d3df7
7
- data.tar.gz: 466cf682be9fa48f9cf4c947472a2cf2a194769632ba083bdbd3d458344df62d0a385a19932d17c74aa1a6fa6665bca584c71fac75603090717060a59e4bdd5e
6
+ metadata.gz: 0b54fd7e6fb5572e2ae2341f8a6e8049174c3961cda8f4a01e18541f6dfc63cca2e91c4e96fc1a98788f490fcc23ebac33208accdfb55f6f1faa57d07c05f0e4
7
+ data.tar.gz: 758cea3008922169f96395ba0540abad5154b224ffbf0b1a4435068db79a9b68031f3e733aa1e2c05189baf7950a4a90556345d9652b819b4b880bded8e84605
data/.rubocop.yml CHANGED
@@ -1,4 +1,10 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ require:
4
+ - rubocop-rspec
5
+
1
6
  AllCops:
7
+ NewCops: enable
2
8
  TargetRubyVersion: 3.1
3
9
 
4
10
  Style/StringLiterals:
@@ -22,3 +28,14 @@ Metrics/BlockLength:
22
28
  Exclude:
23
29
  - "spec/**/*"
24
30
  - "*.gemspec"
31
+
32
+ Metrics/ParameterLists:
33
+ CountKeywordArgs:
34
+ Enabled: false
35
+
36
+ # Allow multiple exceptions
37
+ RSpec/MultipleExpectations:
38
+ Max: 4
39
+
40
+ RSpec/ExampleLength:
41
+ Max: 10 # Adjust this value as needed
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,8 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2025-02-22 02:13:57 UTC using RuboCop version 1.72.2.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.5
@@ -0,0 +1,6 @@
1
+ {
2
+ "recommendations": [
3
+ "shopify.ruby-lsp",
4
+ "rubocop.vscode-rubocop"
5
+ ]
6
+ }
data/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.3.0] - 2025-02-28
4
+
5
+ ### Added
6
+ - Updated version handling adding WeatherGovApi::ApiError
7
+ - added .ruby-version
8
+ - added .vsc recommend plugins
9
+
10
+ ### Fixed
11
+ - tackled rubocop_todo list
12
+ - fixed CodeQL rb/request-forgery error by ensuring same host
13
+
14
+ ## [0.2.1] - 2025-02-21
15
+
16
+ ### Added
17
+ - rubocop_todo
18
+ - simplecov and rubycritic
19
+ - updated CircleCI to require 90% overall and 80% on a file for test coverage
20
+
3
21
  ## [0.2.0] - 2025-02-19
4
22
 
5
23
  ### Added
@@ -9,7 +27,6 @@
9
27
  ## [0.1.0] - Initial Release
10
28
 
11
29
  ### Added
12
- - Basic client implementation
13
30
  - Basic client implementation for weather.gov API
14
31
  - `points` method for fetching weather data by coordinates
15
32
  - Error handling for:
data/README.md CHANGED
@@ -44,12 +44,39 @@ Fetches the current weather conditions from the closest observation station.
44
44
 
45
45
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
46
46
 
47
+ ### Building and Releasing
48
+
49
+ 1. Update the version number in `version.rb`
50
+ 2. Build the gem:
51
+ ```bash
52
+ gem build weather_gov_api.gemspec
53
+ ```
54
+ 3. Test the built gem locally:
55
+ ```bash
56
+ gem install ./weather_gov_api-X.X.X.gem
57
+ ```
58
+ 4. Release to RubyGems:
59
+ ```bash
60
+ gem push weather_gov_api-X.X.X.gem
61
+ ```
62
+
47
63
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
48
64
 
49
65
  ## Contributing
50
66
 
51
67
  Bug reports and pull requests are welcome on GitHub at https://github.com/JeffreyMPrice/weather_gov_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/JeffreyMPrice/weather_gov_api/blob/main/CODE_OF_CONDUCT.md).
52
68
 
69
+ ## TODO
70
+
71
+ - [ ] Implement forecast retrieval functionality
72
+ - [ ] Add validation to limit latitude and longitude to 4 decimal places (weather.gov API requirement)
73
+ - [ ] Improve error handling to properly surface weather.gov API error messages
74
+ - [ ] Implement rate limiting strategies:
75
+ - [ ] Local rate limiting
76
+ - [ ] Distributed rate limiting (Redis/DB-based)
77
+ - [ ] Add caching support for API responses
78
+ - [ ] Automate CHANGELOG.md updates during release process
79
+
53
80
  ## License
54
81
 
55
82
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WeatherGovApi
4
+ # Custom error class to handle errors from the Weather.gov API
5
+ class ApiError < StandardError
6
+ attr_reader :type, :title, :status, :detail, :instance, :correlation_id, :parameter_errors
7
+
8
+ # rubocop:disable Metrics/ParameterLists
9
+ def initialize(type: nil, title: nil, status: nil, detail: nil, instance: nil, correlation_id: nil, message: nil,
10
+ parameter_errors: nil)
11
+ @type = type
12
+ @title = title
13
+ @status = status
14
+ @detail = detail
15
+ @instance = instance
16
+ @correlation_id = correlation_id
17
+ @parameter_errors = parameter_errors
18
+ @message = message
19
+ super(message || title || detail || "API request failed")
20
+ end
21
+ # rubocop:enable Metrics/ParameterLists
22
+
23
+ # rubocop:disable Metrics/CyclomaticComplexity
24
+ # rubocop:disable Metrics/PerceivedComplexity
25
+ def to_s
26
+ return super unless super.empty?
27
+
28
+ parts = []
29
+ parts << "Type: #{@type}" if @type
30
+ parts << "Title: #{@title}" if @title
31
+ parts << "Status: #{@status}" if @status
32
+ parts << "Detail: #{@detail}" if @detail
33
+ parts << "Instance: #{@instance}" if @instance
34
+ parts << "CorrelationID: #{@correlation_id}" if @correlation_id
35
+ parts << "ParameterErros: #{@parameter_errors}" if @parameter_errors
36
+ parts.join(", ")
37
+ end
38
+ # rubocop:enable Metrics/PerceivedComplexity
39
+ # rubocop:enable Metrics/CyclomaticComplexity
40
+ end
41
+ end
@@ -11,44 +11,53 @@ module WeatherGovApi
11
11
  end
12
12
 
13
13
  def points(latitude:, longitude:)
14
- validate_coordinates(latitude, longitude)
15
14
  response = connection.get("/points/#{latitude},#{longitude}")
15
+ raise_api_error(response) unless response.success?
16
16
  Response.new(response)
17
17
  rescue Faraday::Error => e
18
- raise Error, "API request failed: #{e.message}"
18
+ raise WeatherGovApi::ApiError.new(message: "API request failed: #{e.message}")
19
19
  end
20
20
 
21
+ # rubocop:disable Metrics/MethodLength
21
22
  def observation_stations(latitude:, longitude:)
22
23
  points_response = points(latitude: latitude, longitude: longitude)
23
24
  stations_url = points_response.data.dig("properties", "observationStations")
24
- raise Error, "No observation stations URL found in points response" unless stations_url
25
+ unless stations_url
26
+ raise WeatherGovApi::ApiError.new(message: "No observation stations URL found in points response")
27
+ end
28
+
29
+ stations_path = observation_stations_path(stations_url)
25
30
 
26
- response = connection.get(stations_url.sub(BASE_URL, ""))
31
+ response = connection.get(stations_path)
32
+ raise_api_error(response) unless response.success?
27
33
  Response.new(response)
28
34
  rescue Faraday::Error => e
29
- raise Error, "API request failed: #{e.message}"
35
+ raise WeatherGovApi::ApiError.new(message: "API request failed: #{e.message}")
30
36
  end
37
+ # rubocop:enable Metrics/MethodLength
31
38
 
32
39
  def current_weather(latitude:, longitude:)
33
40
  stations_response = observation_stations(latitude: latitude, longitude: longitude)
34
41
  station = stations_response.data.dig("features", 0)
35
- raise Error, "No observation stations found" unless station
42
+ raise WeatherGovApi::ApiError.new(message: "No observation stations found") unless station
36
43
 
37
44
  station_id = station.dig("properties", "stationIdentifier")
38
45
  response = connection.get("/stations/#{station_id}/observations/latest")
46
+ raise_api_error(response) unless response.success?
39
47
  Response.new(response)
40
48
  rescue Faraday::Error => e
41
- raise Error, "API request failed: #{e.message}"
49
+ raise WeatherGovApi::ApiError.new(message: "API request failed: #{e.message}")
42
50
  end
43
51
 
44
52
  private
45
53
 
46
- def validate_coordinates(latitude, longitude)
47
- raise ArgumentError, "Invalid latitude: must be between -90 and 90" unless latitude.between?(-90, 90)
48
-
49
- return if longitude.between?(-180, 180)
54
+ def observation_stations_path(url)
55
+ uri = URI.parse(url)
56
+ unless uri.host == URI.parse(BASE_URL).host
57
+ raise WeatherGovApi::ApiError.new(message: "Invalid observation stations URL: #{url}")
58
+ end
50
59
 
51
- raise ArgumentError, "Invalid longitude: must be between -180 and 180"
60
+ uri.path
52
61
  end
53
62
 
54
63
  def connection
@@ -58,5 +67,26 @@ module WeatherGovApi
58
67
  faraday.adapter Faraday.default_adapter
59
68
  end
60
69
  end
70
+
71
+ # rubocop:disable Metrics/MethodLength
72
+ def raise_api_error(response)
73
+ return if response.success?
74
+
75
+ begin
76
+ error_data = JSON.parse(response.body)
77
+ rescue JSON::ParserError
78
+ raise WeatherGovApi::ApiError.new(message: "API request failed with status #{response.status}\
79
+ but could not parse error response.")
80
+ end
81
+
82
+ raise WeatherGovApi::ApiError.new(
83
+ type: error_data["type"],
84
+ title: error_data["title"],
85
+ status: error_data["status"],
86
+ detail: error_data["detail"],
87
+ instance: error_data["instance"]
88
+ )
89
+ end
90
+ # rubocop:enable Metrics/MethodLength
61
91
  end
62
92
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WeatherGovApi
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "weather_gov_api/version"
4
+ require_relative "weather_gov_api/api_errors"
4
5
  require_relative "weather_gov_api/response"
5
6
  require_relative "weather_gov_api/client"
6
7
 
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weather_gov_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - JeffreyMPrice
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2025-02-19 00:00:00.000000000 Z
10
+ date: 2025-02-28 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: faraday
@@ -24,48 +23,6 @@ dependencies:
24
23
  - - "~>"
25
24
  - !ruby/object:Gem::Version
26
25
  version: '2.0'
27
- - !ruby/object:Gem::Dependency
28
- name: rspec
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '3.0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '3.0'
41
- - !ruby/object:Gem::Dependency
42
- name: vcr
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '6.0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '6.0'
55
- - !ruby/object:Gem::Dependency
56
- name: webmock
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '3.0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '3.0'
69
26
  description: |-
70
27
  A Ruby gem for accessing weather data from the National Weather Service (weather.gov) API.
71
28
  Provides a simple interface to fetch weather data for US locations.
@@ -77,17 +34,20 @@ extra_rdoc_files: []
77
34
  files:
78
35
  - ".rspec"
79
36
  - ".rubocop.yml"
37
+ - ".rubocop_todo.yml"
38
+ - ".ruby-version"
39
+ - ".vscode/extensions.json"
80
40
  - CHANGELOG.md
81
41
  - CODE_OF_CONDUCT.md
82
42
  - LICENSE.txt
83
43
  - README.md
84
44
  - Rakefile
85
45
  - lib/weather_gov_api.rb
46
+ - lib/weather_gov_api/api_errors.rb
86
47
  - lib/weather_gov_api/client.rb
87
48
  - lib/weather_gov_api/response.rb
88
49
  - lib/weather_gov_api/version.rb
89
50
  - sig/weather_gov_api.rbs
90
- - weather_gov_api-0.1.0.gem
91
51
  homepage: https://github.com/JeffreyMPrice/weather_gov_api
92
52
  licenses:
93
53
  - MIT
@@ -96,7 +56,7 @@ metadata:
96
56
  homepage_uri: https://github.com/JeffreyMPrice/weather_gov_api
97
57
  source_code_uri: https://github.com/JeffreyMPrice/weather_gov_api
98
58
  changelog_uri: https://github.com/JeffreyMPrice/weather_gov_api/blob/main/CHANGELOG.md
99
- post_install_message:
59
+ rubygems_mfa_required: 'true'
100
60
  rdoc_options: []
101
61
  require_paths:
102
62
  - lib
@@ -111,8 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
71
  - !ruby/object:Gem::Version
112
72
  version: '0'
113
73
  requirements: []
114
- rubygems_version: 3.5.16
115
- signing_key:
74
+ rubygems_version: 3.6.5
116
75
  specification_version: 4
117
76
  summary: Ruby wrapper for the Weather.gov API
118
77
  test_files: []
Binary file