ambient-ruby-client 0.1.3

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: 3f5774b557c895de5da75ebbc1a1aeae0d0d0c3ce1dd5414652d867ae8738663
4
+ data.tar.gz: 2bbfeea305c3c8e2eab7cdd7c7f04937af5a4f31389c231b48dd1f524efc55d6
5
+ SHA512:
6
+ metadata.gz: '069b20da0446e1ea4df52e0c446199395dcbb9a80a6b2e2a97451a614116fe75f6fece86220f83e2d1bd3a031c96c70421518d00b147424d57845a69369b2e1d'
7
+ data.tar.gz: 6be6e04180d5251aa160879b617308253e0c637f1fd03b982af5e014d64acecaeb6763eac3bf078919ee799e29b1a16684571c444b80a31a97a84b12c5633ec6
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ # 0.1.2
2
+ The initial version
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 TODO: Write your name
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # Ambient::Ruby::Client
2
+
3
+ A Ruby gem that interacts with the [Ambient Weather REST API](https://ambientweather.docs.apiary.io/#introduction).
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add ambient-ruby-client
10
+
11
+ ## Usage
12
+
13
+ TODO: Write usage instructions here
14
+
15
+ ## Development
16
+
17
+ TODO: Write development instructions here
18
+
19
+ ## Contributing
20
+
21
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jar349/ambient-ruby-client.
22
+
23
+ ## License
24
+
25
+ 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,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "net/http"
5
+
6
+ require_relative "client_config"
7
+ require_relative "device"
8
+
9
+ module Ambient
10
+ class ClientError < StandardError; end
11
+
12
+ class Client
13
+ ##
14
+ # Creates a new Ambient Client
15
+ #
16
+ # @param client_config [Ambient.ClientConfiguration] the configuration for the Ambient client.
17
+ def initialize(client_config)
18
+ @config = client_config
19
+ end
20
+
21
+ def devices
22
+ path = "devices"
23
+
24
+ begin
25
+ response = _get(path, @config)
26
+ rescue StandardError => se
27
+ raise Ambient::ClientError, "Error while getting the list of devices from the Ambient API (#{se.class.name}): #{se.message}"
28
+ end
29
+
30
+ unless response.is_a? Net::HTTPOK
31
+ raise Ambient::ClientError, "The response from the Ambient API was not what was expected, got #{response.class.name} instead of Net::HTTPOK"
32
+ end
33
+
34
+ response_data = JSON.parse(response.body)
35
+ array_of_devices = []
36
+ response_data.each do |device_data|
37
+ array_of_devices << Ambient::Device.from_hash(device_data)
38
+ end
39
+
40
+ array_of_devices
41
+ end
42
+
43
+ def device_data(device_mac_address, end_date: nil, limit: 288)
44
+ path = "devices/macAddress"
45
+ params = {macAddress: device_mac_address, endDate: end_date, limit: }
46
+
47
+ begin
48
+ response = _get(path, @config, params: )
49
+ rescue StandardError => se
50
+ raise Ambient::ClientError, "Error while getting device data from the Ambient API #{se.class.name}: #{se.message}"
51
+ end
52
+
53
+ unless response.is_a? Net::HTTPOK
54
+ raise Ambient::ClientError, "The response from the Ambient API was not what was expected, got #{response.class.name} instead of Net::HTTPOK"
55
+ end
56
+
57
+ JSON.parse(response.body)
58
+ end
59
+
60
+ private
61
+
62
+ # :nocov:
63
+ def _get(path, config, params: {})
64
+ uri = URI("#{@config.api_url}/#{path}")
65
+ all_params = params.merge({applicationKey: @config.app_key, apiKey: @config.api_key})
66
+ uri.query = URI.encode_www_form(all_params)
67
+ headers = {Accept: "application/json"}
68
+
69
+ Net::HTTP.get_response(uri, headers)
70
+ end
71
+ # :nocov:
72
+ end
73
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ambient
4
+ class ClientConfiguration
5
+ attr_reader :api_key, :app_key, :api_url
6
+
7
+ ##
8
+ # Creates a new Ambient Client
9
+ #
10
+ # @param api_key [String] the configuration for the Ambient client.
11
+ def initialize(api_key:, app_key:, api_url:)
12
+ @api_key = api_key
13
+ @app_key = app_key
14
+ @api_url = api_url
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ambient
4
+ class DeviceInfo
5
+ attr_reader :name, :location
6
+
7
+ def self.from_hash(data)
8
+ DeviceInfo.new(data.dig("name"), data.dig("location"))
9
+ end
10
+
11
+ def initialize(name, location)
12
+ @name = name
13
+ @location = location
14
+ end
15
+ end
16
+
17
+ class Device
18
+ attr_reader :mac_address, :info, :last_data
19
+
20
+ def self.from_hash(data)
21
+ Device.new(data.dig("macAddress"), Ambient::DeviceInfo.from_hash(data.dig("info")), data.dig("lastData"))
22
+ end
23
+
24
+ ###
25
+ # Create a new Device instance
26
+ #
27
+ # @param mac_address [String] the mac address of the device
28
+ # @param info [Ambient::DeviceInfo] information about the device
29
+ # @param last_data [Hash] the latest measurement data from the device
30
+ def initialize(mac_address, info, last_data)
31
+ @mac_address = mac_address,
32
+ @info = info
33
+ @last_data = last_data
34
+ end
35
+ end
36
+ end
data/lib/ambient.rb ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ambient/client"
4
+ require_relative "version"
data/lib/version.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ambient
4
+ VERSION = "0.1.3"
5
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ambient-ruby-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - John Ruiz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-09-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Use this gem to get data from the Ambient Weather REST API
14
+ email:
15
+ - jruiz@johnruiz.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".ruby-version"
21
+ - CHANGELOG.md
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - lib/ambient.rb
26
+ - lib/ambient/client.rb
27
+ - lib/ambient/client_config.rb
28
+ - lib/ambient/device.rb
29
+ - lib/version.rb
30
+ homepage: https://github.com/jar349/ambient-ruby-client
31
+ licenses:
32
+ - MIT
33
+ metadata:
34
+ allowed_push_host: https://rubygems.org
35
+ homepage_uri: https://github.com/jar349/ambient-ruby-client
36
+ source_code_uri: https://github.com/jar349/ambient-ruby-client
37
+ changelog_uri: https://github.com/jar349/ambient-ruby-client/blob/main/CHANGELOG.md
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 3.0.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.5.16
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Ruby client for the Ambient Weather REST API
57
+ test_files: []