class AmbientIot::Client

Public Class Methods

new(channel_id, options={}) click to toggle source
# File lib/ambient_iot/client.rb, line 31
def initialize channel_id, options={}
  @channel_id = channel_id
  @write_key = options[:write_key]
  @read_key = options[:read_key]
  @user_key = options[:user_key]

  @post_data = []
  @append_timestamp = true
end

Public Instance Methods

<<(data) click to toggle source
# File lib/ambient_iot/client.rb, line 41
def << data
  data = [data] unless data.is_a? Array
  if append_timestamp?
    now = time_to_s Time.now
    data.each{|d| d["created"] = now}
  end
  @post_data += data
end
append_timestamp=(flag) click to toggle source
# File lib/ambient_iot/client.rb, line 54
def append_timestamp= flag
  @append_timestamp = flag
end
append_timestamp?() click to toggle source
# File lib/ambient_iot/client.rb, line 50
def append_timestamp?
  @append_timestamp
end
info() click to toggle source
# File lib/ambient_iot/client.rb, line 68
def info
  get_property
end
Also aliased as: prop, property
prop()
Alias for: info
property()
Alias for: info
read(options={}) click to toggle source
# File lib/ambient_iot/client.rb, line 64
def read options={}
  get options
end
sync()
Alias for: write
synchronize()
Alias for: write
write() click to toggle source
# File lib/ambient_iot/client.rb, line 58
def write
  post
end
Also aliased as: sync, synchronize

Private Instance Methods

date_to_s(date) click to toggle source
# File lib/ambient_iot/client.rb, line 137
def date_to_s date
  date.strftime("%Y-%m-%d")
end
get(options={}) click to toggle source
# File lib/ambient_iot/client.rb, line 107
def get options={}
  raise "'read_key' is required." unless @read_key

  url = URI("http://ambidata.io/api/v2/channels/#{@channel_id}/data")
  query = { readKey:@read_key }
  if options[:date]
    query[:date] = time_to_s options[:date]
  elsif options[:start] and options[:end]
    query[:start] = time_to_s options[:start]
    query[:end] = time_to_s options[:end]
  elsif options[:n]
    query[:n] = options[:n]
    query[:skip] = options[:skip] if options[:skip]
  end
  url.query = query.to_param
  get_with_url url
end
get_property() click to toggle source
# File lib/ambient_iot/client.rb, line 125
def get_property
  raise "'read_key' is required." unless @read_key

  url = URI("http://ambidata.io/api/v2/channels/#{@channel_id}")
  url.query = { readKey:@read_key }.to_param
  get_with_url url
end
get_with_url(url) click to toggle source
# File lib/ambient_iot/client.rb, line 94
def get_with_url url
  req = Net::HTTP::Get.new(url)
  res = Net::HTTP.new(url.host, url.port).start {|http|
    http.request(req)
  }
  case res
  when Net::HTTPSuccess#, Net::HTTPRedirection
    obj_to_symbolize JSON.parse(res.body)
  else
    res.value
  end
end
obj_to_symbolize(obj) click to toggle source
# File lib/ambient_iot/client.rb, line 141
def obj_to_symbolize obj
  begin
    case obj
    when Hash
      obj = obj.deep_symbolize_keys!
      obj.each do |k, v|
        case v
        when String
          if /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z/ =~ v
            obj[k] = Time.parse(v).localtime
          end
        when Array, Hash
          obj[k] = obj_to_symbolize v
        end
      end
    when Array
      obj.map{|h|
        case h
        when Hash, Array
          h = obj_to_symbolize h
        end
        h
      }
    end
  rescue
    obj
  end
end
post() click to toggle source
# File lib/ambient_iot/client.rb, line 76
def post
  raise "'write_key' is required." unless @write_key

  url = URI("http://ambidata.io/api/v2/channels/#{@channel_id}/dataarray")
  req = Net::HTTP::Post.new(url.path, 'Content-Type' => 'application/json')
  payload = {writeKey:@write_key, data:@post_data}
  req.body = payload.to_json
  res = Net::HTTP.new(url.host, url.port).start {|http|
    http.request(req)
  }
  case res
  when Net::HTTPSuccess#, Net::HTTPRedirection
    @post_data = []
  else
    res.value
  end
end
time_to_s(time) click to toggle source
# File lib/ambient_iot/client.rb, line 133
def time_to_s time
  time.strftime("%Y-%m-%d %H:%M:%S.%L")
end