Skip to content

Instantly share code, notes, and snippets.

@julienbourdeau
Last active June 3, 2020 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julienbourdeau/f0dc3bd8b368f6b82785a6bc1088affe to your computer and use it in GitHub Desktop.
Save julienbourdeau/f0dc3bd8b368f6b82785a6bc1088affe to your computer and use it in GitHub Desktop.
Delegate read write to @Transporter

See: https://www.rubyguides.com/2018/10/delegate-methods-in-ruby/

module Algolia
  module Search
    # Class Index
    class Index
      include CallType
      extend Forwardable # available in ruby 2.2
      
      attr_reader :index_name, :transporter, :config
      
      def_delegators :@tranporter, :read, :write
      
    end
  end
end

The instead of

def search(query, opts: {})
  @transporter.read(:POST, "#{@index_uri}/query", body: {'query': query}, opts: opts)
end
      
def batch(request, opts: {})
  @transporter.write(:POST, "#{@index_uri}/batch", body: request, opts: opts)
end

you simply write

def search(query, opts: {})
  read(:POST, "#{@index_uri}/query", body: {'query': query}, opts: opts)
end

def batch(request, opts: {})
  write(:POST, "#{@index_uri}/batch", body: request, opts: opts)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment