Class: CloudKit::URI

Inherits:
Object show all
Defined in:
lib/cloudkit/uri.rb

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

Instance Method Summary collapse

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

#stringObject (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_stringObject

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_typeObject

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_fragmentObject

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

#componentsObject

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_uriObject

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

Returns:

  • (Boolean)


39
40
41
# File 'lib/cloudkit/uri.rb', line 39

def meta_uri?
  return components.size == 1 && components[0] == 'cloudkit-meta'
end

#resolved_resource_collection_uri?Boolean

Returns true if URI matches /collection/_resolved

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


59
60
61
# File 'lib/cloudkit/uri.rb', line 59

def version_collection_uri?
  return components.size == 3 && components[2] == 'versions'
end