Class: PeopleDoc::V2::Client
- Inherits:
-
Object
- Object
- PeopleDoc::V2::Client
- Defined in:
- lib/people_doc.rb
Overview
PeopleDoc REST API v2 Client
Instance Method Summary collapse
- #encoded_credentials ⇒ Object
-
#get(resource) ⇒ Hash
Get a resource Makes a request for a resource from PeopleDoc and returns the response as a raw Hash.
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
-
#post_file(resource, file, payload = nil) ⇒ Hash
POST a file …
-
#put(resource, payload) ⇒ Hash
PUT a resource Makes a request to PUT new or existing resource details to PeopleDoc.
-
#token ⇒ Token
OAuth token Performs authentication using client credentials against the PeopleDoc Api.
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
193 194 195 196 197 198 199 200 201 202 |
# File 'lib/people_doc.rb', line 193 def initialize( = {}) = .merge() @application_id = .fetch(:application_id) @application_secret = .fetch(:application_secret) @base_url = .fetch(:base_url) @client_id = .fetch(:client_id) @logger = .fetch(:logger, Logger.new(STDOUT)) @request = HTTPartyRequest.new(@base_url, @logger, response_handlers) end |
Instance Method Details
#encoded_credentials ⇒ Object
204 205 206 |
# File 'lib/people_doc.rb', line 204 def encoded_credentials EncodedCredentials.new(@application_id, @application_secret).call end |
#get(resource) ⇒ Hash
Get a resource Makes a request for a resource from PeopleDoc and returns the response as a raw Hash.
244 245 246 247 248 249 |
# File 'lib/people_doc.rb', line 244 def get(resource) @request .get(base_headers, "api/v2/client/#{resource}") rescue NotFound nil end |
#post_file(resource, file, payload = nil) ⇒ Hash
POST a file …
259 260 261 262 263 264 265 266 267 268 |
# File 'lib/people_doc.rb', line 259 def post_file(resource, file, payload = nil) @request.post_file( base_headers.merge( 'Content-Type' => 'multipart/form-data' ), "api/v2/#{resource}", file: file, data: payload ? payload.to_json : nil ) end |
#put(resource, payload) ⇒ Hash
PUT a resource Makes a request to PUT new or existing resource details to PeopleDoc.
277 278 279 280 281 282 283 |
# File 'lib/people_doc.rb', line 277 def put(resource, payload) @request.put( base_headers, "api/v2/client/#{resource}", payload.to_json ) end |
#token ⇒ Token
OAuth token Performs authentication using client credentials against the PeopleDoc Api.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/people_doc.rb', line 214 def token return @token if @token payload = { client_id: @client_id, grant_type: 'client_credentials', scope: 'client' } headers = { 'Accept' => 'application/json', 'Authorization' => "Basic #{encoded_credentials}", 'Content-Type' => 'application/x-www-form-urlencoded', 'Host' => uri.host, 'User-Agent' => 'PeopleDoc::V2::Client' } response = @request.post(headers, 'api/v2/client/tokens', payload) @token = Token.new( *response.values_at('access_token', 'token_type', 'expires_in') ) end |