Sha256: d92b03ce127fafc378bba7b99f2b003b4aeb6e60258772a92179de4191c5e325

Contents?: true

Size: 1.25 KB

Versions: 9

Compression:

Stored size: 1.25 KB

Contents

module Terminal
  class Client
    include Terminal::API

    API_VERSION = "v0.1"
    API_URL = 'https://api.terminal.com'
    API_PREFIX = API_URL + "/" + API_VERSION + "/"

    attr_accessor :user_token, :access_token

    def initialize( options = {})
      options.each do | k, v |
        if v.is_a?(Integer)
          raise Terminal::APIConfigurationError
        end
        send(:"#{k}=", v)
      end
      yield(self) if block_given?
    end

    def post(path, params = {} )
      request(:post, path, params)
    end

  private

    def connection(options = {} )
      @connection ||= Faraday.new(API_URL, options) do | conn |
        conn.request   :encode_json
        conn.adapter   :net_http
        conn.response  :parse_json
        conn.response  :raise_error_from_json
      end
    end

    def request(method, path, params = {})
      auth = params.delete(:auth)
      params = params.merge(auth_params) if auth
      connection.send(method.to_sym, API_PREFIX + path, params)
    rescue Faraday::Error::TimeoutError, Timeout::Error => error
      raise(Terminal::APITimeoutError.new(error))
    end

    def auth_params
      params = {}
      params[:access_token] = access_token
      params[:user_token] = user_token
      params
    end
  end
end



Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
cc-terminal-0.1.1 lib/terminal/client.rb
cc-terminal-0.1.0 lib/terminal/client.rb
cc-terminal-0.0.8 lib/terminal/client.rb
cc-terminal-0.0.7 lib/terminal/client.rb
cc-terminal-0.0.6 lib/terminal/client.rb
cc-terminal-0.0.5 lib/terminal/client.rb
cc-terminal-0.0.4 lib/terminal/client.rb
cc-terminal-0.0.3 lib/terminal/client.rb
cc-terminal-0.0.2 lib/terminal/client.rb