Module: JsonApiTestHelpers::Response

Included in:
JsonApiTestHelpers
Defined in:
lib/json_api_test_helpers/response.rb

Instance Method Summary collapse

Instance Method Details

#attributes_for_nested(attributes, **associations) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/json_api_test_helpers/response.rb', line 61

def attributes_for_nested(attributes, **associations)
  associations.reduce(attributes) do |attr_hash, collection|
    attr_hash.merge! "#{collection[0]}_attributes" => (collection[1].each_with_index.reduce({}) do |hash, item|
      hash.merge! item.last.to_s => item.first
    end)
  end
end

#fix_comparing_types(value) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/json_api_test_helpers/response.rb', line 51

def fix_comparing_types(value)
  if value.class.in? [DateTime, ActiveSupport::TimeWithZone]
    value.to_datetime.utc.to_s
  elsif value.class == ActiveRecord::Point
    "#{value.x}, #{value.y}"
  else
    value
  end
end

#fix_value_for_json(value) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/json_api_test_helpers/response.rb', line 36

def fix_value_for_json(value)
  return value.iso8601 if value.class.in? [DateTime, ActiveSupport::TimeWithZone]
  return value if value.is_a? Integer
  return value.serializable_hash if value.is_a? CarrierWave::Uploader::Serialization
  return value.reduce({}) do |hash, k_v|
    if k_v[1].is_a? Hash
      k_v[1] = k_v[1].reduce({}) do |value_hash, value_k_v|
        value_hash.merge! value_k_v[0].gsub('_', '-') => value_k_v[1]
      end
    end
    hash.merge! k_v[0].gsub('_', '-') => k_v[1]    
  end if value.is_a? Hash
  value.to_s
end

#json_api_collection(collection, attributes = nil, relationships: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/json_api_test_helpers/response.rb', line 23

def json_api_collection(collection, attributes = nil, relationships: nil)
  {
    'data' => collection.map do |record|
      hash = {
        'id' => record.id.to_s
      }
      hash['attributes'] = json_api_record_attributes(record, attributes) if attributes
      hash['relationships'] = json_api_relationships(record, relationships) if relationships
      hash
    end
  }
end

#json_api_record(record, attributes, relationships: nil, additional: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/json_api_test_helpers/response.rb', line 12

def json_api_record(record, attributes, relationships: nil, additional: nil)
  json = {
    'data' => {
      'id' => record.id.to_s,
      'attributes' => json_api_record_attributes(record, attributes)
    }
  }
  json['data']['relationships'] = json_api_relationships(record, relationships) if relationships
  json
end

#json_responseObject



3
4
5
6
7
8
9
10
# File 'lib/json_api_test_helpers/response.rb', line 3

def json_response
  if response.body.present?
    parsed_json = JSON.parse response.body
    parsed_json.with_indifferent_access unless parsed_json.is_a? Array
  else
    raise 'Response body is empty'
  end
end