Module: Aspire::Util

Included in:
Caching::CacheEntry, Caching::Util, Object::Base, Object::Factory
Defined in:
lib/aspire/util.rb

Overview

Utility methods mixin

Constant Summary collapse

LD_API_URI =

Regular expression to parse a Linked Data API URI

Regexp.new('https?://(?<tenancy_host>[^/]*)/' \
'(?<type>[^/]*)/' \
'(?<id>[^/\.]*)' \
'(\.(?<format>[^/]*))?' \
'(/' \
'(?<child_type>[^/.]*)' \
'(/(?<child_id>[^/\.]*))?' \
'(\.(?<child_format>[^/]*))?' \
')?(?<rest>.*)').freeze

Instance Method Summary collapse

Instance Method Details

#child_url?(url1, url2, api = nil, strict: false) ⇒ Boolean

Returns true if the first URL is the child of the second URL

Parameters:

Returns:

  • (Boolean)

    true if the URL is a child of the cache entry, false otherwise



23
24
25
# File 'lib/aspire/util.rb', line 23

def child_url?(url1, url2, api = nil, strict: false)
  parent_url?(url2, url1, api, strict: strict)
end

#duration(benchmark_time) ⇒ String

Returns a HH:MM:SS string given a Benchmark time

Parameters:

  • benchmark_time (Benchmark:Tms)

    the Benchmark time object

Returns:

  • (String)

    the HH:HM:SS string



30
31
32
33
34
# File 'lib/aspire/util.rb', line 30

def duration(benchmark_time)
  secs = benchmark_time.real
  hours = secs / 3600
  format('%2.2d:%2.2d:%2.2d', hours, hours % 60, secs % 60)
end

#id_from_uri(u, parsed: nil) ⇒ String

Returns the ID of an object from its URL

Parameters:

  • u (String)

    the URL of the API object

Returns:

  • (String)

    the object ID



39
40
41
42
# File 'lib/aspire/util.rb', line 39

def id_from_uri(u, parsed: nil)
  parsed ||= parse_url(u)
  parsed[:id]
end

#item?(uri) ⇒ Boolean

Returns true if URI is a list item

Parameters:

  • uri (String)

    the URI

Returns:

  • (Boolean)

    true if the URI is a list item, otherwise false



47
48
49
# File 'lib/aspire/util.rb', line 47

def item?(uri)
  uri.include?('/items/')
end

#linked_data(uri, ld) ⇒ Hash

Returns the data for a URI from a parsed linked data API response which may contain multiple objects

Parameters:

  • uri (String)

    the URI of the object

  • ld (Hash)

    the parsed JSON data from the Aspire linked data API

Returns:

  • (Hash)

    the parsed JSON data for the URI



56
57
58
59
60
61
62
63
64
65
# File 'lib/aspire/util.rb', line 56

def linked_data(uri, ld)
  uri = linked_data_path(uri)
  return nil unless uri && ld
  # The URI used to retrieve the data may be the canonical URI or a
  # tenancy aliases. We ignore the host part of the URIs and match just
  # the path
  ld.each { |u, data| return data if uri == linked_data_path(u) }
  # No match was found
  nil
end

#linked_data_path(uri) ⇒ String?

Returns the path of a URI

Parameters:

  • uri (String)

    the URI

Returns:

  • (String, nil)

    the URI path or nil if invalid



70
71
72
73
74
# File 'lib/aspire/util.rb', line 70

def linked_data_path(uri)
  URI.parse(uri).path
rescue URI::InvalidComponentError, URI::InvalidURIError
  nil
end

#list?(uri) ⇒ Boolean

Returns true if URI is a list

Parameters:

  • uri (String)

    the URI

Returns:

  • (Boolean)

    true if the URI is a list, otherwise false



79
80
81
# File 'lib/aspire/util.rb', line 79

def list?(uri)
  uri.include?('/lists/')
end

#list_url?(u = nil, parsed: nil) ⇒ Boolean

Returns true if a URL is a list URL, false otherwise

Parameters:

  • u (String) (defaults to: nil)

    the URL of the API object

Returns:

  • (Boolean)

    true if the URL is a list URL, false otherwise



86
87
88
89
90
91
# File 'lib/aspire/util.rb', line 86

