Module: PRXAccess

Defined in:
lib/prx_access.rb,
lib/prx_access/version.rb,
lib/prx_access/prx_hyper_resource.rb

Defined Under Namespace

Classes: PRXHyperResource

Constant Summary collapse

VERSION =
"0.3.1"

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



131
132
133
134
135
136
137
# File 'lib/prx_access/prx_hyper_resource.rb', line 131

def method_missing(method, *args)
  if method =~ /_root$/
    root_uri ENV[method.to_s.sub(/_root$/, '_HOST').upcase], '/api/v1'
  else
    super
  end
end

Instance Method Details

#api(options = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/prx_access/prx_hyper_resource.rb', line 89

def api(options = {})
  opts = { root: cms_root, headers: default_headers }.merge(options)
  if !opts[:headers]['Authorization'] &&  = opts.delete(:account)
    token = (, opts.delete(:scope))
    opts[:headers]['Authorization'] = "Bearer #{token}"
  end

  PRXHyperResource.new(opts)
end

#api_resource(body, root = cms_root) ⇒ Object



99
100
101
102
103
104
# File 'lib/prx_access/prx_hyper_resource.rb', line 99

def api_resource(body, root = cms_root)
  href = body['_links']['self']['href']
  resource = api(root: root)
  link = PRXHyperResource::Link.new(resource, href: href)
  PRXHyperResource.new_from(body: body, resource: resource, link: link)
end

#default_headersObject



82
83
84
85
86
87
# File 'lib/prx_access/prx_hyper_resource.rb', line 82

def default_headers
  {
    'Content-Type' => 'application/json',
    'Accept' =>  'application/json'
  }
end

#get_account_token(account, scope = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/prx_access/prx_hyper_resource.rb', line 106

def (, scope = nil)
  id = ENV['PRX_CLIENT_ID']
  se = ENV['PRX_SECRET']

  raise 'Missing PRX_CLIENT_ID PRX_SECRET ENV vars' if id.nil? || se.nil?
  raise 'Missing ID_HOST ENV var' if ENV['ID_HOST'].nil?

  oauth_options = { site: id_root, token_url: '/token' }
  client = OAuth2::Client.new(id, se, oauth_options) do |faraday|
    faraday.request  :url_encoded
    faraday.adapter  :excon
  end
  if scope
    client.client_credentials.get_token(account: , scope: scope).token
  else
    client.client_credentials.get_token(account: ).token
  end
end

#id_rootObject



125
126
127
# File 'lib/prx_access/prx_hyper_resource.rb', line 125

def id_root
  root_uri ENV['ID_HOST']
end