Class: Puree::Resource
- Inherits:
-
Object
- Object
- Puree::Resource
- Defined in:
- lib/puree/resource.rb
Overview
Abstract base class for resources.
Direct Known Subclasses
Dataset, Event, Journal, Organisation, Person, Project, Publication, Publisher
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#created ⇒ String
Created (UTC datetime).
-
#get(uuid: nil, id: nil, rendering: :xml_long) ⇒ Hash
(also: #find)
Get.
-
#initialize(api: nil, base_url: nil, username: nil, password: nil, bleeding: true, basic_auth: nil) ⇒ Resource
constructor
A new instance of Resource.
-
#locale ⇒ String
Locale (e.g. en-GB).
-
#modified ⇒ String
Modified (UTC datetime).
-
#set_content(xml) ⇒ Object
Set content from XML.
-
#uuid ⇒ String
UUID.
Constructor Details
#initialize(api: nil, base_url: nil, username: nil, password: nil, bleeding: true, basic_auth: nil) ⇒ Resource
Returns a new instance of Resource.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/puree/resource.rb', line 15 def initialize( api: nil, base_url: nil, username: nil, password: nil, bleeding: true, basic_auth: nil) @resource_type = api @api_map = Puree::Map.new.get @base_url = base_url.nil? ? Puree.base_url : base_url @latest_api = bleeding @basic_auth = basic_auth.nil? ? Puree.basic_auth : basic_auth if @basic_auth === true @username = username.nil? ? Puree.username : username @password = password.nil? ? Puree.password : password end @metadata = {} @options = { basic_auth: @basic_auth, latest_api: @latest_api, resource_type: @resource_type.to_sym } end |
Instance Attribute Details
#response ⇒ Object (readonly)
Returns the value of attribute response.
7 8 9 |
# File 'lib/puree/resource.rb', line 7 def response @response end |
Instance Method Details
#created ⇒ String
Created (UTC datetime)
111 112 113 |
# File 'lib/puree/resource.rb', line 111 def created @metadata['created'] end |
#get(uuid: nil, id: nil, rendering: :xml_long) ⇒ Hash Also known as: find
Get
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/puree/resource.rb', line 43 def get(uuid: nil, id: nil, rendering: :xml_long) reset @options[:rendering] = rendering @options[:uuid] = uuid @options[:id] = id missing = missing_credentials if !missing.empty? missing.each do |m| puts "#{self.class.name}" + '#' + "#{__method__} missing #{m}" end exit end # strip any trailing slash @base_url = @base_url.sub(/(\/)+$/, '') headers = {} headers['Accept'] = 'application/xml' if @options[:basic_auth] === true @auth = Base64::strict_encode64(@username + ':' + @password) headers['Authorization'] = 'Basic ' + @auth end query = {} query['rendering'] = @options[:rendering] if @options[:uuid] query['uuids.uuid'] = @options[:uuid] else if @options[:id] query['pureInternalIds.id'] = @options[:id] end end if @options[:rendering] query['rendering'] = @options[:rendering] end begin url = build_url req = HTTP.headers accept: headers['Accept'] if @options[:basic_auth] req = req.auth headers['Authorization'] end @response = req.get(url, params: query) make_doc @response.body rescue HTTP::Error => e puts 'HTTP::Error '+ e. end get_data? ? : {} end |
#locale ⇒ String
Locale (e.g. en-GB)
125 126 127 |
# File 'lib/puree/resource.rb', line 125 def locale @metadata['locale'] end |
#modified ⇒ String
Modified (UTC datetime)
118 119 120 |
# File 'lib/puree/resource.rb', line 118 def modified @metadata['modified'] end |
#set_content(xml) ⇒ Object
Set content from XML. In order for metadata extraction to work, the XML must have been retrieved using the .current version of the Pure API endpoints
133 134 135 136 137 138 139 140 |
# File 'lib/puree/resource.rb', line 133 def set_content(xml) if xml make_doc xml if get_data? end end end |
#uuid ⇒ String
UUID
104 105 106 |
# File 'lib/puree/resource.rb', line 104 def uuid @metadata['uuid'] end |