Class: JSONAPI::Realizer::Action
- Inherits:
-
Object
- Object
- JSONAPI::Realizer::Action
- Defined in:
- lib/jsonapi/realizer/action.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
Instance Method Summary collapse
- #call ⇒ Object
- #fields ⇒ Object
- #includes ⇒ Object
-
#initialize(payload:, headers:, scope: nil) ⇒ Action
constructor
A new instance of Action.
- #relation ⇒ Object
Constructor Details
#initialize(payload:, headers:, scope: nil) ⇒ Action
Returns a new instance of Action.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/jsonapi/realizer/action.rb', line 13 def initialize(payload:, headers:, scope: nil) @scope = scope @headers = headers @payload = payload raise Error::MissingAcceptHeader unless @headers.key?("Accept") raise Error::InvalidAcceptHeader, given: @headers.fetch("Accept"), wanted: JSONAPI::MEDIA_TYPE unless @headers.fetch("Accept") == JSONAPI::MEDIA_TYPE raise Error::IncludeWithoutDataProperty if @payload.key?("include") && !@payload.key?("data") raise Error::MalformedDataRootProperty, given: data if @payload.key?("data") && !(data.kind_of?(Array) || data.kind_of?(Hash) || data.nil?) end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
11 12 13 |
# File 'lib/jsonapi/realizer/action.rb', line 11 def headers @headers end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
10 11 12 |
# File 'lib/jsonapi/realizer/action.rb', line 10 def payload @payload end |
Instance Method Details
#call ⇒ Object
24 |
# File 'lib/jsonapi/realizer/action.rb', line 24 def call; end |
#fields ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/jsonapi/realizer/action.rb', line 129 def fields return [] unless payload.present? return [] unless payload.key?("fields") payload. fetch("fields"). # "title,active-photographer.email,active-photographer.posts.title" split(/\s*,\s*/). # ["title", "active-photographer.email", "active-photographer.posts.title"] map { |path| path.gsub("-", "_") }. # ["title", "active_photographer.email", "active_photographer.posts.title"] map { |path| path.split(".") }. # [["title"], ["active_photographer", "email"], ["active_photographer", "posts", "title"]] select do |chain| # ["active_photographer", "email"] chain.reduce(resource_class) do |last_resource_class, key| break unless last_resource_class if last_resource_class.valid_includes?(key) JSONAPI::Realizer::Resource.type_mapping.fetch(last_resource_class.relationship(key).as).resource_class elsif last_resource_class.valid_sparse_field?(key) last_resource_class end end end # [["title"], ["active_photographer", "email"]] end |
#includes ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/jsonapi/realizer/action.rb', line 105 def includes return [] unless payload.present? return [] unless payload.key?("include") payload. fetch("include"). # "carts.cart-items,carts.cart-items.product,carts.billing-information,payments" split(/\s*,\s*/). # ["carts.cart-items", "carts.cart-items.product", "carts.billing-information", "payments"] map { |path| path.gsub("-", "_") }. # ["carts.cart_items", "carts.cart_items.product", "carts.billing_information", "payments"] map { |path| path.split(".") }. # [["carts", "cart_items"], ["carts", "cart_items", "product"], ["carts", "billing_information"], ["payments"]] select do |chain| # ["carts", "cart_items"] chain.reduce(resource_class) do |last_resource_class, key| break unless last_resource_class JSONAPI::Realizer::Resource.type_mapping.fetch(last_resource_class.relationship(key).as).resource_class if last_resource_class.valid_includes?(key) end end # [["carts", "cart_items", "product"], ["payments"]] end |
#relation ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/jsonapi/realizer/action.rb', line 54 def relation relation_after_fields( relation_after_inclusion( @scope || model_class ) ) end |