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 Attribute Summary collapse

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:



15
16
17
18
19
20
21
# File 'lib/pmp/credential.rb', line 15

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.



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

def root
  @root
end

Instance Method Details

#create(params = {}) ⇒ Object



28
29
30
31
# File 'lib/pmp/credential.rb', line 28

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

#create_credentials_urlObject



63
64
65
# File 'lib/pmp/credential.rb', line 63

def create_credentials_url
  root.auth['urn:collectiondoc:form:createcredentials'].url
end

#create_params(params = {}) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/pmp/credential.rb', line 38

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



59
60
61
# File 'lib/pmp/credential.rb', line 59

def credentials_url
  root.auth['urn:collectiondoc:form:listcredentials'].url
end

#destroy(client_id) ⇒ Object



33
34
35
36
# File 'lib/pmp/credential.rb', line 33

def destroy(client_id)
  response = request(:delete, remove_credentials_url(client_id))
  response.body
end

#listObject



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

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

#remove_credentials_url(client_id) ⇒ Object



67
68
69
70
# File 'lib/pmp/credential.rb', line 67

def remove_credentials_url(client_id)
  link = root.auth['urn:collectiondoc:form:removecredentials']
  link.where(client_id: client_id).url
end

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

:nodoc:



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pmp/credential.rb', line 46

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