Module: JsonApiTestHelpers::Response
- Included in:
- JsonApiTestHelpers
- Defined in:
- lib/json_api_test_helpers/response.rb
Instance Method Summary collapse
- #attributes_for_nested(attributes, **associations) ⇒ Object
- #data_relationship_object(object, relationship) ⇒ Object
- #fix_comparing_types(value) ⇒ Object
- #fix_value_for_json(value) ⇒ Object
- #json_api_collection(collection, attributes = nil, relationships: nil) ⇒ Object
- #json_api_model_name(model_name) ⇒ Object
- #json_api_record(record, attributes, relationships: nil, additional: nil) ⇒ Object
- #json_api_record_attributes(record, attributes) ⇒ Object
- #json_api_relationships(record, relationships) ⇒ Object
- #json_response ⇒ Object
- #relation_has_another_class_name?(relationship) ⇒ Boolean
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 |
#data_relationship_object(object, relationship) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/json_api_test_helpers/response.rb', line 93 def data_relationship_object(object, relationship) { 'id' => object.uuid, 'type' => json_api_model_name(relationship).pluralize } 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.uuid } hash['attributes'] = json_api_record_attributes(record, attributes) if attributes hash['relationships'] = json_api_relationships(record, relationships) if relationships hash end } end |
#json_api_model_name(model_name) ⇒ Object
89 90 91 |
# File 'lib/json_api_test_helpers/response.rb', line 89 def json_api_model_name(model_name) model_name.to_s.tr '_', '-' 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.uuid, 'attributes' => json_api_record_attributes(record, attributes) } } json['data']['relationships'] = json_api_relationships(record, relationships) if relationships json end |
#json_api_record_attributes(record, attributes) ⇒ Object
69 70 71 72 73 |
# File 'lib/json_api_test_helpers/response.rb', line 69 def json_api_record_attributes(record, attributes) attributes.reduce({}) do |hash, attr| hash.merge! attr.to_s.tr('_', '-') => fix_value_for_json(record.send(attr)) end end |
#json_api_relationships(record, relationships) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/json_api_test_helpers/response.rb', line 75 def json_api_relationships(record, relationships) relationships.reduce({}) do |hash, relationship| name = relation_has_another_class_name?(relationship) ? relationship[0] : relationship class_name = relation_has_another_class_name?(relationship) ? relationship[1] : relationship object = record.send name data = if object.is_a?(ActiveRecord::Associations::CollectionProxy) object.reduce([]) { |result, item| result << data_relationship_object(item, class_name) } else object.present? ? data_relationship_object(object, class_name) : {} end hash.merge! json_api_model_name(name) => { 'data' => data } end end |
#json_response ⇒ Object
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 |
#relation_has_another_class_name?(relationship) ⇒ Boolean
100 101 102 |
# File 'lib/json_api_test_helpers/response.rb', line 100 def relation_has_another_class_name?(relationship) relationship.is_a? Array end |