Module: JSONAPIonify::Api::Resource::Builders::ClassMethods
- Defined in:
- lib/jsonapionify/api/resource/builders.rb
Instance Method Summary collapse
- #build_collection(context, collection, fields:, include_cursors: false, &block) ⇒ Object
- #build_cursor_from_instance(context, instance) ⇒ Object
- #build_id(instance) ⇒ Object
- #build_identifier_collection(collection) ⇒ Object
- #build_relationship(context, instance, name, links: true, data: false) ⇒ Object
- #build_resource(context, instance, fields:, relationships: true, links: true, include_cursor: false, &block) ⇒ Object
- #build_resource_identifier(instance) ⇒ Object
- #build_url(context, instance = nil) ⇒ Object
Instance Method Details
#build_collection(context, collection, fields:, include_cursors: false, &block) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/jsonapionify/api/resource/builders.rb', line 71 def build_collection( context, collection, fields:, include_cursors: false, &block ) include_rel_param = context.params['include-relationships'] relationships = TRUTHY_STRINGS.include? include_rel_param collection.each_with_object( Structure::Collections::Resources.new ) do |instance, resources| resources << build_resource( context, instance, fields: fields, relationships: relationships, include_cursor: include_cursors, &block ) end end |
#build_cursor_from_instance(context, instance) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/jsonapionify/api/resource/builders.rb', line 94 def build_cursor_from_instance(context, instance) sort_string = context.params['sort'] sort_fields = sort_fields_from_sort_string(sort_string) attrs_with_values = sort_fields.each_with_object({}) do |field, hash| hash[field.name] = instance.send(field.name) end Base64.urlsafe_encode64(JSON.dump( { t: type, s: sort_string, a: attrs_with_values } )) end |
#build_id(instance) ⇒ Object
149 150 151 |
# File 'lib/jsonapionify/api/resource/builders.rb', line 149 def build_id(instance) instance.send(id_attribute).to_s end |
#build_identifier_collection(collection) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/jsonapionify/api/resource/builders.rb', line 109 def build_identifier_collection(collection) collection.each_with_object( JSONAPIonify::Structure::Collections::ResourceIdentifiers.new ) do |instance, resource_identifiers| resource_identifiers << build_resource_identifier(instance) end end |
#build_relationship(context, instance, name, links: true, data: false) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/jsonapionify/api/resource/builders.rb', line 117 def build_relationship(context, instance, name, links: true, data: false) resource_url = build_url(context, instance) relationship = self.relationship(name) JSONAPIonify::Structure::Objects::Relationship.new.tap do |rel| rel[:links] = relationship.build_links(resource_url) if links if data || context.includes.present? rel[:data] = if relationship.rel.is_a? Relationship::Many instance.send(name).map do |child| relationship.build_resource_identifier(child) end elsif relationship.rel.is_a? Relationship::One value = instance.send(name) relationship.build_resource_identifier value if value end end end end |
#build_resource(context, instance, fields:, relationships: true, links: true, include_cursor: false, &block) ⇒ Object
10 11 12 13 14 15 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 |
# File 'lib/jsonapionify/api/resource/builders.rb', line 10 def build_resource( context, instance, fields:, relationships: true, links: true, include_cursor: false, &block ) example_id = generate_id include_rel_param = context.params['include-relationships'] relationships = false if FALSEY_STRINGS.include?(include_rel_param) return nil unless instance resource_url = build_url(context, instance) id = build_id(instance) Structure::Objects::Resource.new.tap do |resource| resource[:id] = id resource[:type] = type resource[:attributes] = fields[type.to_sym].each_with_object( Structure::Objects::Attributes.new ) do |member, attributes| attribute = self.attributes.find { |a| a.name == member.to_sym } unless attribute.supports_read_for_action?(context.action_name, context) error_block = context.resource.class.error_definitions[:internal_server_error] context.errors.evaluate( name, error_block: error_block, runtime_block: proc {} ) end attributes[member.to_sym] = attribute.resolve( instance, context, example_id: example_id ) end resource[:links] = Structure::Objects::Links.new( self: resource_url ) if links resource[:meta] = { cursor: build_cursor_from_instance(context, instance) } if include_cursor resource[:relationships] = relationship_definitions.each_with_object( Structure::Maps::Relationships.new ) do |rel, hash| hash[rel.name] = build_relationship(context, instance, rel.name) end if relationships || context.includes.present? block.call(resource, instance) if block end end |
#build_resource_identifier(instance) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/jsonapionify/api/resource/builders.rb', line 64 def build_resource_identifier(instance) Structure::Objects::ResourceIdentifier.new( id: build_id(instance), type: type.to_s ) end |
#build_url(context, instance = nil) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/jsonapionify/api/resource/builders.rb', line 136 def build_url(context, instance = nil) URI.parse(context.request.root_url).tap do |uri| uri.path = if instance File.join(uri.path, type, build_id(instance)) else File.join(context.request.root_url, type) end sticky_params = self.sticky_params(context.params) uri.query = sticky_params.to_param if sticky_params.present? end.to_s end |