Class: GrapeSwagger::FastJsonapi::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/grape_fast_jsonapi/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, endpoint) ⇒ Parser

Returns a new instance of Parser.



9
10
11
12
# File 'lib/grape_fast_jsonapi/parser.rb', line 9

def initialize(model, endpoint)
  @model = model
  @endpoint = endpoint
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



7
8
9
# File 'lib/grape_fast_jsonapi/parser.rb', line 7

def endpoint
  @endpoint
end

#modelObject (readonly)

Returns the value of attribute model.



6
7
8
# File 'lib/grape_fast_jsonapi/parser.rb', line 6

def model
  @model
end

Instance Method Details

#callObject



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
# File 'lib/grape_fast_jsonapi/parser.rb', line 14

def call
  schema = default_schema

  attributes_hash = if (defined? ActiveRecord)
                map_active_record_columns_to_attributes
              else
                map_model_attributes
              end

  attributes_hash.each do |attribute, type|
    schema[:data][:properties][:attributes][:properties][attribute] = { type: type }
    schema[:data][:example][:attributes][attribute] = send "#{type}_example"
  end

  relationships_hash = model.relationships_to_serialize || []

  relationships_hash.each do |model_type, relationship_data|
    relationships_attributes = relationship_data.instance_values.symbolize_keys
    schema[:data][:properties][:relationships][:properties][model_type] = {
      type: :object,
      properties: relationships_properties(relationships_attributes)
    }
    schema[:data][:example][:relationships][model_type] = relationships_example(relationships_attributes)
  end

  schema
end