Module: JsonapiSpecHelpers::Helpers
- Defined in:
- lib/jsonapi_spec_helpers/helpers.rb
Instance Method Summary collapse
- #json ⇒ Object
- #json_ids(integers = false) ⇒ Object
- #json_include(type, index = 0) ⇒ Object
- #json_included_types ⇒ Object
- #json_includes(type, *indices) ⇒ Object
- #json_item(from: nil) ⇒ Object
- #json_items(*indices) ⇒ Object
- #json_related_link(payload, assn_name) ⇒ Object
Instance Method Details
#json ⇒ Object
3 4 5 |
# File 'lib/jsonapi_spec_helpers/helpers.rb', line 3 def json JSON.parse(response.body) end |
#json_ids(integers = false) ⇒ Object
51 52 53 54 55 |
# File 'lib/jsonapi_spec_helpers/helpers.rb', line 51 def json_ids(integers = false) ids = json['data'].map { |d| d['id'] } ids.map!(&:to_i) if integers ids end |
#json_include(type, index = 0) ⇒ Object
47 48 49 |
# File 'lib/jsonapi_spec_helpers/helpers.rb', line 47 def json_include(type, index = 0) json_includes(type, index)[0] end |
#json_included_types ⇒ Object
33 34 35 |
# File 'lib/jsonapi_spec_helpers/helpers.rb', line 33 def json_included_types (json['included'] || []).map { |i| i['type'] }.uniq end |
#json_includes(type, *indices) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/jsonapi_spec_helpers/helpers.rb', line 37 def json_includes(type, *indices) included = (json['included'] || []).select { |data| data['type'] == type } indices = (0...included.length).to_a if indices.empty? includes = [] indices.each do |index| includes << json_item(from: included.at(index)) end includes end |
#json_item(from: nil) ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/jsonapi_spec_helpers/helpers.rb', line 7 def json_item(from: nil) from = json if from.nil? data = from.has_key?('data') ? from['data'] : from {}.tap do |item| item['id'] = data['id'] item['jsonapi_type'] = data['type'] item.merge!(data['attributes']) if data.has_key?('attributes') end end |
#json_items(*indices) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/jsonapi_spec_helpers/helpers.rb', line 18 def json_items(*indices) items = [] json['data'].each_with_index do |datum, index| included = indices.empty? || indices.include?(index) items << json_item(from: datum) if included end indices.length == 1 ? items[0] : items end |
#json_related_link(payload, assn_name) ⇒ Object
27 28 29 30 31 |
# File 'lib/jsonapi_spec_helpers/helpers.rb', line 27 def (payload, assn_name) link = payload['relationships'][assn_name]['links']['related']['href'] fail "link for #{assn_name} not found" unless link URI.decode(link) end |