Class: Puree::Resource
- Inherits:
-
Object
- Object
- Puree::Resource
- Defined in:
- lib/puree/resource.rb
Overview
Abstract base class for resources.
Direct Known Subclasses
Collection, Dataset, Journal, Organisation, Person, Project, Publication, Publisher
Instance Method Summary collapse
-
#content ⇒ Hash
Content.
-
#get(endpoint: nil, username: nil, password: nil, uuid: nil, id: nil) ⇒ HTTParty::Response
Get.
-
#initialize(resource_type) ⇒ Resource
constructor
A new instance of Resource.
-
#response ⇒ HTTParty::Response, Nil
Response, if get method has been called.
-
#set_content(content) ⇒ Object
Set content.
Constructor Details
#initialize(resource_type) ⇒ Resource
Returns a new instance of Resource.
7 8 9 10 |
# File 'lib/puree/resource.rb', line 7 def initialize(resource_type) @resource_type = resource_type @api_map = Puree::Map.new.get end |
Instance Method Details
#content ⇒ Hash
Content
78 79 80 |
# File 'lib/puree/resource.rb', line 78 def content @content ? @content : {} end |
#get(endpoint: nil, username: nil, password: nil, uuid: nil, id: nil) ⇒ HTTParty::Response
Get
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/puree/resource.rb', line 20 def get(endpoint:nil, username:nil, password:nil, uuid:nil, id:nil) # strip any trailing slash @endpoint = endpoint.sub(/(\/)+$/, '') @auth = Base64::strict_encode64(username + ':' + password) = { latest_api: true, resource_type: @resource_type.to_sym, rendering: :xml_long, uuid: uuid, id: id, } headers = { 'Accept' => 'application/xml', 'Authorization' => 'Basic ' + @auth } query = {} query['rendering'] = [:rendering] if [:uuid] query['uuids.uuid'] = [:uuid] else if [:id] query['pureInternalIds.id'] = [:id] end end @response = HTTParty.get(url, query: query, headers: headers) if get_data? response_name = service_response_name content = @response.parsed_response[response_name]['result']['content'] set_content(content) end @response end |
#response ⇒ HTTParty::Response, Nil
Response, if get method has been called
60 61 62 |
# File 'lib/puree/resource.rb', line 60 def response @response ? @response : nil end |
#set_content(content) ⇒ Object
Set content
67 68 69 70 71 72 73 |
# File 'lib/puree/resource.rb', line 67 def set_content(content) if !content.nil? && !content.empty? @content = content else @content = {} end end |