Class: Ambient

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel_id, **args) ⇒ Ambient

Create instance.

Parameters:

  • channel_id (String)

    Channel ID for Ambient.

  • args (Hash)

    a customizable set of options

Options Hash (**args):

  • :write_key (String)

    used when sending data.

  • :read_key (String)

    used when receiving and getting prop.

  • :user_key (String)

    unused



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_idObject (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_keyObject

Returns the value of attribute read_key.



10
11
12
# File 'lib/ruby-ambient.rb', line 10

def read_key
  @read_key
end

#user_keyObject

Returns the value of attribute user_key.



10
11
12
# File 'lib/ruby-ambient.rb', line 10

def user_key
  @user_key
end

#write_keyObject

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

#propArray<Hash>

Receive channel information

Returns:

  • (Array<Hash>)


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.

Parameters:

  • args (Hash)

    a customizable set of options

Options Hash (**args):

  • :date (String)

    used when sending data

  • :start (String)

    used when receiving and getting prop

  • :end (String)

    unused

  • :n (String)

    Specify the number of data to be read. The latest [:n] data are read.

  • :skip (String)

    Available only when the N option is enabled. It skips the latest recent data and reads the [n] data after that.

Returns:

  • (Array<Hash>)

    It skips [:skip] of the latest data and reads [:n] of the latest data after that.



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.

Parameters:

  • data (Object)

    Hash or Array of Hash.

Returns:

  • (Net::HTTPResponse)


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