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
-
#content ⇒ Hash
Content.
-
#created ⇒ String
Created (UTC datetime).
-
#get(uuid: nil, id: nil, rendering: :xml_long) ⇒ Hash
(also: #find)
Get.
-
#initialize(api: nil, endpoint: nil, username: nil, password: nil, bleeding: true, basic_auth: nil) ⇒ Resource
constructor
A new instance of Resource.
-
#metadata ⇒ Hash
All metadata.
-
#modified ⇒ String
Modified (UTC datetime).
-
#set_content(content) ⇒ Object
Set content.
-
#uuid ⇒ String
UUID.
Constructor Details
#initialize(api: nil, endpoint: 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 |
# File 'lib/puree/resource.rb', line 15 def initialize( api: nil, endpoint: nil, username: nil, password: nil, bleeding: true, basic_auth: nil) @resource_type = api @api_map = Puree::Map.new.get @endpoint = endpoint.nil? ? Puree.endpoint : endpoint @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 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
#content ⇒ Hash
Content
115 116 117 |
# File 'lib/puree/resource.rb', line 115 def content @content ? @content : {} end |
#created ⇒ String
Created (UTC datetime)
122 123 124 125 |
# File 'lib/puree/resource.rb', line 122 def created path = '/created' xpath_query_for_single_value path end |
#get(uuid: nil, id: nil, rendering: :xml_long) ⇒ Hash Also known as: find
Get
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 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 37 def get(uuid: nil, id: nil, rendering: :xml_long) reset @options = { basic_auth: @basic_auth, latest_api: @latest_api, resource_type: @resource_type.to_sym, rendering: rendering, uuid: uuid, 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 @endpoint = @endpoint.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) @doc = Nokogiri::XML @response.body @doc.remove_namespaces! rescue HTTP::Error => e puts 'HTTP::Error '+ e. end get_data? ? : {} end |
#metadata ⇒ Hash
All metadata
146 147 148 149 150 151 152 |
# File 'lib/puree/resource.rb', line 146 def o = {} o['uuid'] = uuid o['created'] = created o['modified'] = modified o end |
#modified ⇒ String
Modified (UTC datetime)
130 131 132 133 |
# File 'lib/puree/resource.rb', line 130 def modified path = '/modified' xpath_query_for_single_value path end |
#set_content(content) ⇒ Object
Set content
104 105 106 107 108 109 110 |
# File 'lib/puree/resource.rb', line 104 def set_content(content) if !content.nil? && !content.empty? @content = content else @content = {} end end |
#uuid ⇒ String
UUID
138 139 140 141 |
# File 'lib/puree/resource.rb', line 138 def uuid path = '/@uuid' xpath_query_for_single_value path end |