Class: PMP::Token

Inherits:
Object
  • Object
show all
Includes:
Configuration
Defined in:
lib/pmp/token.rb

Constant Summary

Constants included from Configuration

Configuration::DEFAULT_ADAPTER, Configuration::DEFAULT_CLIENT_ID, Configuration::DEFAULT_CLIENT_SECRET, Configuration::DEFAULT_ENDPOINT, Configuration::DEFAULT_TOKEN_TYPE, Configuration::DEFAULT_USER_AGENT, Configuration::VALID_OPTIONS_KEYS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configuration

#apply_configuration, #configure, extended, #options, #reset!

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Token

Returns a new instance of Token.

Yields:

  • (_self)

Yield Parameters:

  • _self (PMP::Token)

    the object that the method was called on



12
13
14
15
16
17
18
# File 'lib/pmp/token.rb', line 12

def initialize(options={}, &block)
  apply_configuration(options)

  self.root = current_options.delete(:root)

  yield(self) if block_given?
end

Instance Attribute Details

#rootObject

Returns the value of attribute root.



10
11
12
# File 'lib/pmp/token.rb', line 10

def root
  @root
end

Instance Method Details

#connection_options(opts = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pmp/token.rb', line 40

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_tokenObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pmp/token.rb', line 24

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_urlObject



20
21
22
# File 'lib/pmp/token.rb', line 20

def token_url
  root.auth['urn:collectiondoc:form:issuetoken'].url
end