Class: JSONAPI::Realizer::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/realizer/action.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload:, headers:, scope: nil) ⇒ Action

Returns a new instance of Action.

Raises:

  • (Error::MissingAcceptHeader)


15
16
17
18
19
20
21
22
23
24
# File 'lib/jsonapi/realizer/action.rb', line 15

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.is_a?(Array) || data.is_a?(Hash) || data.nil?)
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



13
14
15
# File 'lib/jsonapi/realizer/action.rb', line 13

def headers
  @headers
end

#payloadObject (readonly)

Returns the value of attribute payload.



12
13
14
# File 'lib/jsonapi/realizer/action.rb', line 12

def payload
  @payload
end

Instance Method Details

#callObject



26
# File 'lib/jsonapi/realizer/action.rb', line 26

def call; end

#fieldsObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/jsonapi/realizer/action.rb', line 60

def fields
  return [] if payload.blank?
  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.tr("-", "_") }.
    # ["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

#includesObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jsonapi/realizer/action.rb', line 36

def includes
  return [] if payload.blank?
  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.tr("-", "_") }.
    # ["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

#relationObject



28
29
30
31
32
33
34
# File 'lib/jsonapi/realizer/action.rb', line 28

def relation
  relation_after_fields(
    relation_after_inclusion(
      @scope || model_class
    )
  )
end