owm_sdk 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a96afd1d1c4a14f4f7e8328953212d16d61a493b3c1761d87ce061554b943c6e
4
+ data.tar.gz: 37a169b11b7fa4d8ef3bbfe6c328ce815c5edac7b356b5f39e19a7c28f2f6812
5
+ SHA512:
6
+ metadata.gz: b2fc2e062b873c40acc9ae282668da1089990baa1f161bc1f5535e4cea9860d42208e7a89c0dc56bbc7f976edce0298c928baca262909c524a0d542d4481b5fa
7
+ data.tar.gz: bd9fda28c45f5b276795e1dee5c61b1b1b6c4a04ddd75eed246b978938fbae243c3582784f0eca25c6644909b830f172b7d25fd0092d7b7a4b3d0ed655e64bad
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 2.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-03-09
4
+
5
+ - Initial release
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Alexander Kulichkov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # OwmSdk
2
+
3
+ Example SDK for working with OpenWeatherMap
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install
14
+
15
+ ## Usage
16
+
17
+ SDK supports two working mode: on demand and polling.
18
+
19
+ On demand usage:
20
+
21
+ ```ruby
22
+
23
+ require 'owm_sdk'
24
+
25
+ client = OwmSdk::Weather.new(
26
+ api_key: "OpenWeatherMap API Key",
27
+ mode: "on_demand",
28
+ units: "metric"
29
+ )
30
+
31
+ puts client.get_weather("London")
32
+
33
+ # Requested data is now cached for next 10 minutes
34
+
35
+ ```
36
+
37
+ Polling usage:
38
+
39
+ ```ruby
40
+ require 'owm_sdk'
41
+
42
+ client = OwmSdk::Weather.new(
43
+ api_key: "...",
44
+ mode: "polling",
45
+ units: "metric",
46
+ )
47
+
48
+ puts client.get_weather("London")
49
+
50
+ # Weather data for requested location
51
+ # will be constantly updated in the background
52
+
53
+ puts client.get_weather("London") # Gets new data with zero latency
54
+ ```
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ 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).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hetyh/owm-sdk-ruby.
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[test standard]
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OwmSdk
4
+ module Config
5
+ extend self
6
+
7
+ ATTRIBUTES = %i[
8
+ api_key
9
+ mode
10
+ units
11
+ ].freeze
12
+
13
+ attr_accessor(*Config::ATTRIBUTES)
14
+
15
+ def reset
16
+ self.api_key = nil
17
+ self.mode = nil
18
+ self.units = nil
19
+ end
20
+ end
21
+
22
+ class << self
23
+ def configure
24
+ block_given? ? yield(Config) : Config
25
+ end
26
+
27
+ def config
28
+ Config
29
+ end
30
+ end
31
+ end
32
+
33
+ OwmSdk::Config.reset
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OwmSdk
4
+ module Errors
5
+ class Error < StandardError; end
6
+
7
+ class RequestError < StandardError; end
8
+
9
+ class ServerError < StandardError; end
10
+ end
11
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OwmSdk
4
+ module Request
5
+ private
6
+
7
+ def get(path, query)
8
+ uri = URI.parse("https://api.openweathermap.org")
9
+ uri.path = path
10
+ uri.query = URI.encode_www_form(query)
11
+
12
+ res = Net::HTTP.get_response(uri)
13
+ body = JSON.parse(res.body)
14
+
15
+ case res.code.to_f
16
+ when 200 then body
17
+ when 400..499 then raise OwmSdk::Errors::RequestError, (body["message"]).to_s
18
+ when 500..599 then raise OwmSdk::Errors::ServerError, (body["message"]).to_s
19
+ else raise OwmSdk::Errors::Error, (body["message"]).to_s
20
+ end
21
+ end
22
+
23
+ def get_weather_request(location)
24
+ res = get("/data/2.5/weather", {lat: location[:lat], lon: location[:lon], appid: @api_key, units: @units.to_s})
25
+
26
+ weather_data = res["weather"].first
27
+ temperature_data = res["main"]
28
+ wind_data = res["wind"]
29
+ sys_data = res["sys"]
30
+
31
+ weather = {
32
+ weather: {
33
+ main: weather_data["main"],
34
+ description: weather_data["description"]
35
+ },
36
+ temperature: {
37
+ temp: temperature_data["temp"],
38
+ feels_like: temperature_data["feels_like"]
39
+ },
40
+ visibility: res["visibility"],
41
+ wind: {
42
+ speed: wind_data["speed"]
43
+ },
44
+ datetime: res["dt"],
45
+ sys: {
46
+ sunrise: sys_data["sunrise"],
47
+ sunset: sys_data["sunset"]
48
+ },
49
+ timezone: res["timezone"],
50
+ name: res["name"]
51
+ }
52
+
53
+ @weather_cache[location] = weather
54
+
55
+ weather
56
+ end
57
+
58
+ def get_location_request(city)
59
+ res = get("/geo/1.0/direct", {q: city, limit: 1, appid: @api_key})
60
+
61
+ raise OwmSdk::Errors::Error, "Provided city was not found" if res[0].nil?
62
+
63
+ location = {lat: res[0]["lat"], lon: res[0]["lon"]}
64
+
65
+ @location_cache[city] = location
66
+
67
+ location
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OwmSdk
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OwmSdk
4
+ class Weather
5
+ include Request
6
+
7
+ attr_accessor(*Config::ATTRIBUTES)
8
+
9
+ POLLING_RATE = 600
10
+ LOCATION_CACHE_SIZE = 10
11
+ WEATHER_CACHE_SIZE = 10
12
+ WEATHER_CACHE_TTL = 600
13
+
14
+ def get_weather(city)
15
+ location = get_location(city)
16
+
17
+ weather_cached = @weather_cache[location]
18
+ return weather_cached unless weather_cached.nil?
19
+
20
+ get_weather_request(location)
21
+ end
22
+
23
+ def initialize(config = {})
24
+ OwmSdk::Config::ATTRIBUTES.each do |key|
25
+ send(:"#{key}=", config[key] || OwmSdk.config.send(key))
26
+ end
27
+
28
+ validate_configuration(config)
29
+
30
+ @logger = Logger.new(STDOUT)
31
+
32
+ @location_cache = LruRedux::Cache.new(LOCATION_CACHE_SIZE)
33
+ @weather_cache = LruRedux::TTL::Cache.new(WEATHER_CACHE_SIZE, WEATHER_CACHE_TTL)
34
+
35
+ @polling_thread = Thread.new { polling_loop } if @mode == "polling"
36
+ end
37
+
38
+ class << self
39
+ def configure
40
+ block_given? ? yield(Config) : Config
41
+ end
42
+
43
+ def config
44
+ Config
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def validate_configuration(config)
51
+ validate_api_key(config[:api_key])
52
+ validate_mode(config[:mode]) unless config[:mode].nil?
53
+ validate_units(config[:units]) unless config[:units].nil?
54
+ end
55
+
56
+ def validate_api_key(api_key)
57
+ raise ArgumentError, "API key cannot be nil or empty" if api_key.nil? || api_key.empty?
58
+ end
59
+
60
+ def validate_mode(mode)
61
+ valid_modes = %w[on_demand polling]
62
+ raise ArgumentError, "Invalid mode. Supported modes: #{valid_modes.join(", ")}" unless valid_modes.include?(mode)
63
+ end
64
+
65
+ def validate_units(units)
66
+ valid_units = %w[standard metric imperial]
67
+
68
+ unless valid_units.include?(units)
69
+ raise ArgumentError,
70
+ "Invalid units. Supported units: #{valid_units.join(", ")}"
71
+ end
72
+ end
73
+
74
+ def get_location(city)
75
+ location = @location_cache[city]
76
+
77
+ return location unless location.nil?
78
+
79
+ get_location_request(city)
80
+ end
81
+
82
+ def update_weather
83
+ @location_cache.to_a.each do |city|
84
+ get_weather_request(city[1])
85
+ end
86
+ end
87
+
88
+ def polling_loop
89
+ loop do
90
+ begin
91
+ update_weather
92
+ rescue Error => e
93
+ @logger.error "Error during polling: #{e.message}"
94
+ end
95
+
96
+ sleep POLLING_RATE
97
+ end
98
+ end
99
+ end
100
+ end
data/lib/owm_sdk.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "owm_sdk/version"
4
+ require_relative "owm_sdk/errors"
5
+ require_relative "owm_sdk/config"
6
+ require_relative "owm_sdk/request"
7
+ require_relative "owm_sdk/weather"
8
+
9
+ require "lru_redux"
10
+ require "uri"
11
+ require "net/http"
12
+ require "json"
13
+ require "logger"
data/sig/owm_sdk.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module OwmSdk
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: owm_sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Kulichkov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-03-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lru_redux
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: webmock
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.23.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.23.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest-stub-const
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.6'
55
+ description:
56
+ email:
57
+ - hetyh2004@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".standard.yml"
63
+ - CHANGELOG.md
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - lib/owm_sdk.rb
68
+ - lib/owm_sdk/config.rb
69
+ - lib/owm_sdk/errors.rb
70
+ - lib/owm_sdk/request.rb
71
+ - lib/owm_sdk/version.rb
72
+ - lib/owm_sdk/weather.rb
73
+ - sig/owm_sdk.rbs
74
+ homepage: https://github.com/hetyh/owm-sdk-ruby
75
+ licenses:
76
+ - MIT
77
+ metadata:
78
+ homepage_uri: https://github.com/hetyh/owm-sdk-ruby
79
+ source_code_uri: https://github.com/hetyh/owm-sdk-ruby
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 2.6.0
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubygems_version: 3.5.3
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: An example SDK for accessing OpenWeatherMap API
99
+ test_files: []