ruby-ambient 0.1.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 +7 -0
- data/.gitignore +8 -0
- data/.rspec +1 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +34 -0
- data/README.md +124 -0
- data/Rakefile +2 -0
- data/examples/prop.rb +5 -0
- data/examples/read.rb +5 -0
- data/examples/send.rb +5 -0
- data/lib/ambient/version.rb +3 -0
- data/lib/ruby-ambient.rb +115 -0
- data/ruby-ambient.gemspec +27 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 86bd069eeb4b8253e91441e6015e798f43a2a505cb840b731acd6d9c9183b0cb
|
4
|
+
data.tar.gz: ac440460aba33680beae072b40964159457a7884451e17de8f4c627b504fc351
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 45abaa5971bc905c6a6a0b9a74ea1fea9679f28013bc46c74c2a6e72451dc819a6eab963c20ed3f9ec81e5d8dbd3da012986a2586642c3398e74f1edba150e2b
|
7
|
+
data.tar.gz: 2d8ee417ee30284431358d59769ef0f680a1b0d486dfd31a9b45f1f42e5f2661d2a7f17dd9f7d8856d0fa1ac5a433ce046b2d50fd3ae854e34d569606488176e
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ruby-ambient (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.4.4)
|
10
|
+
rake (13.0.1)
|
11
|
+
rspec (3.9.0)
|
12
|
+
rspec-core (~> 3.9.0)
|
13
|
+
rspec-expectations (~> 3.9.0)
|
14
|
+
rspec-mocks (~> 3.9.0)
|
15
|
+
rspec-core (3.9.2)
|
16
|
+
rspec-support (~> 3.9.3)
|
17
|
+
rspec-expectations (3.9.2)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.9.0)
|
20
|
+
rspec-mocks (3.9.1)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.9.0)
|
23
|
+
rspec-support (3.9.3)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
rake
|
30
|
+
rspec
|
31
|
+
ruby-ambient!
|
32
|
+
|
33
|
+
BUNDLED WITH
|
34
|
+
2.1.2
|
data/README.md
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
# Ruby::Ambient
|
2
|
+
|
3
|
+
A simple library that calls Ambient's API, which can be used similarly to [official python library](https://github.com/AmbientDataInc/ambient-python-lib)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'ruby-ambient'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install ruby-ambient
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Send sensor data to Ambient
|
24
|
+
|
25
|
+
`write_key` is required. Set it when you create the ambient instance, or set it from accessor.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
am = Ambient.new('12345', write_key: '1234567890abcdef')
|
29
|
+
p am.send({d1: 1.03, d2: 2.2})
|
30
|
+
# => #<Net::HTTPOK 200 OK readbody=true>
|
31
|
+
```
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
am = Ambient.new('12345')
|
35
|
+
am.write_key = '1234567890abcdef'
|
36
|
+
am.send([
|
37
|
+
{ created: '2020/7/18 20:21:19', d1: 2.3, d2: 3.8 },
|
38
|
+
{ created: '2020/7/18 20:21:20', d1: 2.1, d2: 3.1 },
|
39
|
+
])
|
40
|
+
```
|
41
|
+
|
42
|
+
### Receive sensor data from Ambient
|
43
|
+
|
44
|
+
`read_key` is required. Set it when you create the ambient instance, or set it from accessor.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
require 'pp'
|
48
|
+
am = Ambient.new('12345', read_key: '1234567890abcdef')
|
49
|
+
pp am.read()
|
50
|
+
|
51
|
+
# =>
|
52
|
+
# [{:d1=>1.1, :d2=>2.1, :created=>"2017-02-18T03:00:00.000Z"},
|
53
|
+
# {:d1=>1.5, :d2=>3.8, :created=>"2017-02-18T03:01:00.000Z"},
|
54
|
+
# {:d1=>1, :d2=>0.8, :created=>"2017-02-18T03:02:00.000Z"},
|
55
|
+
# {:d1=>2.3, :d2=>3.8, :created=>"2020-07-16T11:21:19.000Z"},
|
56
|
+
# {:d1=>2.1, :d2=>3.1, :created=>"2020-07-16T11:21:20.000Z"},
|
57
|
+
# {:d1=>1.1, :d2=>2.1, :created=>"2020-07-18T07:00:00.000Z"},
|
58
|
+
# {:d1=>1.1, :d2=>2.1, :created=>"2020-07-18T07:00:00.000Z"},
|
59
|
+
# {:d1=>1.5, :d2=>3.8, :created=>"2020-07-18T07:01:00.000Z"},
|
60
|
+
# {:d1=>1.5, :d2=>3.8, :created=>"2020-07-18T07:01:00.000Z"},
|
61
|
+
# {:d1=>1, :d2=>0.8, :created=>"2020-07-18T07:02:00.000Z"},
|
62
|
+
# {:d1=>1, :d2=>0.8, :created=>"2020-07-18T07:02:00.000Z"},
|
63
|
+
# {:d1=>1, :d2=>2, :created=>"2020-07-18T08:43:33.240Z"},
|
64
|
+
# {:d1=>1, :d2=>2, :created=>"2020-07-18T12:31:18.886Z"},
|
65
|
+
# {:d1=>1.03, :d2=>2.2, :created=>"2020-07-18T13:21:13.917Z"},
|
66
|
+
# {:d1=>1.03, :d2=>2.2, :created=>"2020-07-18T13:21:26.165Z"},
|
67
|
+
# {:d1=>1.03, :d2=>2.2, :created=>"2020-07-18T13:21:57.722Z"},
|
68
|
+
# {:d1=>1.03, :d2=>2.2, :created=>"2020-07-18T13:22:23.619Z"},
|
69
|
+
# {:d1=>1.03, :d2=>2.2, :created=>"2020-07-18T13:23:03.688Z"},
|
70
|
+
# {:d1=>1.03, :d2=>2.2, :created=>"2020-07-18T13:24:04.937Z"},
|
71
|
+
# {:d1=>1.03, :d2=>2.2, :created=>"2020-07-18T13:26:25.843Z"}]
|
72
|
+
```
|
73
|
+
|
74
|
+
If you want to specify a date, set the `date`.
|
75
|
+
The format of the date is `YYYY-mm-dd` or `YYYY/mm/dd`.
|
76
|
+
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
require 'pp'
|
80
|
+
am = Ambient.new('12345', read_key: '1234567890abcdef')
|
81
|
+
pp am.read(date: '2017-2-18')
|
82
|
+
|
83
|
+
# =>
|
84
|
+
# [{:d1=>1.1, :d2=>2.1, :created=>"2017-02-18T03:00:00.000Z"},
|
85
|
+
# {:d1=>1.5, :d2=>3.8, :created=>"2017-02-18T03:01:00.000Z"},
|
86
|
+
# {:d1=>1, :d2=>0.8, :created=>"2017-02-18T03:02:00.000Z"}]
|
87
|
+
```
|
88
|
+
|
89
|
+
If you want to specify the period, set `start` and `end`.
|
90
|
+
The time zone of the date is `JST`.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
require 'pp'
|
94
|
+
am = Ambient.new('12345', read_key: '1234567890abcdef')
|
95
|
+
pp am.read(start: '2020-07-18 22:20:00', end: '2020-07-18 22:25:00')
|
96
|
+
|
97
|
+
# =>
|
98
|
+
# [{:d1=>1.03, :d2=>2.2, :created=>"2020-07-18T13:21:13.917Z"},
|
99
|
+
# {:d1=>1.03, :d2=>2.2, :created=>"2020-07-18T13:21:26.165Z"},
|
100
|
+
# {:d1=>1.03, :d2=>2.2, :created=>"2020-07-18T13:21:57.722Z"},
|
101
|
+
# {:d1=>1.03, :d2=>2.2, :created=>"2020-07-18T13:22:23.619Z"},
|
102
|
+
# {:d1=>1.03, :d2=>2.2, :created=>"2020-07-18T13:23:03.688Z"},
|
103
|
+
# {:d1=>1.03, :d2=>2.2, :created=>"2020-07-18T13:24:04.937Z"}]
|
104
|
+
```
|
105
|
+
|
106
|
+
### Receive channel information
|
107
|
+
|
108
|
+
`read_key` is required. Set it when you create the ambient instance, or set it from accessor.
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
require 'pp'
|
112
|
+
am = Ambient.new('12345', read_key: '1234567890abcdef')
|
113
|
+
pp am.prop()
|
114
|
+
|
115
|
+
# =>
|
116
|
+
# {:ch=>"12345",
|
117
|
+
# :user=>"1234",
|
118
|
+
# ...
|
119
|
+
# }
|
120
|
+
```
|
121
|
+
|
122
|
+
## Contributing
|
123
|
+
|
124
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/suruseas/ruby-ambient.
|
data/Rakefile
ADDED
data/examples/prop.rb
ADDED
data/examples/read.rb
ADDED
data/examples/send.rb
ADDED
data/lib/ruby-ambient.rb
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "ambient/version"
|
3
|
+
|
4
|
+
require 'net/http'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
# This class is for communicating with Ambient API.
|
8
|
+
class Ambient
|
9
|
+
attr_reader :channel_id
|
10
|
+
attr_accessor :write_key, :read_key, :user_key
|
11
|
+
|
12
|
+
BASE_URL = 'http://ambidata.io/api/v2/channels/%s'
|
13
|
+
|
14
|
+
# Create instance.
|
15
|
+
#
|
16
|
+
# @param [String] channel_id Channel ID for Ambient.
|
17
|
+
# @option args [String] :write_key used when sending data.
|
18
|
+
# @option args [String] :read_key used when receiving and getting prop.
|
19
|
+
# @option args [String] :user_key unused
|
20
|
+
def initialize(channel_id, **args)
|
21
|
+
@channel_id = channel_id
|
22
|
+
@base_url = BASE_URL % @channel_id
|
23
|
+
|
24
|
+
@write_key = args[:write_key]
|
25
|
+
@read_key = args[:read_key]
|
26
|
+
@user_key = args[:user_key]
|
27
|
+
end
|
28
|
+
|
29
|
+
# Send sensor data to Ambient.
|
30
|
+
#
|
31
|
+
# @param [Object] data Hash or Array of Hash.
|
32
|
+
# @return [Net::HTTPResponse]
|
33
|
+
def send(data)
|
34
|
+
data = ([] << data).flatten
|
35
|
+
headers = { 'Content-Type': 'application/json' }
|
36
|
+
uri = URI.parse("#{@base_url}/dataarray")
|
37
|
+
|
38
|
+
post(uri, { writeKey: @write_key, data: data }.to_json, headers)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Receive sensor data from Ambient.
|
42
|
+
#
|
43
|
+
# @option args [String] :date used when sending data
|
44
|
+
# @option args [String] :start used when receiving and getting prop
|
45
|
+
# @option args [String] :end unused
|
46
|
+
# @option args [String] :n Specify the number of data to be read. The latest [:n] data are read.
|
47
|
+
# @option args [String] :skip Available only when the N option is enabled. It skips the latest recent data and reads the [n] data after that.
|
48
|
+
# @return [Array<Hash>] It skips [:skip] of the latest data and reads [:n] of the latest data after that.
|
49
|
+
def read(**args)
|
50
|
+
args = self.class.symbolize_keys(args)
|
51
|
+
|
52
|
+
opt = {}
|
53
|
+
opt[:readKey] = @read_key if @read_key
|
54
|
+
|
55
|
+
if args.key?(:date)
|
56
|
+
opt[:date] = args[:date]
|
57
|
+
elsif args.key?(:start) && args.key?(:end)
|
58
|
+
opt[:start] = args[:start]
|
59
|
+
opt[:end] = args[:end]
|
60
|
+
elsif args.key?(:n)
|
61
|
+
opt[:n] = args[:n]
|
62
|
+
opt[:skip] = args[:skip] if args.key?(:skip)
|
63
|
+
end
|
64
|
+
|
65
|
+
url = "#{@base_url}/data"
|
66
|
+
url += '?' + opt.to_a.map { |k, v| "#{k}=#{v}" }.join('&') unless opt.empty?
|
67
|
+
uri = URI.parse(url)
|
68
|
+
|
69
|
+
res = JSON.parse(get(uri), symbolize_names: true)
|
70
|
+
Array.new(res).reverse
|
71
|
+
end
|
72
|
+
|
73
|
+
# Receive channel information
|
74
|
+
#
|
75
|
+
# @return [Array<Hash>]
|
76
|
+
def prop
|
77
|
+
@prop ||= begin
|
78
|
+
url = @base_url
|
79
|
+
url += "?readKey=#{@read_key}" if @read_key
|
80
|
+
uri = URI.parse(url)
|
81
|
+
JSON.parse(get(uri), symbolize_names: true)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class << self
|
86
|
+
def symbolize_keys(hash)
|
87
|
+
return hash unless hash&.is_a?(Hash)
|
88
|
+
|
89
|
+
hash.map do |k, v|
|
90
|
+
[
|
91
|
+
k.to_sym,
|
92
|
+
case v
|
93
|
+
when Hash
|
94
|
+
symbolize_keys(v)
|
95
|
+
when Array
|
96
|
+
v.each { |obj| symbolize_keys(obj) }
|
97
|
+
else
|
98
|
+
v
|
99
|
+
end
|
100
|
+
]
|
101
|
+
end.to_h
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
def post(uri, payload, headers = {})
|
108
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
109
|
+
http.post(uri.path, payload, headers)
|
110
|
+
end
|
111
|
+
|
112
|
+
def get(uri)
|
113
|
+
Net::HTTP.get(uri)
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative 'lib/ambient/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "ruby-ambient"
|
5
|
+
spec.version = Ambient::VERSION
|
6
|
+
spec.authors = ["yukihiro amadatsu"]
|
7
|
+
spec.email = ["suruseas@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{A simple library for using the Ambient API.}
|
10
|
+
spec.description = %q{A RubyGem library that can be used like the official python library}
|
11
|
+
spec.homepage = "https://github.com/suruseas/ruby-ambient"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
15
|
+
spec.metadata["source_code_uri"] = "https://github.com/suruseas/ruby-ambient"
|
16
|
+
spec.metadata["changelog_uri"] = "https://github.com/suruseas/ruby-ambient/CHANGELOG.md"
|
17
|
+
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-ambient
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- yukihiro amadatsu
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '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: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: A RubyGem library that can be used like the official python library
|
42
|
+
email:
|
43
|
+
- suruseas@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- CHANGELOG.md
|
51
|
+
- Gemfile
|
52
|
+
- Gemfile.lock
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- examples/prop.rb
|
56
|
+
- examples/read.rb
|
57
|
+
- examples/send.rb
|
58
|
+
- lib/ambient/version.rb
|
59
|
+
- lib/ruby-ambient.rb
|
60
|
+
- ruby-ambient.gemspec
|
61
|
+
homepage: https://github.com/suruseas/ruby-ambient
|
62
|
+
licenses: []
|
63
|
+
metadata:
|
64
|
+
homepage_uri: https://github.com/suruseas/ruby-ambient
|
65
|
+
source_code_uri: https://github.com/suruseas/ruby-ambient
|
66
|
+
changelog_uri: https://github.com/suruseas/ruby-ambient/CHANGELOG.md
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 2.3.0
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubygems_version: 3.1.2
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: A simple library for using the Ambient API.
|
86
|
+
test_files: []
|