Class: Aspire::API::LinkedData

Inherits:
Base
  • Object
show all
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

Attributes inherited from Base

#logger, #ssl, #tenancy_code, #timeout

Instance Method Summary collapse

Constructor Details

#initialize(tenancy_code, **opts) ⇒ void

Initialises a new LinkedData instance

Parameters:

  • tenancy_code (String)

    the Aspire tenancy code

  • opts (Hash)

    the options hash

Options Hash (**opts):

  • :linked_data_root (String)

    the root URI of linked data URIs usually ‘http://<tenancy-code>.myreadinglists.org’

  • :tenancy_host_aliases (Array<String>)

    the list of host name aliases for the tenancy

  • :tenancy_root (String)

    the canonical root URI of the tenancy, usually ‘http://<tenancy-code>.rl.talis.com’



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_rootURI

Returns the root URI of linked data URIs.

Returns:

  • (URI)

    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_aliasesArray<String>

Returns the list of non-canonical tenancy host names.

Returns:

  • (Array<String>)

    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_rootURI

Returns the canonical root URI of the tenancy.

Returns:

  • (URI)

    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

Parameters:

  • path (String)

    the partial resource path

Returns:

  • (String)

    the full tenancy URL



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

Parameters:

  • url (String)

    the partial (minus the tenancy root) or complete tenancy URL of the resource

Yields:

  • (response, data)

    Passes the REST client response and parsed JSON hash to the block

Yield Parameters:

  • response (RestClient::Response)

    the REST client response

  • data (Hash)

    the parsed JSON data from the response

Returns:

  • (Hash)

    the parsed JSON content from the API response



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')
  rest_options = call_rest_options(url)
  response, data = call_api(**rest_options)
  yield(response, data) if block_given?
  data
end

#canonical_hostString

Returns the canonical host name for an Aspire tenancy

Returns:

  • (String)

    the canonical host name for the 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

Parameters:

  • url (String)

    an Aspire host name or URL

Returns:

  • (String, nil)

    the equivalent canonical host name or URL using the tenancy base URL, or nil if the host is not a valid tenancy alias



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_hostString

Returns the linked data URI host name

Returns:

  • (String)

    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

Parameters:

  • url (String)

    an Aspire URL

Returns:

  • (String, nil)

    the equivalent linked data URL



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_hostString

Returns the canonical tenancy host name

Returns:

  • (String)

    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

Parameters:

  • host (String, URI)

    the hostname

Returns:

  • (Boolean)

    true if the hostname is valid, false otherwise



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

Parameters:

  • url (String)

    the URL or host

Returns:

  • (Boolean)

    true if the URL or host is valid, false otherwise



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