module Particle::Connection

Network layer for API client

Constants

MIDDLEWARE

Faraday middleware stack

Attributes

last_response[R]

Response for last HTTP request

@return [Faraday::Response]

Public Instance Methods

connection() click to toggle source

HTTP connection for the Particle API

@return [Faraday::Connection]

# File lib/particle/connection.rb, line 70
def connection
  @connection ||= Faraday.new(conn_opts) do |http|
    http.url_prefix = endpoint
    if @access_token
      http.authorization :Bearer, @access_token
    end
  end
end
delete(url, options = {}) click to toggle source

Make a HTTP DELETE request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Query and header params for request @return [Hash] JSON response as a hash

# File lib/particle/connection.rb, line 63
def delete(url, options = {})
  request :delete, url, options
end
get(url, options = {}) click to toggle source

Make a HTTP GET request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Query and header params for request @return [Hash] JSON response as a hash

# File lib/particle/connection.rb, line 27
def get(url, options = {})
  request :get, url, options
end
patch(url, options = {}) click to toggle source

Make a HTTP PATCH request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Body and header params for request @return [Hash] JSON response as a hash

# File lib/particle/connection.rb, line 54
def patch(url, options = {})
  request :patch, url, options
end
post(url, options = {}) click to toggle source

Make a HTTP POST request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Body and header params for request @return [Hash] JSON response as a hash

# File lib/particle/connection.rb, line 36
def post(url, options = {})
  request :post, url, options
end
put(url, options = {}) click to toggle source

Make a HTTP PUT request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Body and header params for request @return [Hash] JSON response as a hash

# File lib/particle/connection.rb, line 45
def put(url, options = {})
  request :put, url, options
end

Protected Instance Methods

endpoint() click to toggle source
# File lib/particle/connection.rb, line 86
def endpoint
  api_endpoint
end

Private Instance Methods

basic_auth_header(username, password) click to toggle source

Temporarily set the Authorization to use basic auth

# File lib/particle/connection.rb, line 137
def basic_auth_header(username, password)
  Faraday::Request.lookup_middleware(:basic_auth).
    header(username, password)
end
conn_opts() click to toggle source
# File lib/particle/connection.rb, line 130
def conn_opts
  conn_opts = @connection_options.dup
  conn_opts[:builder] = MIDDLEWARE
  conn_opts
end
prepare_request_options(data, options) click to toggle source
# File lib/particle/connection.rb, line 112
def prepare_request_options(data, options)
  if data.is_a?(Hash)
    options[:query]   ||= data.delete(:query) || {}
    options[:headers] ||= data.delete(:headers) || {}
    if accept = data.delete(:accept)
      options[:headers][:accept] = accept
    end
  end

  username = options.delete(:username)
  password = options.delete(:password)
  if username && password
    options[:headers] ||= {}
    options[:headers][:authorization] =
      basic_auth_header(username, password)
  end
end
request(method, path, data, options = {}) click to toggle source
# File lib/particle/connection.rb, line 96
def request(method, path, data, options = {})
  prepare_request_options(data, options)
  @last_response = response = connection.send(method, URI::Parser.new.escape(path.to_s)) do |req|
    if data && method != :get
      req.body = data
    end
    if params = options[:query]
      req.params.update params
    end
    if headers = options[:headers]
      req.headers.update headers
    end
  end
  response.body
end
reset_connection() click to toggle source
# File lib/particle/connection.rb, line 92
def reset_connection
  @connection = nil
end