class SmartEnergyGroup::Client

Constants

DATA_TYPES

Public Class Methods

new(site_token) click to toggle source

debug_output $stdout

# File lib/smart_energy_group/client.rb, line 10
def initialize(site_token)
  @site_token = site_token
end

Public Instance Methods

generate_data(options) click to toggle source
# File lib/smart_energy_group/client.rb, line 39
def generate_data(options)
  data = []

  DATA_TYPES.keys.each do |type|
    case options[type]
    when Array
      data += options[type].map.with_index(1) { |value, i| "(#{DATA_TYPES[type]}_#{i} #{value})" }
    when Hash
      data += options[type].map { |i, value| "(#{DATA_TYPES[type]}_#{i} #{value})" }
    when NilClass
      next
    else
      raise('Unsupported object type')
    end
  end

  data.join('')
end
send_data(node_name, options) click to toggle source
# File lib/smart_energy_group/client.rb, line 27
def send_data(node_name, options)
  time = options[:when] || Time.now
  time = time.utc.strftime('%FT%T')

  data = generate_data(options)
  msg = "(site #{@site_token} (node #{node_name} #{time} #{data}))"

  response = self.class.put('/api_sites/stream', :body => msg)

  raise("Bad Post: #{response.body}") unless response.code == 200 && response.body == "(status ok)\n"
end