class Bloopi::API::Auth

Attributes

client_id[R]
client_secret[R]

Public Class Methods

new(client_id = nil, client_secret = nil) click to toggle source

Creates a new client.

@param client_id [String, Integer] a Bloopi client ID @param client_secret a Bloopi client secret

# File lib/bloopi/api/auth.rb, line 14
def initialize(client_id = nil, client_secret = nil)
  @client_id = client_id || Bloopi.config.client_id
  @client_secret = client_secret || Bloopi.config.client_secret
end

Public Instance Methods

get_access_token(options = {}) click to toggle source

Fetches the application's access token (ignoring expiration and other info). @see get_app_access_token_info

@param (see get_app_access_token_info)

@return the application access token

# File lib/bloopi/api/auth.rb, line 44
def get_access_token(options = {})
  if info = get_access_token_info(options)
    Bloopi.config.access_token = info["access_token"]
  end
end
get_access_token_info(options = {}) click to toggle source

Fetches an access token, token expiration, and other info from Bloopi. Useful when you've received an OAuth code using the server-side authentication process. @see url_for_oauth_code

@note (see url_for_oauth_code)

@param code (see url_for_access_token) @param options any additional parameters to send to Bloopi when redeeming the token

@raise Bloopi::OAuthTokenRequestError if Bloopi returns an error response

@return a hash of the access token info returned by Bloopi (token, expiration, etc.)

# File lib/bloopi/api/auth.rb, line 33
def get_access_token_info(options = {})
  # convenience method to get a the application's sessionless access token
   get_token_from_server({}, true, options)
end

Protected Instance Methods

fetch_token_string(args, post = false, endpoint = "auth", options = {}) click to toggle source
# File lib/bloopi/api/auth.rb, line 68
def fetch_token_string(args, post = false, endpoint = "auth", options = {})
  raise ClientError if @client_id == nil
  raise ClientError if @client_secret == nil
  response = Bloopi.make_request("/#{endpoint}", {
    :client_id => @client_id,
    :client_secret => @client_secret
  }.merge!(args), post ? "post" : "get", {:use_ssl => true, format: :json}.merge!(options))

  raise ServerError.new(response.status, response.body) if response.status >= 500
  raise OAuthTokenRequestError.new(response.status, response.body) if response.status >= 400

  response.body
end
get_token_from_server(args, post = false, options = {}) click to toggle source
# File lib/bloopi/api/auth.rb, line 52
def get_token_from_server(args, post = false, options = {})
  # fetch the result from Bloopi's servers
  response = fetch_token_string(args, post, "auth", options)
  parse_access_token(response)
end
parse_access_token(response_text) click to toggle source
# File lib/bloopi/api/auth.rb, line 58
def parse_access_token(response_text)
  JSON.parse(response_text)
rescue JSON::ParserError
  response_text.split("&").inject({}) do |hash, bit|
    key, value = bit.split("=")
    hash.merge!(key => value)
  end
end