Module: GraphitiSpecHelpers::Helpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/graphiti_spec_helpers/helpers.rb

Instance Method Summary collapse

Instance Method Details

#jsonObject



5
6
7
8
9
10
11
# File 'lib/graphiti_spec_helpers/helpers.rb', line 5

def json
  if response && response.body
    JSON.parse(response.body).with_indifferent_access
  else
    raise Errors::NoResponse.new
  end
end

#json_date(value) ⇒ Object



76
77
78
# File 'lib/graphiti_spec_helpers/helpers.rb', line 76

def json_date(value)
  Graphiti::Types[:date][:read][value].as_json
end

#json_datetime(value) ⇒ Object



72
73
74
# File 'lib/graphiti_spec_helpers/helpers.rb', line 72

def json_datetime(value)
  Graphiti::Types[:datetime][:read][value].as_json
end

#jsonapi_dataObject



13
14
15
16
17
18
19
20
21
# File 'lib/graphiti_spec_helpers/helpers.rb', line 13

def jsonapi_data
  @jsonapi_data ||= begin
    if _jsonapi_data.is_a?(Hash)
      node(from: _jsonapi_data)
    else
      _jsonapi_data.map { |datum| node(from: datum) }
    end
  end
end

#jsonapi_delete(url, headers: {}) ⇒ Object



68
69
70
# File 'lib/graphiti_spec_helpers/helpers.rb', line 68

def jsonapi_delete(url, headers: {})
  delete url, headers: jsonapi_headers.merge(headers)
end

#jsonapi_errorsObject



36
37
38
# File 'lib/graphiti_spec_helpers/helpers.rb', line 36

def jsonapi_errors
  @jsonapi_errors ||= ErrorsProxy.new(json['errors'] || [])
end

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



52
53
54
# File 'lib/graphiti_spec_helpers/helpers.rb', line 52

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

#jsonapi_headersObject



44
45
46
47
48
49
50
# File 'lib/graphiti_spec_helpers/helpers.rb', line 44

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

#jsonapi_included(type = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/graphiti_spec_helpers/helpers.rb', line 23

def jsonapi_included(type = nil)
  variable = :"@jsonapi_included#{type}"
  memo = instance_variable_get(variable)
  return memo if memo

  nodes =  _jsonapi_included.map { |i| node(from: i) }
  if type
    nodes.select! { |n| n.jsonapi_type == type }
  end
  instance_variable_set(variable, nodes)
  nodes
end

#jsonapi_metaObject



40
41
42
# File 'lib/graphiti_spec_helpers/helpers.rb', line 40

def jsonapi_meta
  @jsonapi_errors = json['meta'] || raise(Errors::NoMeta.new(json))
end

#jsonapi_patch(url, payload, headers: {}) ⇒ Object



64
65
66
# File 'lib/graphiti_spec_helpers/helpers.rb', line 64

def jsonapi_patch(url, payload, headers: {})
  patch url, params: payload.to_json, headers: jsonapi_headers.merge(headers)
end

#jsonapi_post(url, payload, headers: {}) ⇒ Object



56
57
58
# File 'lib/graphiti_spec_helpers/helpers.rb', line 56

def jsonapi_post(url, payload, headers: {})
  post url, params: payload.to_json, headers: jsonapi_headers.merge(headers)
end

#jsonapi_put(url, payload, headers: {}) ⇒ Object



60
61
62
# File 'lib/graphiti_spec_helpers/helpers.rb', line 60

def jsonapi_put(url, payload, headers: {})
  put url, params: payload.to_json, headers: jsonapi_headers.merge(headers)
end

#node(from: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



81
82
83
84
85
86
87
88
89
# File 'lib/graphiti_spec_helpers/helpers.rb', line 81

def node(from: nil)
  from = json if from.nil?
  data = from.has_key?('data') ? from['data'] : from
  hash = {}
  hash['id'] = data['id']
  hash['jsonapi_type'] = data['type']
  hash.merge!(data['attributes']) if data.has_key?('attributes')
  Node.new(hash, data['relationships'], self)
end