Sha256: 697902291af73342b41270c7b0458082836e02985e3b5daa3c6ce5c7e03a5cc2

Contents?: true

Size: 1022 Bytes

Versions: 1

Compression:

Stored size: 1022 Bytes

Contents

module HabiticaCli
  # responsible for communicating with habit at a low level
  class Api
    def initialize(user_id, api_token)
      @debug = ENV['DEBUG_HABITICA'] == 'true'

      @connection = create_connection(user_id, api_token)
    end

    def get(url)
      @connection.get(url)
    end

    def post(url, body = nil)
      @connection.post(url, body)
    end

    def put(url, body)
      @connection.put(url, body)
    end

    private

    def default_headers(user_id, api_token)
      {
        'x-api-key' => api_token,
        'x-api-user' => user_id,
        'Content-Type' => 'application/json'
      }
    end

    def create_connection(user_id, api_token)
      Faraday.new(
        url: 'https://habitica.com/api/v2/',
        headers: default_headers(user_id, api_token)
      ) do |faraday|
        faraday.request :json

        faraday.response :json, content_type: /\bjson$/
        faraday.response :logger if @debug

        faraday.adapter  Faraday.default_adapter
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
habitica_cli-0.0.1 lib/habitica_cli/api.rb