Class: CloudKit::URI
Overview
A CloudKit::URI wraps a URI string, adding methods useful for routing in CloudKit as well as caching URI components for future comparisons.
Instance Attribute Summary collapse
-
#string ⇒ Object
readonly
The string form of a URI.
Instance Method Summary collapse
-
#cannonical_uri_string ⇒ Object
Returns a cannonical URI for a given URI/URI fragment, generating it if required.
-
#collection_type ⇒ Object
Return the resource collection referenced by a URI.
-
#collection_uri_fragment ⇒ Object
Return the resource collection URI fragment.
-
#components ⇒ Object
Splits a URI into its components.
-
#current_resource_uri ⇒ Object
Return the URI for the current version of a resource.
-
#initialize(string) ⇒ URI
constructor
Create a new URI with the given string.
-
#meta_uri? ⇒ Boolean
Returns true if URI matches /cloudkit-meta.
-
#resolved_resource_collection_uri? ⇒ Boolean
Returns true if URI matches /collection/_resolved.
-
#resolved_version_collection_uri? ⇒ Boolean
Returns true if URI matches /collection/uuid/versions/_resolved.
-
#resource_collection_uri? ⇒ Boolean
Returns true if URI matches /collection.
-
#resource_uri? ⇒ Boolean
Returns true if URI matches /collection/uuid.
-
#resource_version_uri? ⇒ Boolean
Returns true if URI matches /collection/uuid/versions/etag.
-
#version_collection_uri? ⇒ Boolean
Returns true if URI matches /collection/uuid/versions.
Constructor Details
#initialize(string) ⇒ URI
Create a new URI with the given string.
11 12 13 |
# File 'lib/cloudkit/uri.rb', line 11 def initialize(string) @string = string end |
Instance Attribute Details
#string ⇒ Object (readonly)
The string form of a URI.
8 9 10 |
# File 'lib/cloudkit/uri.rb', line 8 def string @string end |
Instance Method Details
#cannonical_uri_string ⇒ Object
Returns a cannonical URI for a given URI/URI fragment, generating it if required.
Example: URI.new('/items/123').cannoncal_uri_string => /items/123
Example: URI.new('/items').cannonical_uri_string => /items/some-new-uuid
78 79 80 81 82 83 84 85 86 |
# File 'lib/cloudkit/uri.rb', line 78 def cannonical_uri_string @cannonical_uri_string ||= if resource_collection_uri? "#{@string}/#{UUID.generate}" elsif resource_uri? @string else raise CloudKit::InvalidURIFormat end end |
#collection_type ⇒ Object
Return the resource collection referenced by a URI.
Example: URI.new('/foos/123').collection_type => :foos
28 29 30 |
# File 'lib/cloudkit/uri.rb', line 28 def collection_type components[0].to_sym rescue nil end |
#collection_uri_fragment ⇒ Object
Return the resource collection URI fragment.
Example: URI.new('/foos/123').collection_uri_fragment => '/foos
17 18 19 |
# File 'lib/cloudkit/uri.rb', line 17 def collection_uri_fragment "/#{components[0]}" rescue nil end |
#components ⇒ Object
Splits a URI into its components
22 23 24 |
# File 'lib/cloudkit/uri.rb', line 22 def components @components ||= @string.split('/').reject{|x| x == '' || x == nil} rescue [] end |
#current_resource_uri ⇒ Object
Return the URI for the current version of a resource.
Example: URI.new('/foos/123/versions/abc').current_resource_uri => '/foos/123'
34 35 36 |
# File 'lib/cloudkit/uri.rb', line 34 def current_resource_uri "/#{components[0..1].join('/')}" rescue nil end |
#meta_uri? ⇒ Boolean
Returns true if URI matches /cloudkit-meta
39 40 41 |
# File 'lib/cloudkit/uri.rb', line 39 def return components.size == 1 && components[0] == 'cloudkit-meta' end |
#resolved_resource_collection_uri? ⇒ Boolean
Returns true if URI matches /collection/_resolved
49 50 51 |
# File 'lib/cloudkit/uri.rb', line 49 def resolved_resource_collection_uri? return components.size == 2 && components[1] == '_resolved' end |
#resolved_version_collection_uri? ⇒ Boolean
Returns true if URI matches /collection/uuid/versions/_resolved
64 65 66 |
# File 'lib/cloudkit/uri.rb', line 64 def resolved_version_collection_uri? return components.size == 4 && components[2] == 'versions' && components[3] == '_resolved' end |
#resource_collection_uri? ⇒ Boolean
Returns true if URI matches /collection
44 45 46 |
# File 'lib/cloudkit/uri.rb', line 44 def resource_collection_uri? return components.size == 1 && components[0] != 'cloudkit-meta' end |
#resource_uri? ⇒ Boolean
Returns true if URI matches /collection/uuid
54 55 56 |
# File 'lib/cloudkit/uri.rb', line 54 def resource_uri? return components.size == 2 && components[1] != '_resolved' end |
#resource_version_uri? ⇒ Boolean
Returns true if URI matches /collection/uuid/versions/etag
69 70 71 |
# File 'lib/cloudkit/uri.rb', line 69 def resource_version_uri? return components.size == 4 && components[2] == 'versions' && components[3] != '_resolved' end |
#version_collection_uri? ⇒ Boolean
Returns true if URI matches /collection/uuid/versions
59 60 61 |
# File 'lib/cloudkit/uri.rb', line 59 def version_collection_uri? return components.size == 3 && components[2] == 'versions' end |