Class: Aspire::API::LinkedData
- Defined in:
- lib/aspire/api/linked_data.rb
Overview
A wrapper class for the Aspire linked data API
Constant Summary collapse
- TENANCY_DOMAIN =
The tenancy domain
'myreadinglists.org'.freeze
Constants inherited from Base
Base::ASPIRE_AUTH_DOMAIN, Base::ASPIRE_DOMAIN, Base::SCHEME, Base::SSL_OPTS, Base::TALIS_DOMAIN
Instance Attribute Summary collapse
-
#linked_data_root ⇒ URI
The root URI of linked data URIs.
-
#tenancy_host_aliases ⇒ Array<String>
The list of non-canonical tenancy host names.
-
#tenancy_root ⇒ URI
The canonical root URI of the tenancy.
Attributes inherited from Base
#logger, #ssl, #tenancy_code, #timeout
Instance Method Summary collapse
-
#api_url(path) ⇒ String
Returns a full Aspire tenancy URL from a partial resource path.
-
#call(url) {|response, data| ... } ⇒ Hash
Returns parsed JSON data for a URI using the Aspire linked data API.
-
#canonical_host ⇒ String
Returns the canonical host name for an Aspire tenancy.
-
#canonical_url(url) ⇒ String?
Converts an Aspire tenancy alias or URL to canonical form.
-
#initialize(tenancy_code, **opts) ⇒ void
constructor
Initialises a new LinkedData instance.
-
#linked_data_host ⇒ String
Returns the linked data URI host name.
-
#linked_data_url(url) ⇒ String?
Converts an Aspire URL to the form used in linked data APIs.
-
#tenancy_host ⇒ String
Returns the canonical tenancy host name.
-
#valid_host?(host) ⇒ Boolean
Returns true if host is a valid tenancy hostname.
-
#valid_url?(url) ⇒ Boolean
Returns true if URL is a valid tenancy URL or host.
Constructor Details
#initialize(tenancy_code, **opts) ⇒ void
Initialises a new LinkedData instance
34 35 36 37 38 39 |
# File 'lib/aspire/api/linked_data.rb', line 34 def initialize(tenancy_code, **opts) super(tenancy_code, **opts) self.linked_data_root = opts[:linked_data_root] self.tenancy_host_aliases = opts[:tenancy_host_aliases] self.tenancy_root = opts[:tenancy_root] end |
Instance Attribute Details
#linked_data_root ⇒ URI
Returns the root URI of linked data URIs.
14 15 16 |
# File 'lib/aspire/api/linked_data.rb', line 14 def linked_data_root @linked_data_root end |
#tenancy_host_aliases ⇒ Array<String>
Returns the list of non-canonical tenancy host names.
18 19 20 |
# File 'lib/aspire/api/linked_data.rb', line 18 def tenancy_host_aliases @tenancy_host_aliases end |
#tenancy_root ⇒ URI
Returns the canonical root URI of the tenancy.
22 23 24 |
# File 'lib/aspire/api/linked_data.rb', line 22 def tenancy_root @tenancy_root end |
Instance Method Details
#api_url(path) ⇒ String
Returns a full Aspire tenancy URL from a partial resource path
44 45 46 |
# File 'lib/aspire/api/linked_data.rb', line 44 def api_url(path) path.include?('//') ? path : "#{tenancy_root}/#{path}" end |
#call(url) {|response, data| ... } ⇒ Hash
Returns parsed JSON data for a URI using the Aspire linked data API
56 57 58 59 60 61 62 63 |
# File 'lib/aspire/api/linked_data.rb', line 56 def call(url) url = api_url(url) url = "#{url}.json" unless url.end_with?('.json') = (url) response, data = call_api(**) yield(response, data) if block_given? data end |
#canonical_host ⇒ String
Returns the canonical host name for an Aspire tenancy
67 68 69 |
# File 'lib/aspire/api/linked_data.rb', line 67 def canonical_host "#{tenancy_code}.#{TENANCY_DOMAIN}" end |
#canonical_url(url) ⇒ String?
Converts an Aspire tenancy alias or URL to canonical form
75 76 77 78 79 |
# File 'lib/aspire/api/linked_data.rb', line 75 def canonical_url(url) # Set the canonical host name and add the default format extension if # required rewrite_url(url, tenancy_host) end |
#linked_data_host ⇒ String
Returns the linked data URI host name
83 84 85 |
# File 'lib/aspire/api/linked_data.rb', line 83 def linked_data_host linked_data_root.host end |
#linked_data_url(url) ⇒ String?
Converts an Aspire URL to the form used in linked data APIs
99 100 101 102 |
# File 'lib/aspire/api/linked_data.rb', line 99 def linked_data_url(url) # Set the linked data URI host name and remove any format extension rewrite_url(url, linked_data_host, '') end |
#tenancy_host ⇒ String
Returns the canonical tenancy host name
106 107 108 |
# File 'lib/aspire/api/linked_data.rb', line 106 def tenancy_host tenancy_root.host end |
#valid_host?(host) ⇒ Boolean
Returns true if host is a valid tenancy hostname
138 139 140 141 142 |
# File 'lib/aspire/api/linked_data.rb', line 138 def valid_host?(host) return false if host.nil? host = host.host if host.is_a?(URI) host == tenancy_host || tenancy_host_aliases.include?(host) end |
#valid_url?(url) ⇒ Boolean
Returns true if URL is a valid tenancy URL or host
147 148 149 150 151 |
# File 'lib/aspire/api/linked_data.rb', line 147 def valid_url?(url) url.nil? ? false : valid_host?(uri(url)) rescue URI::InvalidComponentError, URI::InvalidURIError false end |