module Graphiti::Resource::Remote

Public Instance Methods

before_resolve(scope, query) click to toggle source
# File lib/graphiti/resource/remote.rb, line 33
def before_resolve(scope, query)
  scope[:params] = Util::RemoteParams.generate(self, query, scope[:foreign_key])
  scope
end
destroy(*args) click to toggle source
# File lib/graphiti/resource/remote.rb, line 29
def destroy(*args)
  raise Errors::RemoteWrite.new(self.class)
end
make_request(url) { |req| ... } click to toggle source
# File lib/graphiti/resource/remote.rb, line 55
def make_request(url)
  headers = request_headers.dup
  headers["Content-Type"] = "application/vnd.api+json"
  faraday.get(url, nil, headers) do |req|
    yield req if block_given? # for super do ... end
    req.options.timeout = timeout if timeout
    req.options.open_timeout = open_timeout if open_timeout
  end
end
remote_url() click to toggle source
# File lib/graphiti/resource/remote.rb, line 16
def remote_url
  [remote_base_url, remote].join
end
request_headers() click to toggle source

Forward all headers

# File lib/graphiti/resource/remote.rb, line 39
def request_headers
  {}.tap do |headers|
    # TODO: Maybe handle this in graphiti-rails
    if defined?(Rails) && context
      raw = context.request.headers.to_h
      if (auth = raw["HTTP_AUTHORIZATION"])
        headers["Authorization"] = auth
      end
    end
  end
end
save(model, meta) click to toggle source
# File lib/graphiti/resource/remote.rb, line 21
def save(model, meta)
  if meta[:attributes].except(:id) == {} && meta[:method] == :update
    model
  else
    raise Errors::RemoteWrite.new(self.class)
  end
end

Private Instance Methods

faraday() click to toggle source
# File lib/graphiti/resource/remote.rb, line 67
def faraday
  if defined?(Faraday)
    Faraday
  else
    raise "Faraday not defined. Please require the 'faraday' gem to use remote resources"
  end
end