Class: Gcloud::Search::Project

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

Overview

Project

Projects are top-level containers in Google Cloud Platform. They store information about billing and authorized users, and they control access to Google Cloud Search resources. Each project has a friendly name and a unique ID. Projects can be created only in the Google Developers Console.

require "gcloud"

gcloud = Gcloud.new
search = gcloud.search
index = search.index "books"

See Gcloud#search

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, credentials) ⇒ Project

Creates a new Connection instance.

See Gcloud.search



49
50
51
52
53
# File 'lib/gcloud/search/project.rb', line 49

def initialize project, credentials #:nodoc:
  project = project.to_s # Always cast to a string
  fail ArgumentError, "project is missing" if project.empty?
  @connection = Connection.new project, credentials
end

Instance Attribute Details

#connectionObject

The Connection object.



43
44
45
# File 'lib/gcloud/search/project.rb', line 43

def connection
  @connection
end

Class Method Details

.default_projectObject

Default project.



73
74
75
76
77
78
# File 'lib/gcloud/search/project.rb', line 73

def self.default_project #:nodoc:
  ENV["SEARCH_PROJECT"] ||
    ENV["GCLOUD_PROJECT"] ||
    ENV["GOOGLE_CLOUD_PROJECT"] ||
    Gcloud::GCE.project_id
end

Instance Method Details

#index(index_id, skip_lookup: false) ⇒ Object

Retrieves an existing index by ID.

Parameters

index_id

The ID of an index. (String)

skip_lookup

Optionally create an Index object without verifying the index resource exists on the Search service. Documents saved on this object will create the index resource if the resource does not yet exist. Default is false. (Boolean)

Returns

Gcloud::Search::Index or nil if the index does not exist

Examples

require "gcloud"

gcloud = Gcloud.new
search = gcloud.search

index = search.index "books"
index.index_id #=> "books"

A new index can be created by providing the desired index_id and the skip_lookup option:

require "gcloud"

gcloud = Gcloud.new
search = gcloud.search

index = search.index "more-books"
index #=> nil
index = search.index "more-books", skip_lookup: true
index.index_id #=> "more-books"


120
121
122
123
124
125
126
127
128
# File 'lib/gcloud/search/project.rb', line 120

def index index_id, skip_lookup: false
  if skip_lookup
    index_hash = { "indexId" => index_id, "projectId" => project }
    return Gcloud::Search::Index.from_raw index_hash, connection
  end
  indexes(prefix: index_id).all.detect do |ix|
    ix.index_id == index_id
  end
end

#indexes(prefix: nil, token: nil, max: nil) ⇒ Object

Retrieves the list of indexes belonging to the project.

Parameters

prefix

The prefix of the index name. It is used to list all indexes with names that have this prefix. (String)

token

A previously-returned page token representing part of the larger set of results to view. (String)

max

Maximum number of indexes to return. The default is 100. (Integer)

Returns

Array of Gcloud::Search::Index (See Gcloud::Search::Index::List)

Examples

require "gcloud"

gcloud = Gcloud.new
search = gcloud.search

indexes = search.indexes
indexes.each do |index|
  puts index.index_id
end

If you have a significant number of indexes, you may need to paginate through them: (See Gcloud::Search::Index::List)

require "gcloud"

gcloud = Gcloud.new
search = gcloud.search

indexes = search.indexes
loop do
  indexes.each do |index|
    puts index.index_id
  end
  break unless indexes.next?
  indexes = indexes.next
end


177
178
179
180
181
182
183
184
185
186
# File 'lib/gcloud/search/project.rb', line 177

def indexes prefix: nil, token: nil, max: nil
  ensure_connection!
  options = { prefix: prefix, token: token, max: max }
  resp = connection.list_indexes options
  if resp.success?
    Index::List.from_response resp, connection
  else
    fail ApiError.from_response(resp)
  end
end

#projectObject

The ID of the current project.

Example

require "gcloud"

gcloud = Gcloud.new "my-project", "/path/to/keyfile.json"
search = gcloud.search

search.project #=> "my-project"


67
68
69
# File 'lib/gcloud/search/project.rb', line 67

def project
  connection.project
end