module Josean::Objects

Public Class Methods

button(name, url, style) click to toggle source
Creates a button item for the section[:items]

name Name of the label action action name that will be triggered style Hash containing the style

# File lib/josean/objects.rb, line 58
def self.button name, url, style
  if valid_name(name) and valid_url(url) and valid_style(style)
    {
      type: 'label',
      text: name,
      style: style,
      href: {
        url: url
      }
    }
  else
    nil
  end
end
image(url, style) click to toggle source
Creates an image item for the section[:items]

url Url to which it points style Hash containing the style

# File lib/josean/objects.rb, line 76
def self.image url, style
  if valid_url(url) and valid_style(style)
    {
      type: "image",
      url: url,
      style: style
    }
  else
    nil
  end
end
label(name, style) click to toggle source
Creates a label item for the section[:items]

name Name of the label style Hash containing the style

# File lib/josean/objects.rb, line 25
def self.label name, style
  if valid_name(name) and valid_style(style)
    {
      type: 'label',
      text: name,
      style: style,
    }
  else
    nil
  end
end
map(coordinates, width, height, pins, style) click to toggle source
Creates a map item for the section[:items]

style Hash containing the style

# File lib/josean/objects.rb, line 121
def self.map coordinates, width, height, pins, style
  if valid_coordinates(coordinates) and valid_radio(width) and valid_radio(height) and valid_pins(pins) and valid_style(style)

    output = {
      type: "map",
      region: {
        coord: coordinates.join(','),
        width: width.to_s,
        height: height.to_s
      },
      style: style
    }
    if pins.any?
      output[:pins] = pins.map do |p|
        pin p[:title], p[:description], p[:coord], p[:style]
      end
    end
    output
  else
    nil
  end
end
pin(name, description, coordinates, style) click to toggle source
Creates a pin item for a map for the section[:items]

style Hash containing the style

# File lib/josean/objects.rb, line 146
def self.pin name, description, coordinates, style
  if valid_name(name) and valid_name(description) and valid_coordinates(coordinates) and valid_style(style)
    {
      title: name,
      description: description,
      coord: coordinates.join(','),
      style: style
    }
  else
    nil
  end
end
slider(name, value, action, style) click to toggle source
Creates an image item for the section[:items]

url Url to which it points style Hash containing the style

# File lib/josean/objects.rb, line 91
def self.slider name, value, action, style
  if valid_name(name) and valid_value_slider(value) and valid_action(action) and valid_style(style)
    {
      type: "slider",
      name: name,
      value: value.to_s,
      action: {
        "trigger": action
      }
    }
  else
    nil
  end
end
space(style) click to toggle source
Creates a space item for the section[:items]

style Hash containing the style

# File lib/josean/objects.rb, line 108
def self.space style
  if valid_style(style)
    {
      type: "space",
      style: style 
    }
  else
    nil
  end
end
switch(name, value, action, style) click to toggle source

Creates a switch

# File lib/josean/objects.rb, line 5
def self.switch name, value, action, style
  if valid_name(name) and valid_value_switch(value) and valid_action(action) and valid_style(style)
    {
      type: 'switch',
      name: name,
      value: value,
      action: {
        trigger: action,
        options: {
          item: "Name of the switch #{name}"
        }
      }
    }
  else
    nil
  end
end
textfield(name, style) click to toggle source
Creates a textfield item for the section[:items]

name Name of the label style Hash containing the style

# File lib/josean/objects.rb, line 40
def self.textfield name, style
  if valid_name(name) and valid_style(style)
    {
      type: 'textfield',
      name: name,
      keyboard: 'email',
      placeholder: "Enter #{name.downcase}",
      style: style 
    }
  else
    nil
  end
end

Private Class Methods

valid_action(action) click to toggle source
# File lib/josean/objects.rb, line 168
def self.valid_action action
  action.is_a? Hash
end
valid_coordinates(coordinates) click to toggle source
# File lib/josean/objects.rb, line 184
def self.valid_coordinates coordinates
  coordinates.is_a? Array and coordinates.length == 2 and (coordinates.first.is_a? Float or coordinates.first.is_a? Integer) and (coordinates.last.is_a? Float or coordinates.last_is_a? Integer)
end
valid_name(name) click to toggle source
# File lib/josean/objects.rb, line 160
def self.valid_name name
  name.is_a? String
end
valid_pin(pin) click to toggle source
# File lib/josean/objects.rb, line 196
def self.valid_pin pin
  pin.is_a? Hash and pin.keys.sort == [:coord, :title, :description, :style].sort
end
valid_pins(pins) click to toggle source
# File lib/josean/objects.rb, line 192
def self.valid_pins pins
  pins.is_a? Array and (not pins.map{ |p| valid_pin(p)}.include?(false))
end
valid_radio(radio) click to toggle source
# File lib/josean/objects.rb, line 188
def self.valid_radio radio
  (radio.is_a? Integer or radio.is_a? Float) and radio >= 0 and radio <=  100000
end
valid_style(style) click to toggle source
# File lib/josean/objects.rb, line 164
def self.valid_style style
  style.is_a? Hash
end
valid_url(url) click to toggle source
# File lib/josean/objects.rb, line 172
def self.valid_url url
  url =~ URI::Parser.new.make_regexp
end
valid_value_slider(value) click to toggle source
# File lib/josean/objects.rb, line 180
def self.valid_value_slider value
  (value.is_a? Float or value.is_a? Integer) and (value >= 0) and (value <= 1)
end
valid_value_switch(value) click to toggle source
# File lib/josean/objects.rb, line 176
def self.valid_value_switch value
  value.is_a? TrueClass
end