module GraphitiSpecHelpers::Helpers

Public Instance Methods

json() click to toggle source
# 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) click to toggle source
# File lib/graphiti_spec_helpers/helpers.rb, line 76
def json_date(value)
  Graphiti::Types[:date][:read][value].as_json
end
json_datetime(value) click to toggle source
# File lib/graphiti_spec_helpers/helpers.rb, line 72
def json_datetime(value)
  Graphiti::Types[:datetime][:read][value].as_json
end
jsonapi_data() click to toggle source
# 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: {}) click to toggle source
# File lib/graphiti_spec_helpers/helpers.rb, line 68
def jsonapi_delete(url, headers: {})
  delete url, headers: jsonapi_headers.merge(headers)
end
jsonapi_errors() click to toggle source
# File lib/graphiti_spec_helpers/helpers.rb, line 36
def jsonapi_errors
  @jsonapi_errors ||= ErrorsProxy.new(json['errors'] || [])
end
jsonapi_get(url, params: {}, headers: {}) click to toggle source
# 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_headers() click to toggle source
# 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) click to toggle source
# 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_meta() click to toggle source
# 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: {}) click to toggle source
# 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: {}) click to toggle source
# 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: {}) click to toggle source
# 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) click to toggle source

@api private

# 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

Private Instance Methods

_jsonapi_data() click to toggle source

@api private

# File lib/graphiti_spec_helpers/helpers.rb, line 94
def _jsonapi_data
  json['data'] || raise(Errors::NoData.new(json))
end
_jsonapi_included() click to toggle source

@api private

# File lib/graphiti_spec_helpers/helpers.rb, line 99
def _jsonapi_included
  if json.has_key?('included')
    json['included']
  else
    raise Errors::NoSideloads.new(json)
  end
end