Module: JsonapiSpecHelpers::Helpers

Defined in:
lib/jsonapi_spec_helpers/helpers.rb

Instance Method Summary collapse

Instance Method Details

#jsonObject



3
4
5
# File 'lib/jsonapi_spec_helpers/helpers.rb', line 3

def json
  JSON.parse(response.body)
end

#json_ids(integers = false) ⇒ Object



55
56
57
58
59
# File 'lib/jsonapi_spec_helpers/helpers.rb', line 55

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



51
52
53
# File 'lib/jsonapi_spec_helpers/helpers.rb', line 51

def json_include(type, index = 0)
  json_includes(type, index)[0]
end

#json_included_typesObject



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
46
47
48
49
# 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) ⇒ 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


27
28
29
30
31
# File 'lib/jsonapi_spec_helpers/helpers.rb', line 27

def json_related_link(payload, assn_name)
  link = payload['relationships'][assn_name]['links']['related']['href']
  fail "link for #{assn_name} not found" unless link
  URI.decode(link)
end

#jsonapi_delete(url) ⇒ Object



99
100
101
# File 'lib/jsonapi_spec_helpers/helpers.rb', line 99

def jsonapi_delete(url)
  delete url, headers: jsonapi_headers
end

#jsonapi_get(url, params: {}) ⇒ Object



83
84
85
# File 'lib/jsonapi_spec_helpers/helpers.rb', line 83

def jsonapi_get(url, params: {})
  get url, params: params, headers: jsonapi_headers
end

#jsonapi_headersObject



77
78
79
80
81
# File 'lib/jsonapi_spec_helpers/helpers.rb', line 77

def jsonapi_headers
  {
    'CONTENT_TYPE' => 'application/vnd.api+json'
  }
end

#jsonapi_patch(url, payload) ⇒ Object



95
96
97
# 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) ⇒ Object



103
104
105
# File 'lib/jsonapi_spec_helpers/helpers.rb', line 103

def jsonapi_payload(input)
  PayloadSanitizer.new(input).sanitize
end

#jsonapi_post(url, payload) ⇒ Object



87
88
89
# 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) ⇒ Object



91
92
93
# 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_errorsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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