Sha256: 636d25e4882598e58d33875c1425d88143119f6121b94714c10640f2dfec5525

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require 'faraday'
require 'json'

module Sesame
  module Api
    ENDPOINT_URL = 'https://api.candyhouse.co/public'.freeze

    def client
      @client ||= Faraday.new(url: ENDPOINT_URL)
    end

    def auth_token(value)
      @auth_token = value
      self
    end

    def get_sesames
      get('sesames')
    end

    def get_sesame(device_id:)
      get("sesame/#{device_id}")
    end

    def control_sesame(device_id:, command:)
      post("sesame/#{device_id}", command: command)
    end

    def get(path)
      call(:get, path)
    end

    def post(path, params)
      call(:post, path, params)
    end

    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

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sesame-ruby-1.0.0 lib/sesame/api.rb