Class: Ambient
- Inherits:
-
Object
- Object
- Ambient
- Defined in:
- lib/ruby-ambient.rb,
lib/ambient/version.rb
Overview
This class is for communicating with Ambient API.
Constant Summary collapse
- BASE_URL =
'http://ambidata.io/api/v2/channels/%s'
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#channel_id ⇒ Object
readonly
Returns the value of attribute channel_id.
-
#read_key ⇒ Object
Returns the value of attribute read_key.
-
#user_key ⇒ Object
Returns the value of attribute user_key.
-
#write_key ⇒ Object
Returns the value of attribute write_key.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(channel_id, **args) ⇒ Ambient
constructor
Create instance.
-
#prop ⇒ Array<Hash>
Receive channel information.
-
#read(**args) ⇒ Array<Hash>
Receive sensor data from Ambient.
-
#send(data) ⇒ Net::HTTPResponse
Send sensor data to Ambient.
Constructor Details
#initialize(channel_id, **args) ⇒ Ambient
Create instance.
20 21 22 23 24 25 26 27 |
# File 'lib/ruby-ambient.rb', line 20 def initialize(channel_id, **args) @channel_id = channel_id @base_url = BASE_URL % @channel_id @write_key = args[:write_key] @read_key = args[:read_key] @user_key = args[:user_key] end |
Instance Attribute Details
#channel_id ⇒ Object (readonly)
Returns the value of attribute channel_id.
9 10 11 |
# File 'lib/ruby-ambient.rb', line 9 def channel_id @channel_id end |
#read_key ⇒ Object
Returns the value of attribute read_key.
10 11 12 |
# File 'lib/ruby-ambient.rb', line 10 def read_key @read_key end |
#user_key ⇒ Object
Returns the value of attribute user_key.
10 11 12 |
# File 'lib/ruby-ambient.rb', line 10 def user_key @user_key end |
#write_key ⇒ Object
Returns the value of attribute write_key.
10 11 12 |
# File 'lib/ruby-ambient.rb', line 10 def write_key @write_key end |
Class Method Details
.symbolize_keys(hash) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ruby-ambient.rb', line 86 def symbolize_keys(hash) return hash unless hash&.is_a?(Hash) hash.map do |k, v| [ k.to_sym, case v when Hash symbolize_keys(v) when Array v.each { |obj| symbolize_keys(obj) } else v end ] end.to_h end |
Instance Method Details
#prop ⇒ Array<Hash>
Receive channel information
76 77 78 79 80 81 82 83 |
# File 'lib/ruby-ambient.rb', line 76 def prop @prop ||= begin url = @base_url url += "?readKey=#{@read_key}" if @read_key uri = URI.parse(url) JSON.parse(get(uri), symbolize_names: true) end end |
#read(**args) ⇒ Array<Hash>
Receive sensor data from Ambient.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ruby-ambient.rb', line 49 def read(**args) args = self.class.symbolize_keys(args) opt = {} opt[:readKey] = @read_key if @read_key if args.key?(:date) opt[:date] = args[:date] elsif args.key?(:start) && args.key?(:end) opt[:start] = args[:start] opt[:end] = args[:end] elsif args.key?(:n) opt[:n] = args[:n] opt[:skip] = args[:skip] if args.key?(:skip) end url = "#{@base_url}/data" url += '?' + opt.to_a.map { |k, v| "#{k}=#{v}" }.join('&') unless opt.empty? uri = URI.parse(url) res = JSON.parse(get(uri), symbolize_names: true) Array.new(res).reverse end |
#send(data) ⇒ Net::HTTPResponse
Send sensor data to Ambient.
33 34 35 36 37 38 39 |
# File 'lib/ruby-ambient.rb', line 33 def send(data) data = ([] << data).flatten headers = { 'Content-Type': 'application/json' } uri = URI.parse("#{@base_url}/dataarray") post(uri, { writeKey: @write_key, data: data }.to_json, headers) end |