Sha256: 7c1dbfef4495a6a4375f57a7432f8ed3a8918bde7b7c71d4ee0046932cc62278

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require_relative 'endpoints'

module NatureRemo
  # Client to send requests to Nature Remo cloud API
  class Client
    include NatureRemo::Endpoints

    BASE_URL = 'https://api.nature.global/'
    API_VERSION = '1'

    def initialize(access_token)
      raise ArgumentError, 'argument access_token must not be nil' if access_token.nil?

      @client = Faraday.new(url: "#{BASE_URL}#{API_VERSION}/") do |conn|
        conn.request :json
        conn.response :json, content_type: /\bjson$/, parser_options: { symbolize_names: true }
        conn.adapter Faraday.default_adapter
        conn.use RaiseError
      end
      @client.headers['Authorization'] = "Bearer #{access_token}"
    end

    private

    def get(path)
      response = @client.get(path)

      raise NatureRemo::Error, "request failed with status code #{response.status}, #{response.body}" unless response.success?

      response.body
    end

    def post(path, params = nil)
      response = @client.post(path) do |req|
        req.body = params
      end

      raise NatureRemo::Error, "request failed with status code #{response.status}, #{response.body}" unless response.success?

      response.body
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nature_remo_client-0.1.0 lib/nature_remo/client.rb