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
-
#child_url?(url1, url2, api = nil, strict: false) ⇒ Boolean
Returns true if the first URL is the child of the second URL.
-
#duration(benchmark_time) ⇒ String
Returns a HH:MM:SS string given a Benchmark time.
-
#id_from_uri(u, parsed: nil) ⇒ String
Returns the ID of an object from its URL.
-
#item?(uri) ⇒ Boolean
Returns true if URI is a list item.
-
#linked_data(uri, ld) ⇒ Hash
Returns the data for a URI from a parsed linked data API response which may contain multiple objects.
-
#linked_data_path(uri) ⇒ String?
Returns the path of a URI.
-
#list?(uri) ⇒ Boolean
Returns true if URI is a list.
-
#list_url?(u = nil, parsed: nil) ⇒ Boolean
Returns true if a URL is a list URL, false otherwise.
-
#module?(uri) ⇒ Boolean
Returns true if URI is a module.
-
#parent_url?(url1, url2, api = nil, strict: false) ⇒ Boolean
Returns true if the first URL is the parent of the second URL.
-
#parse_url(url) ⇒ MatchData?
Returns the components of an object URL.
-
#resource?(uri) ⇒ Boolean
Returns true if URI is a resource.
-
#section?(uri) ⇒ Boolean
Returns true if URI is a section.
-
#url_for_comparison(url, api = nil, parsed: false) ⇒ Aspire::Caching::CacheEntry, String
Returns a parsed or unparsed URL for comparison.
-
#url_path ⇒ Object
Returns the path from the URL as a relative filename.
-
#user?(uri) ⇒ Boolean
Returns true if URI is a section.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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_path ⇒ Object
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
181 182 183 |
# File 'lib/aspire/util.rb', line 181 def user?(uri) uri.include?('/users/') end |