Module: Algolia::Protocol

Defined in:
lib/algolia/protocol.rb

Overview

A module which encapsulates the specifics of Algolia’s REST API.

Constant Summary collapse

VERSION =

The version of the REST API implemented by this module.

1
HEADER_APP_ID =

The HTTP header used for passing your application ID to the Algolia API.

"X-Algolia-Application-Id"
HEADER_API_KEY =

The HTTP header used for passing your API key to the Algolia API.

"X-Algolia-API-Key"
HEADER_FORWARDED_IP =
"X-Forwarded-For"
HEADER_FORWARDED_API_KEY =
"X-Forwarded-API-Key"
ERROR_BAD_REQUEST =

HTTP ERROR CODES


400
ERROR_FORBIDDEN =
403
ERROR_NOT_FOUND =
404

Class Method Summary collapse

Class Method Details

.ab_tests_stop_uri(ab_test) ⇒ Object



206
207
208
# File 'lib/algolia/protocol.rb', line 206

def Protocol.ab_tests_stop_uri(ab_test)
  "/2/abtests/#{ab_test}/stop"
end

.ab_tests_uri(ab_test = nil) ⇒ Object



200
201
202
203
204
# File 'lib/algolia/protocol.rb', line 200

def Protocol.ab_tests_uri(ab_test = nil)
  ab_test = "/#{ab_test}" if ab_test

  "/2/abtests" + ab_test.to_s
end

.batch_rules_uri(index) ⇒ Object



166
167
168
# File 'lib/algolia/protocol.rb', line 166

def Protocol.batch_rules_uri(index)
  "#{rules_uri(index)}/batch"
end

.batch_synonyms_uri(index) ⇒ Object



146
147
148
# File 'lib/algolia/protocol.rb', line 146

def Protocol.batch_synonyms_uri(index)
  "#{synonyms_uri(index)}/batch"
end

.batch_uri(index = nil) ⇒ Object



51
52
53
# File 'lib/algolia/protocol.rb', line 51

def Protocol.batch_uri(index = nil)
  "#{index.nil? ? "/#{VERSION}/indexes/*" : index_uri(index)}/batch"
end

.browse_uri(index, params = {}) ⇒ Object



77
78
79
80
# File 'lib/algolia/protocol.rb', line 77

def Protocol.browse_uri(index, params = {})
  params = params.nil? || params.size == 0 ? '' : "?#{to_query(params)}"
  "#{index_uri(index)}/browse#{params}"
end

.clear_rules_uri(index) ⇒ Object



162
163
164
# File 'lib/algolia/protocol.rb', line 162

def Protocol.clear_rules_uri(index)
  "#{rules_uri(index)}/clear"
end

.clear_synonyms_uri(index) ⇒ Object



142
143
144
# File 'lib/algolia/protocol.rb', line 142

def Protocol.clear_synonyms_uri(index)
  "#{synonyms_uri(index)}/clear"
end

.clear_uri(index) ⇒ Object



96
97
98
# File 'lib/algolia/protocol.rb', line 96

def Protocol.clear_uri(index)
  "#{index_uri(index)}/clear"
end

.cluster_mapping_uri(user_id = nil) ⇒ Object



182
183
184
185
186
# File 'lib/algolia/protocol.rb', line 182

def Protocol.cluster_mapping_uri(user_id = nil)
  user_id = "/#{CGI.escape(user_id)}" if user_id

  "/#{VERSION}/clusters/mapping" + user_id.to_s
end

.cluster_top_user_uriObject



192
193
194
# File 'lib/algolia/protocol.rb', line 192

def Protocol.cluster_top_user_uri
  "/#{VERSION}/clusters/mapping/top"
end

.clusters_uriObject



178
179
180
# File 'lib/algolia/protocol.rb', line 178

def Protocol.clusters_uri
  "/#{VERSION}/clusters"
end

.delete_by_uri(index) ⇒ Object



170
171
172
# File 'lib/algolia/protocol.rb', line 170

def Protocol.delete_by_uri(index)
  "#{index_uri(index)}/deleteByQuery"
end

.index_key_uri(index, key) ⇒ Object



116
117
118
# File 'lib/algolia/protocol.rb', line 116

def Protocol.index_key_uri(index, key)
  "#{index_uri(index)}/keys/#{key}"
end

.index_keys_uri(index) ⇒ Object



120
121
122
# File 'lib/algolia/protocol.rb', line 120

def Protocol.index_keys_uri(index)
  "#{index_uri(index)}/keys"
end

.index_operation_uri(index) ⇒ Object



55
56
57
# File 'lib/algolia/protocol.rb', line 55

def Protocol.index_operation_uri(index)
  "#{index_uri(index)}/operation"
end

.index_uri(index) ⇒ Object

Construct a uri referencing a given Algolia index



47
48
49
# File 'lib/algolia/protocol.rb', line 47

def Protocol.index_uri(index)
  "/#{VERSION}/indexes/#{CGI.escape(index)}"
end

.indexes_uriObject

Construct a uri to list available indexes



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

def Protocol.indexes_uri
  "/#{VERSION}/indexes"
end

.key_uri(key) ⇒ Object



108
109
110
# File 'lib/algolia/protocol.rb', line 108

def Protocol.key_uri(key)
  "/#{VERSION}/keys/#{key}"
end

.keys_uriObject



104
105
106
# File 'lib/algolia/protocol.rb', line 104

def Protocol.keys_uri
  "/#{VERSION}/keys"
end

