Class: EDH::Passport::API

Inherits:
Object
  • Object
show all
Includes:
RestAPIMethods
Defined in:
lib/edh/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RestAPIMethods

#create, #delete, #rest_call, #update

Constructor Details

#initialize(options = {}) ⇒ EDH::Passport::API

Note:

If no access token is provided, you can only access some public information.

Creates a new API client.

Parameters:

  • access_token (String)

    access token



33
34
35
36
37
# File 'lib/edh/api.rb', line 33

def initialize options = {}
   @access_token = options[:access_token] || Passport.config.access_token
   @app_access_token = options[:app_access_token] || Passport.config.app_access_token
   @server = options[:server] || Passport.config.server
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



27
28
29
# File 'lib/edh/api.rb', line 27

def access_token
  @access_token
end

#app_access_tokenObject

Returns the value of attribute app_access_token.



27
28
29
# File 'lib/edh/api.rb', line 27

def app_access_token
  @app_access_token
end

#serverObject

Returns the value of attribute server.



27
28
29
# File 'lib/edh/api.rb', line 27

def server
  @server
end

Instance Method Details

#api(path, args = {}, verb = "get", options = {}, &error_checking_block) { ... } ⇒ Object

Note:

You’ll rarely need to call this method directly.

Makes a request to the appropriate Passport API.

Parameters:

  • path

    the server path for this request (leading / is prepended if not present)

  • args (defaults to: {})

    arguments to be sent to Passport

  • verb (defaults to: "get")

    the HTTP method to use

  • options (defaults to: {})

    request-related options for EDH and Faraday.

  • error_checking_block

    a block to evaluate the response status for additional JSON-encoded errors

Options Hash (options):

  • :http_component (Symbol)

    which part of the response (headers, body, or status) to return

  • :use_ssl (Boolean)

    force SSL for this request, even if it’s tokenless. (All API requests with access tokens use SSL.)

Yields:

  • The response for evaluation

Returns:

  • the body of the response from Passport (unless another http_component is requested)

Raises:

See Also:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/edh/api.rb', line 61

def api(path, args = {}, verb = "get", options = {}, &error_checking_block)
  # If a access token is explicitly provided, use that
  # This is explicitly needed in batch requests so GraphCollection
  # results preserve any specific access tokens provided
  args["access_token"] ||= @access_token || @app_access_token if @access_token || @app_access_token
  options.merge!({:server => @server}) unless @server.nil?

  # Translate any arrays in the params into comma-separated strings
  args = sanitize_request_parameters(args)

  # add a leading /
  path = "/#{path}" unless path =~ /^\//

  # make the request via the provided service
  result = EDH.make_request(path, args, verb, options)

  if result.status.to_i >= 500
    raise EDH::Passport::ServerError.new(result.status.to_i, result.body)
  end

  yield result if error_checking_block

  # if we want a component other than the body (e.g. redirect header for images), return that
  if component = options[:http_component]
    component == :response ? result : result.send(options[:http_component])
  else
    # parse the body as JSON and run it through the error checker (if provided)
    # Note: Passport sometimes sends results like "true" and "false", which aren't strictly objects
    # and cause MultiJson.load to fail -- so we account for that by wrapping the result in []
    MultiJson.load("[#{result.body.to_s}]")[0]
  end
end