class Geckoboard::DatasetsClient

Attributes

connection[R]

Public Class Methods

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

Public Instance Methods

delete(dataset_id) click to toggle source
# File lib/geckoboard/datasets_client.rb, line 19
def delete(dataset_id)
  path = dataset_path(dataset_id)
  connection.delete(path)
  true
end
find_or_create(dataset_id, fields: nil, unique_by: nil) click to toggle source
# File lib/geckoboard/datasets_client.rb, line 9
def find_or_create(dataset_id, fields: nil, unique_by: nil)
  path = dataset_path(dataset_id)
  body = { fields: hashify_fields(fields) }
  body[:unique_by] = unique_by unless unique_by.nil?
  response = connection.put(path, body.to_json)

  data = JSON.parse(response.body)
  Dataset.new(self, data.fetch('id'), data.fetch('fields'))
end
post_data(dataset_id, data, options) click to toggle source
# File lib/geckoboard/datasets_client.rb, line 31
def post_data(dataset_id, data, options)
  path = "#{dataset_path(dataset_id)}/data"
  body = options.merge({ data: data }).to_json
  connection.post(path, body)
  true
end
put_data(dataset_id, data) click to toggle source
# File lib/geckoboard/datasets_client.rb, line 25
def put_data(dataset_id, data)
  path = "#{dataset_path(dataset_id)}/data"
  connection.put(path, { data: data }.to_json)
  true
end

Private Instance Methods

dataset_path(dataset_id) click to toggle source
# File lib/geckoboard/datasets_client.rb, line 40
def dataset_path(dataset_id)
  "/datasets/#{CGI.escape(dataset_id)}"
end
hashify_fields(fields) click to toggle source
# File lib/geckoboard/datasets_client.rb, line 44
def hashify_fields(fields)
  return fields if fields.is_a? Hash

  unless fields.respond_to?(:inject) && fields.all? { |field| field.is_a? Field }
    raise ArgumentError, "`fields:' must be either a hash of field definitions, or collection of `Geckoboard::Field' objects"
  end

  fields.inject({}) do |hash, field|
    hash.merge(field.id => field.to_hash)
  end
end