.list_ids_uri(page, hits_per_page) ⇒ Object



188
189
190
# File 'lib/algolia/protocol.rb', line 188

def Protocol.list_ids_uri(page, hits_per_page)
  Protocol.cluster_mapping_uri+"?page=#{CGI.escape(page.to_s)}&hitsPerPage=#{CGI.escape(hits_per_page.to_s)}"
end

.logs(offset, length, type) ⇒ Object



100
101
102
# File 'lib/algolia/protocol.rb', line 100

def Protocol.logs(offset, length, type)
  "/#{VERSION}/logs?offset=#{offset}&length=#{length}&type=#{type}"
end

.multiple_queries_uri(strategy = 'none') ⇒ Object



38
39
40
# File 'lib/algolia/protocol.rb', line 38

def Protocol.multiple_queries_uri(strategy = 'none')
  "/#{VERSION}/indexes/*/queries?strategy=#{strategy}"
end

.object_uri(index, object_id, params = {}) ⇒ Object



63
64
65
66
# File 'lib/algolia/protocol.rb', line 63

def Protocol.object_uri(index, object_id, params = {})
  params = params.nil? || params.size == 0 ? '' : "?#{to_query(params)}"
  "#{index_uri(index)}/#{CGI.escape(object_id.to_s)}#{params}"
end

.objects_uriObject



42
43
44
# File 'lib/algolia/protocol.rb', line 42

def Protocol.objects_uri
  "/#{VERSION}/indexes/*/objects"
end

.partial_object_uri(index, object_id, create_if_not_exits = true) ⇒ Object



86
87
88
89
# File 'lib/algolia/protocol.rb', line 86

def Protocol.partial_object_uri(index, object_id, create_if_not_exits = true)
  params = create_if_not_exits ? '' : '?createIfNotExists=false'
  "#{index_uri(index)}/#{CGI.escape(object_id.to_s)}/partial#{params}"
end

.personalization_strategy_uriObject



174
175
176
# File 'lib/algolia/protocol.rb', line 174

def Protocol.personalization_strategy_uri
  "/1/recommendation/personalization/strategy"
end

.restore_key_uri(key) ⇒ Object



112
113
114
# File 'lib/algolia/protocol.rb', line 112

def Protocol.restore_key_uri(key)
  "/#{VERSION}/keys/#{key}/restore"
end

.rule_uri(index, object_id) ⇒ Object



154
155
156
# File 'lib/algolia/protocol.rb', line 154

def Protocol.rule_uri(index, object_id)
  "#{rules_uri(index)}/#{CGI.escape(object_id.to_s)}"
end

.rules_uri(index) ⇒ Object



150
151
152
# File 'lib/algolia/protocol.rb', line 150

def Protocol.rules_uri(index)
  "#{index_uri(index)}/rules"
end

.search_facet_uri(index, facet) ⇒ Object



82
83
84
# File 'lib/algolia/protocol.rb', line 82

def Protocol.search_facet_uri(index, facet)
  "#{index_uri(index)}/facets/#{CGI.escape(facet)}/query"
end

.search_post_uri(index) ⇒ Object



73
74
75
# File 'lib/algolia/protocol.rb', line 73

def Protocol.search_post_uri(index)
  "#{index_uri(index)}/query"
end

.search_rules_uri(index) ⇒ Object



158
159
160
# File 'lib/algolia/protocol.rb', line 158

def Protocol.search_rules_uri(index)
  "#{rules_uri(index)}/search"
end

.search_synonyms_uri(index) ⇒ Object



138
139
140
# File 'lib/algolia/protocol.rb', line 138

def Protocol.search_synonyms_uri(index)
  "#{synonyms_uri(index)}/search"
end

.search_uri(index, query, params = {}) ⇒ Object



68
69
70
71
# File 'lib/algolia/protocol.rb', line 68

def Protocol.search_uri(index, query, params = {})
  params = params.nil? || params.size == 0 ? '' : "&#{to_query(params)}"
  "#{index_uri(index)}?query=#{CGI.escape(query)}&#{params}"
end

.search_user_id_uriObject



196
197
198
# File 'lib/algolia/protocol.rb', line 196

def Protocol.search_user_id_uri
  "/#{VERSION}/clusters/mapping/search"
end

.settings_uri(index, params = {}) ⇒ Object



91
92
93
94
# File 'lib/algolia/protocol.rb', line 91

def Protocol.settings_uri(index, params = {})
  params = params.nil? || params.size == 0 ? '' : "?#{to_query(params)}"
  "#{index_uri(index)}/settings#{params}"
end

.synonym_uri(index, object_id) ⇒ Object



134
135
136
# File 'lib/algolia/protocol.rb', line 134

def Protocol.synonym_uri(index, object_id)
  "#{synonyms_uri(index)}/#{CGI.escape(object_id.to_s)}"
end

.synonyms_uri(index) ⇒ Object



130
131
132
# File 'lib/algolia/protocol.rb', line 130

def Protocol.synonyms_uri(index)
  "#{index_uri(index)}/synonyms"
end

.task_uri(index, task_id) ⇒ Object



59
60
61
# File 'lib/algolia/protocol.rb', line 59

def Protocol.task_uri(index, task_id)
  "#{index_uri(index)}/task/#{task_id}"
end

.to_query(params) ⇒ Object



124
125
126
127
128
# File 'lib/algolia/protocol.rb', line 124

def Protocol.to_query(params)
  params.map do |k, v|
    "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
  end.join('&')
end