module Subaru::Helper

Sinatra helper

Public Instance Methods

device_with_name(name) click to toggle source

Create device object with name @param name [String] @return [Object]

# File lib/subaru/helper.rb, line 37
def device_with_name(name)
  c = settings.subaru_config[:devices][name]
  return Object.const_get("Subaru::Definitions::#{c[:definition]}").new(c) if c
rescue => e
  puts e.message
  return nil
end
json_with_object(object, opts = {pretty: settings.subaru_config[:global][:pretty_json]}) click to toggle source

Convert object into JSON.

# File lib/subaru/helper.rb, line 11
def json_with_object(object, opts = {pretty: settings.subaru_config[:global][:pretty_json]})
  return '{}' if object.nil?
  if opts[:pretty] == true
    opts = {
      indent: '  ',
      space: ' ',
      object_nl: "\n",
      array_nl: "\n"
    }
  end
  JSON.fast_generate(object, opts)
rescue => e
  puts e.message
  raise
end
valid_token?(token, method = :any) click to toggle source

Validate auth token, if configured.

# File lib/subaru/helper.rb, line 28
def valid_token?(token, method = :any)
  tokens = settings.subaru_config[:global][:auth_tokens][method]
  return true if tokens.nil?
  tokens.include?(token)
end