Class: JSONAPI::Realizer::Action
- Inherits:
-
Object
- Object
- JSONAPI::Realizer::Action
- Defined in:
- lib/jsonapi/realizer/action.rb,
lib/jsonapi/realizer/action/show.rb,
lib/jsonapi/realizer/action/index.rb,
lib/jsonapi/realizer/action/create.rb,
lib/jsonapi/realizer/action/update.rb,
lib/jsonapi/realizer/action/destroy.rb
Defined Under Namespace
Classes: Create, Destroy, Index, Show, Update
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.
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 unless @headers.fetch("Accept") == JSONAPI::MEDIA_TYPE raise Error::IncludeWithoutDataProperty if @payload.key?("include") && !@payload.key?("data") raise Error::MalformedDataRootProperty 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
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/jsonapi/realizer/action.rb', line 118 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.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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/jsonapi/realizer/action.rb', line 94 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.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 |