Module: Graphiti::SpecHelpers::Helpers

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

Instance Method Summary collapse

Instance Method Details

#jsonObject



6
7
8
9
10
11
12
# File 'lib/graphiti/spec_helpers/helpers.rb', line 6

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

#json_date(value) ⇒ Object



75
76
77
# File 'lib/graphiti/spec_helpers/helpers.rb', line 75

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

#json_datetime(value) ⇒ Object



71
72
73
# File 'lib/graphiti/spec_helpers/helpers.rb', line 71

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

#jsonapi_dataObject



14
15
16
17
18
19
20
# File 'lib/graphiti/spec_helpers/helpers.rb', line 14

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

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



67
68
69
# File 'lib/graphiti/spec_helpers/helpers.rb', line 67

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

#jsonapi_errorsObject



35
36
37
# File 'lib/graphiti/spec_helpers/helpers.rb', line 35

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

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



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

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

#jsonapi_headersObject



43
44
45
46
47
48
49
# File 'lib/graphiti/spec_helpers/helpers.rb', line 43

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

#jsonapi_included(type = nil) ⇒ Object



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

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.to_s }
  end
  instance_variable_set(variable, nodes)
  nodes
end

#jsonapi_metaObject



39
40
41
# File 'lib/graphiti/spec_helpers/helpers.rb', line 39

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

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



63
64
65
# File 'lib/graphiti/spec_helpers/helpers.rb', line 63

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

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



55
56
57
# File 'lib/graphiti/spec_helpers/helpers.rb', line 55

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

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



59
60
61
# File 'lib/graphiti/spec_helpers/helpers.rb', line 59

def jsonapi_put(url, payload, headers: {})
  put url_for(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.



80
81
82
83
84
85
86
87
88
# File 'lib/graphiti/spec_helpers/helpers.rb', line 80

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