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