Module: JsonApiTestHelpers
- Defined in:
- lib/json_api_test_helpers.rb,
lib/json_api_test_helpers/version.rb
Constant Summary collapse
- VERSION =
'1.0'
Instance Method Summary collapse
- #attributes_for_nested(attributes, **associations) ⇒ Object
- #fix_comparing_types(value) ⇒ Object
- #fix_value_for_json(value) ⇒ Object
- #json_api_collection(collection, attributes = nil, relationships: nil) ⇒ Object
- #json_api_record(record, attributes, relationships: nil, additional: nil) ⇒ Object
- #json_response ⇒ Object
Instance Method Details
#attributes_for_nested(attributes, **associations) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/json_api_test_helpers.rb', line 58 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
48 49 50 51 52 53 54 55 56 |
# File 'lib/json_api_test_helpers.rb', line 48 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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/json_api_test_helpers.rb', line 33 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
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/json_api_test_helpers.rb', line 20 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
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/json_api_test_helpers.rb', line 9 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_response ⇒ Object
4 5 6 7 |
# File 'lib/json_api_test_helpers.rb', line 4 def json_response parsed_json = JSON.parse response.body parsed_json.with_indifferent_access unless parsed_json.is_a? Array end |