Module: Subaru::Helper

Defined in:
lib/subaru/helper.rb

Overview

Sinatra helper

Instance Method Summary collapse

Instance Method Details

#device_with_name(name) ⇒ Object

Create device object with name

Parameters:

  • name (String)

Returns:

  • (Object)

37
38
39
40
41
42
43
# 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]}) ⇒ Object

Convert object into JSON.


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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) ⇒ Boolean

Validate auth token, if configured.

Returns:

  • (Boolean)

28
29
30
31
32
# 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