Class: Gcloud::Search::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/search/connection.rb

Overview

Represents the connection to Search, as well as expose the API calls.

Constant Summary collapse

API_VERSION =

:nodoc:

"v1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, credentials) ⇒ Connection

Creates a new Connection instance.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gcloud/search/connection.rb', line 34

def initialize project, credentials #:nodoc:
  @project = project
  @credentials = credentials
  client_config = {
    application_name: "gcloud-ruby",
    application_version: Gcloud::VERSION
  }
  @client = Gcloud::Search::APIClient.new client_config
  @client.authorization = @credentials.client
  @search = @client.discovered_api "cloudsearch", API_VERSION
end

Instance Attribute Details

#clientObject

:nodoc:



29
30
31
# File 'lib/gcloud/search/connection.rb', line 29

def client
  @client
end

#connectionObject

:nodoc:



30
31
32
# File 'lib/gcloud/search/connection.rb', line 30

def connection
  @connection
end

#credentialsObject

:nodoc:



28
29
30
# File 'lib/gcloud/search/connection.rb', line 28

def credentials
  @credentials
end

#projectObject

Returns the value of attribute project.



27
28
29
# File 'lib/gcloud/search/connection.rb', line 27

def project
  @project
end

Instance Method Details

#create_doc(index_id, document_hash) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/gcloud/search/connection.rb', line 91

def create_doc index_id, document_hash
  @client.execute(
    api_method: @search.documents.create,
    parameters: { projectId: @project,
                  indexId: index_id },
    body_object: document_hash
  )
end

#delete_doc(index_id, doc_id) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/gcloud/search/connection.rb', line 100

def delete_doc index_id, doc_id
  @client.execute(
    api_method: @search.documents.delete,
    parameters: { projectId: @project,
                  indexId: index_id,
                  docId: doc_id }
  )
end

#delete_index(index_id) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/gcloud/search/connection.rb', line 60

def delete_index index_id
  @client.execute(
    api_method: @search.indexes.delete,
    parameters: { projectId: @project,
                  indexId: index_id }
  )
end

#get_doc(index_id, doc_id) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/gcloud/search/connection.rb', line 68

def get_doc index_id, doc_id
  @client.execute(
    api_method: @search.documents.get,
    parameters: { projectId: @project,
                  indexId: index_id,
                  docId: doc_id }
  )
end

#inspectObject

:nodoc:



123
124
125
# File 'lib/gcloud/search/connection.rb', line 123

def inspect #:nodoc:
  "#{self.class}(#{@project})"
end

#list_docs(index_id, options = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gcloud/search/connection.rb', line 77

def list_docs index_id, options = {}
  params = { projectId: @project,
             indexId: index_id,
             view: (options[:view] || "FULL"),
             pageSize: options[:max],
             pageToken: options[:token]
           }.delete_if { |_, v| v.nil? }

  @client.execute(
    api_method: @search.documents.list,
    parameters: params
  )
end

#list_indexes(options = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gcloud/search/connection.rb', line 46

def list_indexes options = {}
  params = { projectId: @project,
             indexNamePrefix: options[:prefix],
             view: (options[:view] || "FULL"),
             pageSize: options[:max],
             pageToken: options[:token]
  }.delete_if { |_, v| v.nil? }

  @client.execute(
    api_method: @search.indexes.list,
    parameters: params
  )
end

#search(index_id, query, options = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gcloud/search/connection.rb', line 109

def search index_id, query, options = {}
  # Always encode expression hashes as JSON strings
  if options[:expressions]
    # Force to an array of hashes, this works with an array or a hash
    tmp = [options[:expressions]].flatten.map { |ex| JSON.dump ex }
    options[:expressions] = tmp
  end

  @client.execute(
    api_method: @search.indexes.search,
    parameters: search_request(index_id, query, options)
  )
end