class Geckoboard::PayloadFormatter

Attributes

dataset[R]

Public Class Methods

new(dataset) click to toggle source
# File lib/geckoboard/payload_formatter.rb, line 5
def initialize(dataset)
  @dataset = dataset
end

Public Instance Methods

format(payload) click to toggle source
# File lib/geckoboard/payload_formatter.rb, line 9
def format(payload)
  payload.map do |item|
    item.inject({}) do |hash, (field_name, value)|
      field_name = field_name.to_s

      value = case field_type(field_name)
              when 'date'     then format_date(value)
              when 'datetime' then format_datetime(value)
              else
                value
              end

      hash.merge(field_name => value)
    end
  end
end

Private Instance Methods

field_type(name) click to toggle source
# File lib/geckoboard/payload_formatter.rb, line 28
def field_type(name)
  @field_types ||= dataset.fields.inject({}) do |hash, (field_name, definition)|
    hash.merge(field_name => definition.fetch('type'))
  end

  @field_types.fetch(name, 'string')
end
format_date(value) click to toggle source
# File lib/geckoboard/payload_formatter.rb, line 36
def format_date(value)
  return value unless value.respond_to? :strftime
  value.strftime('%Y-%m-%d')
end
format_datetime(value) click to toggle source
# File lib/geckoboard/payload_formatter.rb, line 41
def format_datetime(value)
  return value unless value.respond_to? :strftime
  value.strftime('%Y-%m-%dT%H:%M:%S%:z')
end