Sha256: 9b08ba34b1d9e88717dc4f71c172f58bf6efc036683c6a60909762412decbc0f

Contents?: true

Size: 1.66 KB

Versions: 6

Compression:

Stored size: 1.66 KB

Contents

module Graphiti
  class Resource
    module Remote
      extend ActiveSupport::Concern

      included do
        self.adapter = Graphiti::Adapters::GraphitiAPI
        self.model = OpenStruct
        self.validate_endpoints = false

        class_attribute :timeout,
          :open_timeout
      end

      class_methods do
        def remote_url
          [remote_base_url, remote].join
        end
      end

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

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

      def before_resolve(scope, query)
        scope[:params] = Util::RemoteParams.generate(self, query)
        scope
      end

      # Forward all headers
      def request_headers
        {}.tap do |headers|
          if defined?(Rails) && context
            raw = context.request.headers.to_h
            if (auth = raw["HTTP_AUTHORIZATION"])
              headers["Authorization"] = auth
            end
          end
        end
      end

      def remote_url
        self.class.remote_url
      end

      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

      private

      def faraday
        if defined?(Faraday)
          Faraday
        else
          raise "Faraday not defined. Please require the 'faraday' gem to use remote resources"
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
graphiti-1.0.rc.27 lib/graphiti/resource/remote.rb
graphiti-1.0.rc.26 lib/graphiti/resource/remote.rb
graphiti-1.0.rc.25 lib/graphiti/resource/remote.rb
graphiti-1.0.rc.24 lib/graphiti/resource/remote.rb
graphiti-1.0.rc.23 lib/graphiti/resource/remote.rb
graphiti-1.0.rc.22 lib/graphiti/resource/remote.rb