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, Download, Event, Journal, Organisation, Person, Project, Publication, Publisher
Instance Method Summary collapse
-
#content ⇒ Hash
Content.
-
#created ⇒ String
Created (UTC datetime).
-
#get(uuid: nil, id: nil) ⇒ HTTParty::Response
(also: #find)
Get.
-
#initialize(api: nil, endpoint: nil, username: nil, password: nil) ⇒ Resource
constructor
A new instance of Resource.
-
#metadata ⇒ Hash
All metadata.
-
#modified ⇒ String
Modified (UTC datetime).
-
#response ⇒ HTTParty::Response, Nil
Response, if get method has been called.
-
#set_content(content) ⇒ Object
Set content.
-
#uuid ⇒ String
UUID.
Constructor Details
#initialize(api: nil, endpoint: nil, username: nil, password: nil) ⇒ Resource
Returns a new instance of Resource.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/puree/resource.rb', line 11 def initialize( api: nil, endpoint: nil, username: nil, password: nil) @resource_type = api @api_map = Puree::Map.new.get @endpoint = endpoint.nil? ? Puree::Configuration.endpoint : endpoint @username = username.nil? ? Puree::Configuration.username : username @password = password.nil? ? Puree::Configuration.password : password end |
Instance Method Details
#content ⇒ Hash
Content
98 99 100 |
# File 'lib/puree/resource.rb', line 98 def content @content ? @content : {} end |
#created ⇒ String
Created (UTC datetime)
105 106 107 108 |
# File 'lib/puree/resource.rb', line 105 def created data = node 'created' !data.nil? && !data.empty? ? data.strip : '' end |
#get(uuid: nil, id: nil) ⇒ HTTParty::Response Also known as: find
Get
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/puree/resource.rb', line 27 def get(uuid: nil, id: nil) missing = missing_credentials if !missing.empty? missing.each do |m| puts "#{self.class.name}" + '#' + "#{__method__} missing #{m}" end exit end # strip any trailing slash @endpoint = @endpoint.sub(/(\/)+$/, '') @auth = Base64::strict_encode64(@username + ':' + @password) @options = { 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'] = @options[:rendering] if @options[:uuid] query['uuids.uuid'] = @options[:uuid] else if @options[:id] query['pureInternalIds.id'] = @options[:id] end end begin @response = HTTParty.get(build_url, query: query, headers: headers, timeout: 120) rescue HTTParty::Error => e puts 'HttParty::Error '+ e. end if get_data? response_name = service_response_name content = @response.parsed_response[response_name]['result']['content'] set_content(content) end @response end |
#metadata ⇒ Hash
All metadata
129 130 131 132 133 134 135 |
# File 'lib/puree/resource.rb', line 129 def o = {} o['uuid'] = uuid o['created'] = created o['modified'] = modified o end |
#modified ⇒ String
Modified (UTC datetime)
113 114 115 116 |
# File 'lib/puree/resource.rb', line 113 def modified data = node 'modified' !data.nil? && !data.empty? ? data.strip : '' end |
#response ⇒ HTTParty::Response, Nil
Response, if get method has been called
80 81 82 |
# File 'lib/puree/resource.rb', line 80 def response @response ? @response : nil end |
#set_content(content) ⇒ Object
Set content
87 88 89 90 91 92 93 |
# File 'lib/puree/resource.rb', line 87 def set_content(content) if !content.nil? && !content.empty? @content = content else @content = {} end end |
#uuid ⇒ String
UUID
121 122 123 124 |
# File 'lib/puree/resource.rb', line 121 def uuid data = node 'uuid' !data.nil? && !data.empty? ? data.strip : '' end |