weather_gov_api 0.2.1 → 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: 9a3357a492f9e0fefc3414c9f9e8cd54d4fbf1bd24caff3741021af46948b9a6
4
- data.tar.gz: 9f5f503e4db23945aa94eb18dfac9d24f1bb5fbf11d72f2040201c5db7cb004e
3
+ metadata.gz: 1774def1c5d0e09a3fb74a4d66b0f2c63c6c18fd97c02871690db6bfdac5c38c
4
+ data.tar.gz: 35336afbc96f2517d96b993de9e73501b427843cc649b0b1217d621c7b9be8dc
5
5
  SHA512:
6
- metadata.gz: fcbe8dc202094f73f1693806a6356644b7e68b37a5c5f378b3e129377bae89d883dbe5905768db8124f74c15a83ea4d4d55b50569dc508fa731f9d4ab246b0ce
7
- data.tar.gz: d6f8cbbb7ccb653fb09d0e05a191b306ed69c650346f05a5b7c1a5baa0000a0ac29c7afa184b10918430f1e1b5f625e9cc6d05c63c4d4427b7142df07c4c2937
6
+ metadata.gz: 0b54fd7e6fb5572e2ae2341f8a6e8049174c3961cda8f4a01e18541f6dfc63cca2e91c4e96fc1a98788f490fcc23ebac33208accdfb55f6f1faa57d07c05f0e4
7
+ data.tar.gz: 758cea3008922169f96395ba0540abad5154b224ffbf0b1a4435068db79a9b68031f3e733aa1e2c05189baf7950a4a90556345d9652b819b4b880bded8e84605
data/.rubocop.yml CHANGED
@@ -1,30 +1,41 @@
1
- inherit_from: .rubocop_todo.yml
2
-
3
- require:
4
- - rubocop-rspec
5
-
6
- AllCops:
7
- NewCops: enable
8
- TargetRubyVersion: 3.1
9
-
10
- Style/StringLiterals:
11
- EnforcedStyle: double_quotes
12
-
13
- Style/StringLiteralsInInterpolation:
14
- EnforcedStyle: double_quotes
15
-
16
- Layout/EndOfLine:
17
- EnforcedStyle: lf
18
-
19
- # Disable documentation requirements for specs
20
- Style/Documentation:
21
- Enabled: true
22
- Exclude:
23
- - "spec/**/*"
24
-
25
- # Allow longer blocks in specs
26
- Metrics/BlockLength:
27
- Enabled: true
28
- Exclude:
29
- - "spec/**/*"
30
- - "*.gemspec"
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ require:
4
+ - rubocop-rspec
5
+
6
+ AllCops:
7
+ NewCops: enable
8
+ TargetRubyVersion: 3.1
9
+
10
+ Style/StringLiterals:
11
+ EnforcedStyle: double_quotes
12
+
13
+ Style/StringLiteralsInInterpolation:
14
+ EnforcedStyle: double_quotes
15
+
16
+ Layout/EndOfLine:
17
+ EnforcedStyle: lf
18
+
19
+ # Disable documentation requirements for specs
20
+ Style/Documentation:
21
+ Enabled: true
22
+ Exclude:
23
+ - "spec/**/*"
24
+
25
+ # Allow longer blocks in specs
26
+ Metrics/BlockLength:
27
+ Enabled: true
28
+ Exclude:
29
+ - "spec/**/*"
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 CHANGED
@@ -6,34 +6,3 @@
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 3
10
- # Configuration parameters: EnforcedStyle, AllowedGems, Include.
11
- # SupportedStyles: Gemfile, gems.rb, gemspec
12
- # Include: **/*.gemspec, **/Gemfile, **/gems.rb
13
- Gemspec/DevelopmentDependencies:
14
- Exclude:
15
- - 'weather_gov_api.gemspec'
16
-
17
- # Offense count: 2
18
- # Configuration parameters: EnforcedStyle.
19
- # SupportedStyles: native, lf, crlf
20
- Layout/EndOfLine:
21
- Exclude:
22
- - 'spec/weather_gov_api_spec.rb'
23
- - 'weather_gov_api.gemspec'
24
-
25
- # Offense count: 1
26
- # Configuration parameters: Prefixes, AllowedPatterns.
27
- # Prefixes: when, with, without
28
- RSpec/ContextWording:
29
- Exclude:
30
- - 'spec/weather_gov_api/client_spec.rb'
31
-
32
- # Offense count: 10
33
- # Configuration parameters: CountAsOne.
34
- RSpec/ExampleLength:
35
- Max: 22
36
-
37
- # Offense count: 5
38
- RSpec/MultipleExpectations:
39
- Max: 4
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
@@ -76,9 +76,6 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/Jeffre
76
76
  - [ ] Distributed rate limiting (Redis/DB-based)
77
77
  - [ ] Add caching support for API responses
78
78
  - [ ] Automate CHANGELOG.md updates during release process
79
- - [ ] Configure Git repository settings:
80
- - [ ] Disable direct commits to main branch
81
- - [ ] Require passing CI builds for branch merges
82
79
 
83
80
  ## License
84
81
 
@@ -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.1"
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.1
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-22 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.
@@ -78,12 +35,15 @@ files:
78
35
  - ".rspec"
79
36
  - ".rubocop.yml"
80
37
  - ".rubocop_todo.yml"
38
+ - ".ruby-version"
39
+ - ".vscode/extensions.json"
81
40
  - CHANGELOG.md
82
41
  - CODE_OF_CONDUCT.md
83
42
  - LICENSE.txt
84
43
  - README.md
85
44
  - Rakefile
86
45
  - lib/weather_gov_api.rb
46
+ - lib/weather_gov_api/api_errors.rb
87
47
  - lib/weather_gov_api/client.rb
88
48
  - lib/weather_gov_api/response.rb
89
49
  - lib/weather_gov_api/version.rb
@@ -97,7 +57,6 @@ metadata:
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
59
  rubygems_mfa_required: 'true'
100
- post_install_message:
101
60
  rdoc_options: []
102
61
  require_paths:
103
62
  - lib
@@ -112,8 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
71
  - !ruby/object:Gem::Version
113
72
  version: '0'
114
73
  requirements: []
115
- rubygems_version: 3.5.16
116
- signing_key:
74
+ rubygems_version: 3.6.5
117
75
  specification_version: 4
118
76
  summary: Ruby wrapper for the Weather.gov API
119
77
  test_files: []