class PMP::Token

Attributes

root[RW]

Public Class Methods

new(options={}) { |self| ... } click to toggle source
# File lib/pmp/token.rb, line 11
def initialize(options={}, &block)
  apply_configuration(options)

  self.root = current_options.delete(:root)

  yield(self) if block_given?
end

Public Instance Methods

connection_options(opts={}) click to toggle source
# File lib/pmp/token.rb, line 39
def connection_options(opts={})
  headers = opts.delete(:headers) || {}
  options = {
    headers: {
      'User-Agent'   => opts[:user_agent],
      'Accept'       => 'application/json',
      'Content-Type' => 'application/x-www-form-urlencoded'
    },
    ssl: {:verify => false},
    url: opts[:endpoint]
  }.merge(opts)
  options[:headers] = options[:headers].merge(headers)

  # clean out any that don't belong
  options.select{|k,v| PMP::Connection::ALLOWED_CONNECTION_OPTIONS.include?(k.to_sym)}
end
get_token() click to toggle source
# File lib/pmp/token.rb, line 23
def get_token
  oauth_options = {
    site:            endpoint,
    token_url:       token_url,
    connection_opts: connection_options(options)
  }

  client = OAuth2::Client.new(client_id, client_secret, oauth_options) do |faraday|
    faraday.request  :url_encoded
    faraday.response :logger if debug
    faraday.adapter  adapter
  end

  client.client_credentials.get_token
end
token_url() click to toggle source
# File lib/pmp/token.rb, line 19
def token_url
  root.auth['urn:collectiondoc:form:issuetoken'].url
end