Module: Api::Connection

Included in:
Client
Defined in:
lib/api/connection.rb

Overview

Network layer for API clients.

Constant Summary collapse

CONVENIENCE_HEADERS =

Header keys that can be passed in options hash to #get,#head

Set.new([:accept, :content_type])

Instance Method Summary collapse

Instance Method Details

#agentSawyer::Agent

Hypermedia agent for the API

Returns:

  • (Sawyer::Agent)


103
104
105
106
107
108
109
110
111
112
113
# File 'lib/api/connection.rb', line 103

def agent
  @agent ||= Sawyer::Agent.new(endpoint, sawyer_options) do |http|
    http.headers[:content_type] = "application/json"
    http.headers[:user_agent] = user_agent
    if basic_authenticated?
      http.basic_auth(@basic_login, @basic_password)
    elsif token_authenticated?
      http.authorization @access_token_prefix, @access_token
    end
  end
end

#delete(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP DELETE request

Parameters:

  • url (String)

    The path, relative to Api::Connection#api_endpoint/#api_endpoint/#api_version

  • options (Hash) (defaults to: {})

    Query and header params for request

Returns:

  • (Sawyer::Resource)


53
54
55
# File 'lib/api/connection.rb', line 53

def delete(url, options = {})
  request :delete, url, options
end

#get(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP GET request

Parameters:

  • url (String)

    The path, relative to Api::Connection#api_endpoint/#api_endpoint/#api_version

  • options (Hash) (defaults to: {})

    Query and header params for request

Returns:

  • (Sawyer::Resource)


17
18
19
# File 'lib/api/connection.rb', line 17

def get(url, options = {})
  request :get, url, parse_query_and_convenience_headers(options)
end

#head(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP HEAD request

Parameters:

  • url (String)

    The path, relative to Api::Connection#api_endpoint/#api_endpoint/#api_version

  • options (Hash) (defaults to: {})

    Query and header params for request

Returns:

  • (Sawyer::Resource)


62
63
64
# File 'lib/api/connection.rb', line 62

def head(url, options = {})
  request :head, url, parse_query_and_convenience_headers(options)
end

#last_responseSawyer::Response

Response for last HTTP request

Returns:

  • (Sawyer::Response)


125
126
127
# File 'lib/api/connection.rb', line 125

def last_response
  @last_response if defined? @last_response
end

#paginate(url, options = {}, &block) ⇒ Sawyer::Resource

Make one or more HTTP GET requests, optionally fetching the next page of results from URL in Link response header based on value in #auto_paginate.

Parameters:

  • url (String)

    The path, relative to Api::Connection#api_endpoint/#api_endpoint/#api_version

  • options (Hash) (defaults to: {})

    Query and header params for request

  • block (Block)

    Block to perform the data concatination of the multiple requests. The block is called with two parameters, the first contains the contents of the requests so far and the second parameter contains the latest response.

Returns:

  • (Sawyer::Resource)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/api/connection.rb', line 77

def paginate(url, options = {}, &block)
  opts = parse_query_and_convenience_headers(options.dup)
  if @auto_paginate || @per_page
    opts[:query][:per_page] ||=  @per_page || (@auto_paginate ? 100 : nil)
  end

  data = request(:get, url, opts.dup)

  if @auto_paginate
    while @last_response.rels[:next] && rate_limit.remaining > 0
      @last_response = @last_response.rels[:next].get(:headers => opts[:headers])
      if block_given?
        yield(data, @last_response)
      else
        data.concat(@last_response.data) if @last_response.data.is_a?(Array)
      end
    end

  end

  data
end

#patch(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP PATCH request

Parameters:

  • url (String)

    The path, relative to Api::Connection#api_endpoint/#api_endpoint/#api_version

  • options (Hash) (defaults to: {})

    Body and header params for request

Returns:

  • (Sawyer::Resource)


44
45
46
# File 'lib/api/connection.rb', line 44

def patch(url, options = {})
  request :patch, url, options
end

#post(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP POST request

Parameters:

  • url (String)

    The path, relative to Api::Connection#api_endpoint/#api_endpoint/#api_version

  • options (Hash) (defaults to: {})

    Body and header params for request

Returns:

  • (Sawyer::Resource)


26
27
28
# File 'lib/api/connection.rb', line 26

def post(url, options = {})
  request :post, url, options
end

#put(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP PUT request

Parameters:

  • url (String)

    The path, relative to Api::Connection#api_endpoint/#api_endpoint/#api_version

  • options (Hash) (defaults to: {})

    Body and header params for request

Returns:

  • (Sawyer::Resource)


35
36
37
# File 'lib/api/connection.rb', line 35

def put(url, options = {})
  request :put, url, options
end

#rootSawyer::Resource

Fetch the root resource for the API

Returns:

  • (Sawyer::Resource)


118
119
120
# File 'lib/api/connection.rb', line 118

def root
  get "/"
end