Module: Occi::Api::Client::Base::Helpers
- Included in:
- ClientBase
- Defined in:
- lib/occi/api/client/base/helpers.rb
Instance Method Summary collapse
-
#path_for_instance(instance) ⇒ String
Returns the path for a given instance, instances not providing path information will raise an exception.
-
#path_for_kind_type_identifier(kind_type_identifier) ⇒ String
Returns the path for a given kind type identifier.
-
#sanitize_instance_link(instance_link) ⇒ String
Extracts path from an instance link.
Instance Method Details
#path_for_instance(instance) ⇒ String
Returns the path for a given instance, instances not providing path information will raise an exception.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/occi/api/client/base/helpers.rb', line 50 def path_for_instance(instance) unless instance.respond_to?(:location) raise Occi::Api::Client::Errors::TypeMismatchError, "Expected an instance responding to #location, " \ "got #{instance.class.name.inspect}" end if instance.location.blank? raise Occi::Api::Client::Errors::LocationError, "Instance of #{instance.class.name.inspect} has " \ "an empty location" end instance.location end |
#path_for_kind_type_identifier(kind_type_identifier) ⇒ String
Returns the path for a given kind type identifier
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/occi/api/client/base/helpers.rb', line 16 def path_for_kind_type_identifier(kind_type_identifier) raise ArgumentError, "Kind type identifier is a required argument!" if kind_type_identifier.blank? if kind_type_identifier.start_with?(@endpoint.to_s) || kind_type_identifier.start_with?('/') #we got an instance link return sanitize_instance_link(kind_type_identifier) end kind_type_id = get_kind_type_identifier(kind_type_identifier) unless kind_type_id raise ArgumentError, "There is no such kind type registered in the model! #{kind_type_identifier.inspect}" end kinds = @model.kinds.select { |kind| kind.type_identifier == kind_type_id } path_for_instance(kinds.first) end |
#sanitize_instance_link(instance_link) ⇒ String
Extracts path from an instance link. It will remove the leading @endpoint and replace it with a slash.
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/occi/api/client/base/helpers.rb', line 77 def sanitize_instance_link(instance_link) # everything starting with '/' is considered to be a resource path return instance_link if instance_link.start_with? '/' unless instance_link.start_with?(@endpoint.to_s) raise ArgumentError, "Resource link #{instance_link.inspect} is not valid!" end URI(instance_link).request_uri end |