class Milkbottle::Client

Public Class Methods

new(options = {}) click to toggle source
# File lib/milkbottle/client.rb, line 23
def initialize(options = {})
  # Use options passed in, but fall back to module defaults
  Milkbottle::Configurable.keys.each do |key|
    instance_variable_set(:"@#{key}", options[key] || Milkbottle.instance_variable_get(:"@#{key}"))
  end
end

Public Instance Methods

agent() click to toggle source
# File lib/milkbottle/client.rb, line 58
def agent
  @agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http|
    http.headers[:content_type] = "application/json"
    http.headers[:user_agent] = user_agent
    if token_authenticated?
      http.authorization('Bearer', @jwt_token.encode(external_auth_key))
    elsif anonymous_authenticated?
      http.authorization('Bearer', @anonymous_token)
      http.headers['X-MILK-API-KEY'] = @api_key
    elsif app_authenticated?
      http.headers['X-MILK-API-KEY'] = @api_key
    else
      throw "Please supply an api_key or jwt_token"
    end
  end
end
anonymous_token=(anonymous_token) click to toggle source
# File lib/milkbottle/client.rb, line 84
def anonymous_token=(anonymous_token)
  reset_agent
  @anonymous_token = anonymous_token
end
api_key=(api_key) click to toggle source
# File lib/milkbottle/client.rb, line 79
def api_key=(api_key)
  reset_agent
  @api_key = api_key
end
delete(url, options = {}) click to toggle source
# File lib/milkbottle/client.rb, line 50
def delete(url, options = {})
  request :delete, url, options
end
get(url, options = {}) click to toggle source
# File lib/milkbottle/client.rb, line 34
def get(url, options = {})
  request :get, url, options
end
head(url, options = {}) click to toggle source
# File lib/milkbottle/client.rb, line 54
def head(url, options = {})
  request :head, url, options
end
jwt_token=(jwt_token) click to toggle source
# File lib/milkbottle/client.rb, line 89
def jwt_token=(jwt_token)
  reset_agent
  @jwt_token = jwt_token
end
last_response() click to toggle source
# File lib/milkbottle/client.rb, line 75
def last_response
  @last_response if defined? @last_response
end
patch(url, options = {}) click to toggle source
# File lib/milkbottle/client.rb, line 46
def patch(url, options = {})
  request :patch, url, options
end
post(url, options = {}) click to toggle source
# File lib/milkbottle/client.rb, line 38
def post(url, options = {})
  request :post, url, options
end
put(url, options = {}) click to toggle source
# File lib/milkbottle/client.rb, line 42
def put(url, options = {})
  request :put, url, options
end
same_options?(opts) click to toggle source
# File lib/milkbottle/client.rb, line 30
def same_options?(opts)
  opts.hash == options.hash
end

Private Instance Methods

request(method, path, data, options = {}) click to toggle source
# File lib/milkbottle/client.rb, line 100
def request(method, path, 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

  @last_response = response = agent.call(method, URI::Parser.new.escape(path.to_s), data, options)
  response.data
end
reset_agent() click to toggle source
# File lib/milkbottle/client.rb, line 96
def reset_agent
  @agent = nil
end
sawyer_options() click to toggle source
# File lib/milkbottle/client.rb, line 113
def sawyer_options
  opts = {
    :links_parser => Sawyer::LinkParsers::Simple.new
  }
  conn_opts = @connection_options
  conn_opts[:builder] = @middleware if @middleware
  conn_opts[:proxy] = @proxy if @proxy
  conn_opts[:ssl] = {:verify => false}
  opts[:faraday] = Faraday.new(conn_opts)

  opts
end