Module: Graphiti::Resource::Remote

Extended by:
ActiveSupport::Concern
Defined in:
lib/graphiti/resource/remote.rb

Instance Method Summary collapse

Instance Method Details

#before_resolve(scope, query) ⇒ Object



33
34
35
36
# 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) ⇒ Object



29
30
31
# File 'lib/graphiti/resource/remote.rb', line 29

def destroy(*args)
  raise Errors::RemoteWrite.new(self.class)
end

#make_request(url) ⇒ Object



55
56
57
58
59
60
61
62
63
# 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_urlObject



51
52
53
# File 'lib/graphiti/resource/remote.rb', line 51

def remote_url
  self.class.remote_url
end

#request_headersObject

Forward all headers



39
40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Object



21
22
23
24
25
26
27
# 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