Module: JsonApiTestHelpers
- Defined in:
- lib/json_api_test_helpers.rb,
lib/json_api_test_helpers/version.rb
Constant Summary collapse
- VERSION =
'1.0.1'
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
62 63 64 65 66 67 68 |
# File 'lib/json_api_test_helpers.rb', line 62 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
52 53 54 55 56 57 58 59 60 |
# File 'lib/json_api_test_helpers.rb', line 52 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
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/json_api_test_helpers.rb', line 37 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
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/json_api_test_helpers.rb', line 24 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
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/json_api_test_helpers.rb', line 13 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 8 9 10 11 |
# File 'lib/json_api_test_helpers.rb', line 4 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 |