def list_url?(u = nil, parsed: nil)
  return false if (u.nil? || u.empty?) && parsed.nil?
  parsed ||= parse_url(u)
  child_type = parsed[:child_type]
  parsed[:type] == 'lists' && (child_type.nil? || child_type.empty?)
end

#module?(uri) ⇒ Boolean

Returns true if URI is a module

Parameters:

  • uri (String)

    the URI

Returns:

  • (Boolean)

    true if the URI is a module, otherwise false



96
97
98
# File 'lib/aspire/util.rb', line 96

def module?(uri)
  uri.include?('/modules/')
end

#parent_url?(url1, url2, api = nil, strict: false) ⇒ Boolean

Returns true if the first URL is the parent of the second URL

Parameters:

Returns:

  • (Boolean)

    true if the URL has the same parent as this entry



108
109
110
111
112
113
114
115
116
117
# File 'lib/aspire/util.rb', line 108

def parent_url?(url1, url2, api = nil, strict: false)
  u1 = url_for_comparison(url1, api, parsed: true)
  u2 = url_for_comparison(url2, api, parsed: true)
  # Both URLs must have the same parent
  return false unless u1[:type] == u2[:type] && u1[:id] == u2[:id]
  # Non-strict comparison requires only the same parent object
  return true unless strict
  # Strict comparison requires that this entry is a child of the URL
  u1[:child_type].nil? && !u2[:child_type].nil? ? true : false
end

#parse_url(url) ⇒ MatchData?

Returns the components of an object URL

Parameters:

  • url (String)

    the object URL

Returns:

  • (MatchData, nil)

    the URI components:

    tenancy_host: tenancy root (server name),
    type: type of primary object,
    id: ID of primary object,
    child_type: type of child object,
    child_id: ID of child object
    



129
130
131
# File 'lib/aspire/util.rb', line 129

def parse_url(url)
  url ? LD_API_URI.match(url) : nil
end

#resource?(uri) ⇒ Boolean

Returns true if URI is a resource

Parameters:

  • uri (String)

    the URI

Returns:

  • (Boolean)

    true if the URI is a resource, otherwise false



136
137
138
# File 'lib/aspire/util.rb', line 136

def resource?(uri)
  uri.include?('/resources/')
end

#section?(uri) ⇒ Boolean

Returns true if URI is a section

Parameters:

  • uri (String)

    the URI

Returns:

  • (Boolean)

    true if the URI is a section, otherwise false



143
144
145
# File 'lib/aspire/util.rb', line 143

def section?(uri)
  uri.include?('/sections/')
end

#url_for_comparison(url, api = nil, parsed: false) ⇒ Aspire::Caching::CacheEntry, String

Returns a parsed or unparsed URL for comparison

Parameters:

  • url (Aspire::Caching::CacheEntry, String)

    the URL

  • api (Aspire::API::LinkedData) (defaults to: nil)

    the API for generating canonical URLs

  • parsed (Boolean) (defaults to: false)

    if true, return a parsed URL, otherwise return an unparsed URL string

Returns:



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/aspire/util.rb', line 153

def url_for_comparison(url, api = nil, parsed: false)
  if url.is_a?(MatchData) && parsed
    url
  elsif parsed && url.respond_to?(:parsed_url)
    url.parsed_url
  elsif !parsed && url.respond_to?(url)
    url.url
  else
    result = api.nil? ? url.to_s : api.canonical_url(url.to_s)
    parsed ? parse_url(result) : result
  end
end

#url_pathObject

Returns the path from the URL as a relative filename



167
168
169
170
171
172
173
174
175
176
# File 'lib/aspire/util.rb', line 167

def url_path
  # Get the path component of the URL as a relative path
  filename = URI.parse(url).path
  filename.slice!(0) # Remove the leading /
  # Return the path with '.json' extension if not already present
  filename.end_with?('.json') ? filename : "#{filename}.json"
rescue URI::InvalidComponentError, URI::InvalidURIError
  # Return nil if the URL is invalid
  nil
end

#user?(uri) ⇒ Boolean

Returns true if URI is a section

Parameters:

  • uri (String)

    the URI

Returns:

  • (Boolean)

    true if the URI is a section, otherwise false



181
182
183
# File 'lib/aspire/util.rb', line 181

def user?(uri)
  uri.include?('/users/')
end