class RubySpark::Device

Public Class Methods

new(device_id, access_token = RubySpark.access_token) click to toggle source
# File lib/ruby_spark/device.rb, line 6
def initialize(device_id, access_token = RubySpark.access_token)
  raise RubySpark::ConfigurationError.new("Access Token not defined") if access_token.nil?

  @access_token = access_token
  @device_id    = device_id
end

Public Instance Methods

function(function_name, arguments) click to toggle source
# File lib/ruby_spark/device.rb, line 27
def function(function_name, arguments)
  response = post(function_name, :params => arguments)
  handle(response) do
    response["return_value"]
  end
end
info() click to toggle source
# File lib/ruby_spark/device.rb, line 13
def info
  response = get("")
  handle(response) do
    response
  end
end
variable(variable_name) click to toggle source
# File lib/ruby_spark/device.rb, line 20
def variable(variable_name)
  response = get(variable_name)
  handle(response) do
    response["result"]
  end
end

Private Instance Methods

access_params() click to toggle source
# File lib/ruby_spark/device.rb, line 70
def access_params
  {:access_token => @access_token}
end
base_url() click to toggle source
# File lib/ruby_spark/device.rb, line 66
def base_url
  "https://api.particle.io/v1/devices/#{@device_id}/"
end
error_from(response) click to toggle source
# File lib/ruby_spark/device.rb, line 58
def error_from(response)
  response["error"].tap do |error|
    description = response["error_description"]
    error.concat(": #{description}") if description
    error.concat(": Invalid Device ID") if error == "Permission Denied"
  end
end
get(action, params = {}) click to toggle source
# File lib/ruby_spark/device.rb, line 43
def get(action, params = {})
  url  = base_url + action
  query = access_params.merge(params)

  HTTParty.get(url, :query => query, :timeout => timeout).parsed_response
end
handle(response) { |block| ... } click to toggle source
# File lib/ruby_spark/device.rb, line 50
def handle(response, &block)
  if error = error_from(response)
    raise ApiError.new(error) if error.length > 0
  else
    yield block
  end
end
post(action, params = {}) click to toggle source
# File lib/ruby_spark/device.rb, line 36
def post(action, params = {})
  url  = base_url + action
  body = access_params.merge(params)

  HTTParty.post(url, :body => body, :timeout => timeout).parsed_response
end
timeout() click to toggle source
# File lib/ruby_spark/device.rb, line 74
def timeout
  RubySpark.timeout
end