module Sesame::Api

Constants

ENDPOINT_URL

Public Instance Methods

auth_token(value) click to toggle source
# File lib/sesame/api.rb, line 12
def auth_token(value)
  @auth_token = value
  self
end
call(method, path, params = nil) click to toggle source
# File lib/sesame/api.rb, line 37
def call(method, path, params = nil)
  response = client.send(method) do |req|
    req.url path
    req.headers['Content-Type'] = 'application/json'
    req.headers['Authorization'] = @auth_token unless @auth_token.nil?
    req.body = params.to_json unless params.nil?
  end
  parse_response(response)
end
client() click to toggle source
# File lib/sesame/api.rb, line 8
def client
  @client ||= Faraday.new(url: ENDPOINT_URL)
end
control_sesame(device_id:, command:) click to toggle source
# File lib/sesame/api.rb, line 25
def control_sesame(device_id:, command:)
  post("sesame/#{device_id}", command: command)
end
get(path) click to toggle source
# File lib/sesame/api.rb, line 29
def get(path)
  call(:get, path)
end
get_sesame(device_id:) click to toggle source
# File lib/sesame/api.rb, line 21
def get_sesame(device_id:)
  get("sesame/#{device_id}")
end
get_sesames() click to toggle source
# File lib/sesame/api.rb, line 17
def get_sesames
  get('sesames')
end
parse_response(response) click to toggle source
# File lib/sesame/api.rb, line 47
def parse_response(response)
  parsed_response = response.headers['Content-Length'].to_i > 0 ? JSON.parse(response.body) : ''
  raise Error.new(response.status, parsed_response) if response.status >= 400

  parsed_response
end
post(path, params) click to toggle source
# File lib/sesame/api.rb, line 33
def post(path, params)
  call(:post, path, params)
end