Class: PMP::Credential

Inherits:
Object
  • Object
show all
Includes:
Configuration, Connection
Defined in:
lib/pmp/credential.rb

Constant Summary collapse

CREDENTIAL_PARAMS =
[:scope, :token_expires_in, :label]

Constants included from Connection

PMP::Connection::ALLOWED_CONNECTION_OPTIONS

Constants included from Configuration

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

Instance Method Summary collapse

Methods included from Connection

#add_request_auth, #connection, #process_options

Methods included from Configuration

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

Constructor Details

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

Returns a new instance of Credential.

Yields:

  • (_self)

Yield Parameters:



13
14
15
16
17
# File 'lib/pmp/credential.rb', line 13

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

  yield(self) if block_given?
end

Instance Method Details

#create(params = {}) ⇒ Object



24
25
26
27
# File 'lib/pmp/credential.rb', line 24

def create(params={})
  response = request(:post, credentials_url, create_params(params))
  response.body
end

#create_params(params = {}) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/pmp/credential.rb', line 29

def create_params(params={})
  HashWithIndifferentAccess.new({
    scope: 'read',
    token_expires_in: 60*60*24*30,
    label: "#{user}: #{Time.now}"
  }).merge(params.select{|k,v| CREDENTIAL_PARAMS.include?(k.to_sym)})
end

#credentials_urlObject



50
51
52
# File 'lib/pmp/credential.rb', line 50

def credentials_url
  "#{endpoint}auth/credentials"
end

#listObject



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

def list
  response = request(:get, credentials_url)
  response.body
end

#request(method, url, body = {}) ⇒ Object

:nodoc:



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pmp/credential.rb', line 37

def request(method, url, body={}) # :nodoc:

  headers = {
    'Accept' => "*/*",
    'Content-Type' => [:put, :post].include?(method) ? "application/x-www-form-urlencoded" : nil
  }

  conn_opts = current_options.merge({headers: headers, basic_auth: true})

  raw = connection(conn_opts).send(method, url, body)
  PMP::Response.new(raw, {method: method, url: url, body: body})
end