Sha256: 4050dab7b2f7c13bb55132520cc484e71db9c49232d68a9871e3278389a1a49b

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

module Switchbot
  class Client
    API_ENDPOINT = 'https://api.switch-bot.com'

    def initialize(token)
      @token = token
    end

    def devices
      request(
        http_method: :get,
        endpoint: 'v1.0/devices'
      )
    end

    def device(device_id)
      Device.new(client: self, device_id: device_id)
    end

    def status(device_id:)
      request(
        http_method: :get,
        endpoint: "/v1.0/devices/#{device_id}/status"
      )
    end

    def commands(device_id:, command:, parameter: 'default', command_type: 'command')
      request(
        http_method: :post,
        endpoint: "/v1.0/devices/#{device_id}/commands",
        params: {
          command: command,
          parameter: parameter,
          commandType: command_type
        }
      )
    end

    def scenes
      request(
        http_method: :get,
        endpoint: 'v1.0/scenes'
      )
    end

    def scene(scene_id)
      Scene.new(client: self, scene_id: scene_id)
    end

    def execute(scene_id:)
      request(
        http_method: :post,
        endpoint: "/v1.0/scenes/#{scene_id}/execute"
      )
    end

    def bot(device_id)
      Bot.new(client: self, device_id: device_id)
    end

    def light(device_id)
      Light.new(client: self, device_id: device_id)
    end

    def humidifier(device_id)
      Humidifier.new(client: self, device_id: device_id)
    end

    private

    def headers
      {
        'User-Agent' => "Switchbot v#{Switchbot::VERSION} (https://github.com/ytkg/switchbot)",
        'Authorization' => @token
      }
    end

    def connection
      Faraday.new(url: API_ENDPOINT, headers: headers) do |conn|
        conn.request :json
      end
    end

    def request(http_method:, endpoint:, params: {})
      response = connection.public_send(http_method, endpoint, params)
      JSON.parse(response.body).deep_transform_keys(&:underscore).deep_symbolize_keys
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
switchbot-0.5.1 lib/switchbot/client.rb
switchbot-0.5.0 lib/switchbot/client.rb