module JsonapiSpecHelpers::Helpers
Public Instance Methods
json()
click to toggle source
# File lib/jsonapi_spec_helpers/helpers.rb, line 3 def json JSON.parse(response.body) end
json_ids(integers = false)
click to toggle source
# File lib/jsonapi_spec_helpers/helpers.rb, line 55 def json_ids(integers = false) ids = json['data'].map { |d| d['id'] } ids.map! { |id| Integer(id) } if integers ids end
json_include(type, index = 0)
click to toggle source
# File lib/jsonapi_spec_helpers/helpers.rb, line 51 def json_include(type, index = 0) json_includes(type, index)[0] end
json_included_types()
click to toggle source
# 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)
click to toggle source
# 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| single_included = included.at(index) if single_included.nil? raise Errors::IncludedOutOfBounds.new(type, index, included) end includes << json_item(from: single_included) end includes end
json_item(from: nil)
click to toggle source
# 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)
click to toggle source
# 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
jsonapi_delete(url)
click to toggle source
# File lib/jsonapi_spec_helpers/helpers.rb, line 99 def jsonapi_delete(url) delete url, headers: jsonapi_headers end
jsonapi_get(url, params: {})
click to toggle source
# File lib/jsonapi_spec_helpers/helpers.rb, line 83 def jsonapi_get(url, params: {}) get url, params: params, headers: jsonapi_headers end
jsonapi_headers()
click to toggle source
# File lib/jsonapi_spec_helpers/helpers.rb, line 77 def jsonapi_headers { 'CONTENT_TYPE' => 'application/vnd.api+json' } end
jsonapi_patch(url, payload)
click to toggle source
# File lib/jsonapi_spec_helpers/helpers.rb, line 95 def jsonapi_patch(url, payload) patch url, params: payload.to_json, headers: jsonapi_headers end
jsonapi_payload(input)
click to toggle source
# File lib/jsonapi_spec_helpers/helpers.rb, line 103 def jsonapi_payload(input) PayloadSanitizer.new(input).sanitize end
jsonapi_post(url, payload)
click to toggle source
# File lib/jsonapi_spec_helpers/helpers.rb, line 87 def jsonapi_post(url, payload) post url, params: payload.to_json, headers: jsonapi_headers end
jsonapi_put(url, payload)
click to toggle source
# File lib/jsonapi_spec_helpers/helpers.rb, line 91 def jsonapi_put(url, payload) put url, params: payload.to_json, headers: jsonapi_headers end
validation_errors()
click to toggle source
# File lib/jsonapi_spec_helpers/helpers.rb, line 61 def validation_errors @validation_errors ||= {}.tap do |errors| return errors if json['errors'].nil? json['errors'].each do |e| attr = e['meta']['attribute'].to_sym message = e['meta']['message'] if errors[attr] errors[attr] = Array(errors[attr]).push(message) else errors[attr] = message end end end end