Class: Algolia::Recommend::Client

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/algolia/recommend_client.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#check_array, #check_object, #chunk, #deserialize_settings, #get_object_id, #get_option, #handle_params, #hash_includes_subset?, included, #json_to_hash, #path_encode, #symbolize_hash, #to_json, #to_query_string

Constructor Details

#initialize(recommend_config, opts = {}) ⇒ Client

Initializes the Recommend client

Parameters:

  • recommend_config (Recommend::Config)

    a Recommend::Config object which contains your APP_ID and API_KEY

  • adapter (Hash)

    a customizable set of options

  • logger (Hash)

    a customizable set of options

  • http_requester (Hash)

    a customizable set of options



18
19
20
21
22
23
24
# File 'lib/algolia/recommend_client.rb', line 18

def initialize(recommend_config, opts = {})
  @config      = recommend_config
  adapter      = opts[:adapter] || Defaults::ADAPTER
  logger       = opts[:logger] || LoggerHelper.create
  requester    = opts[:http_requester] || Defaults::REQUESTER_CLASS.new(adapter, logger)
  @transporter = Transport::Transport.new(@config, requester)
end

Class Method Details

.create(app_id, api_key) ⇒ Object

Create a new client providing only app ID and API key

Parameters:

  • app_id (String)

    Algolia application ID

  • api_key (String)

    Algolia API key

Returns:

  • self



33
34
35
36
# File 'lib/algolia/recommend_client.rb', line 33

def self.create(app_id, api_key)
  config = Recommend::Config.new(application_id: app_id, api_key: api_key)
  create_with_config(config)
end

.create_with_config(config) ⇒ Object

Create a new client providing only an Recommend::Config object

Parameters:

Returns:

  • self



44
45
46
# File 'lib/algolia/recommend_client.rb', line 44

def self.create_with_config(config)
  new(config)
end

Instance Method Details

#get_frequently_bought_together(requests, opts = {}) ⇒ Hash

Get frequently bought together items for the given requests

Parameters:

  • requests (Array<Hash>)

    the requests to get frequently bought together items for

  • opts (Hash) (defaults to: {})

    extra parameters to send with your request

Returns:

  • (Hash)


85
86
87
88
89
90
# File 'lib/algolia/recommend_client.rb', line 85

def get_frequently_bought_together(requests, opts = {})
  get_recommendations(
    set_request_models(symbolize_all(requests), Model::BOUGHT_TOGETHER),
    opts
  )
end

#get_recommendations(requests, opts = {}) ⇒ Hash

Get recommendation for the given queries

Parameters:

  • requests (Array<Hash>)

    the queries to retrieve recommendations for

  • opts (Hash) (defaults to: {})

    extra parameters to send with your request

Returns:

  • (Hash)


55
56
57
58
59
60
61
62
# File 'lib/algolia/recommend_client.rb', line 55

def get_recommendations(requests, opts = {})
  @transporter.write(
    :POST,
    '/1/indexes/*/recommendations',
    { requests: format_recommendation_requests(symbolize_all(requests)) },
    opts
  )
end

Get related products for the given requests

Parameters:

  • requests (Array<Hash>)

    the requests to get related products for

  • opts (Hash) (defaults to: {})

    extra parameters to send with your request

Returns:

  • (Hash)


71
72
73
74
75
76
# File 'lib/algolia/recommend_client.rb', line 71

def get_related_products(requests, opts = {})
  get_recommendations(
    set_request_models(symbolize_all(requests), Model::RELATED_PRODUCTS),
    opts
  )
end