Module: RestfulObjects::LinkGenerator
- Included in:
- ActionDescription, CollectionDescription, DomainModel, DomainType, Object, ParameterDescription, PropertyDescription, Service, User
- Defined in:
- lib/restful_objects/domain_model/helpers/link_generator.rb
Constant Summary collapse
- HTTP_OK =
200
Instance Method Summary collapse
- #generate_rel(rel, options = {}) ⇒ Object
- #generate_repr_type(type, domain_type = nil, element_type = nil) ⇒ Object
- #link_to(rel, href, type, options = {}) ⇒ Object
- #ro_content_type_for_object(domain_type) ⇒ Object
- #ro_content_type_for_object_collection(element_type) ⇒ Object
- #ro_content_type_for_property ⇒ Object
- #underscore_to_hyphen_string(symbol) ⇒ Object
Instance Method Details
#generate_rel(rel, options = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/restful_objects/domain_model/helpers/link_generator.rb', line 16 def generate_rel(rel, = {}) if [:self, :up, :next, :previous, :icon, :help].include?(rel) rel.to_s elsif rel == :described_by 'describedby' else if [:action, :action_param, :collection, :delete, :domain_type, :domain_types, :element, :element_type, :persist, :property, :return_type, :services, :update, :user, :version].include?(rel) 'urn:org.restfulobjects:rels/' + underscore_to_hyphen_string(rel) else 'urn:org.restfulobjects:rels/' + case rel when :add_to 'add-to;collection="' + [:collection] + '"' when :attachment 'attachment;property="' + [:property] + '"' when :choice if [:property] 'choice;property="' + [:property] + '"' elsif [:action] 'choice;action="' + [:action] + '"' elsif [:param] 'choice;param="' + [:param] + '"' else raise 'option not found for choice rel' end when :clear 'clear;property="' + [:property] + '"' when :details if [:property] 'details;property="' + [:property] + '"' elsif [:collection] 'details;collection="' + [:collection] + '"' elsif [:action] 'details;action="' + [:action] + '"' else raise 'option not found for details rel' end when :invoke if [:action] 'invoke;action="' + [:action] + '"' elsif [:type_action] 'invoke;typeaction="' + [:type_action] + '"' else raise 'option not found for invoke rel' end when :modify 'modify;property="' + [:property] + '"' when :remove_from 'remove-from;collection="' + [:collection] + '"' when :service 'service;serviceId="' + [:service_id] + '"' when :value if [:property] 'value;property="' + [:property] + '"' elsif [:collection] 'value;collection="' + [:collection] + '"' else raise 'option not found for value rel' end else raise "rel invalid: #{rel}" end end end end |
#generate_repr_type(type, domain_type = nil, element_type = nil) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/restful_objects/domain_model/helpers/link_generator.rb', line 83 def generate_repr_type(type, domain_type = nil, element_type = nil) valid_repr = [:homepage, :user, :version, :list, :object, :object_property, :object_collection, :object_action, :object_result, :action_result, :type_list, :domain_type, :property_description, :collection_description, :action_description, :action_param_description, :type_action_result, :error, :services] raise "repr-type invalid: #{type.to_s}" if not valid_repr.include?(type) repr = 'application/json;profile="urn:org.restfulobjects:repr-types/' + underscore_to_hyphen_string(type) + '"' if domain_type && [:object, :action].include?(type) repr += ';x-ro-domain-type="' + domain_type + '"' elsif element_type && [:object_collection, :action].include?(type) repr +=';x-ro-element-type="' + element_type + '"' end repr end |
#link_to(rel, href, type, options = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/restful_objects/domain_model/helpers/link_generator.rb', line 4 def link_to(rel, href, type, = {}) link = { 'rel' => generate_rel(rel, ), 'href' => RestfulObjects::DomainModel.current.base_url + href, 'type' => generate_repr_type(type, [:domain_type], [:element_type]), 'method' => [:method] || 'GET' } link['arguments'] = [:arguments] if [:arguments] link end |
#ro_content_type_for_object(domain_type) ⇒ Object
105 106 107 |
# File 'lib/restful_objects/domain_model/helpers/link_generator.rb', line 105 def ro_content_type_for_object(domain_type) "application/json;profile=\"urn:org.restfulobjects:repr-types/object\";x-ro-domain-type=\"#{domain_type}\"" end |
#ro_content_type_for_object_collection(element_type) ⇒ Object
113 114 115 |
# File 'lib/restful_objects/domain_model/helpers/link_generator.rb', line 113 def ro_content_type_for_object_collection(element_type) "application/json;profile=\"urn:org.restfulobjects:repr-types/object-collection\";x-ro-element-type=\"#{element_type}\"" end |
#ro_content_type_for_property ⇒ Object
109 110 111 |
# File 'lib/restful_objects/domain_model/helpers/link_generator.rb', line 109 def ro_content_type_for_property "application/json;profile=\"urn:org.restfulobjects:repr-types/object-property\"" end |
#underscore_to_hyphen_string(symbol) ⇒ Object
101 102 103 |
# File 'lib/restful_objects/domain_model/helpers/link_generator.rb', line 101 def underscore_to_hyphen_string(symbol) symbol.to_s.sub('_', '-') end |