gcloud 0.7.2 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,90 +0,0 @@
1
- # Copyright 2015 Google Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- module Gcloud
17
- module Search
18
- class Index
19
- ##
20
- # Index::List is a special case Array with additional values.
21
- class List < DelegateClass(::Array)
22
- ##
23
- # If not empty, indicates that there are more records that match
24
- # the request and this value should be passed to continue.
25
- attr_accessor :token
26
-
27
- ##
28
- # Create a new Index::List with an array of {Index} instances.
29
- def initialize arr = []
30
- super arr
31
- end
32
-
33
- ##
34
- # Whether there a next page of indexes.
35
- def next?
36
- !token.nil?
37
- end
38
-
39
- ##
40
- # Retrieve the next page of indexes.
41
- def next
42
- return nil unless next?
43
- ensure_connection!
44
- resp = @connection.list_indexes token: token
45
- if resp.success?
46
- Index::List.from_response resp, @connection
47
- else
48
- fail ApiError.from_response(resp)
49
- end
50
- end
51
-
52
- ##
53
- # Retrieves all indexes by repeatedly loading pages until {#next?}
54
- # returns false. Returns the list instance for method chaining.
55
- def all
56
- while next?
57
- next_indexes = self.next
58
- push(*next_indexes)
59
- self.token = next_indexes.token
60
- end
61
- self
62
- end
63
-
64
- ##
65
- # @private New Index::List from a response object.
66
- def self.from_response resp, conn
67
- data = JSON.parse resp.body
68
- indexes = new(Array(data["indexes"]).map do |raw_index|
69
- Index.from_raw raw_index, conn
70
- end)
71
- indexes.instance_eval do
72
- @token = data["nextPageToken"]
73
- @connection = conn
74
- end
75
- indexes
76
- rescue JSON::ParserError
77
- raise ApiError.from_response(resp)
78
- end
79
-
80
- protected
81
-
82
- ##
83
- # Raise an error unless an active connection is available.
84
- def ensure_connection!
85
- fail "Must have active connection" unless @connection
86
- end
87
- end
88
- end
89
- end
90
- end
@@ -1,181 +0,0 @@
1
- # Copyright 2015 Google Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- require "gcloud/gce"
17
- require "gcloud/search/connection"
18
- require "gcloud/search/credentials"
19
- require "gcloud/search/index"
20
- require "gcloud/search/errors"
21
-
22
- module Gcloud
23
- module Search
24
- ##
25
- # # Project
26
- #
27
- # Projects are top-level containers in Google Cloud Platform. They store
28
- # information about billing and authorized users, and they control access to
29
- # Google Cloud Search resources. Each project has a friendly name and a
30
- # unique ID. Projects can be created only in the [Google Developers
31
- # Console](https://console.developers.google.com). See {Gcloud#search}.
32
- #
33
- # @example
34
- # require "gcloud"
35
- #
36
- # gcloud = Gcloud.new
37
- # search = gcloud.search
38
- # index = search.index "books"
39
- #
40
- class Project
41
- ##
42
- # @private The Connection object.
43
- attr_accessor :connection
44
-
45
- ##
46
- # @private Creates a new Connection instance.
47
- #
48
- # See Gcloud.search
49
- def initialize project, credentials
50
- project = project.to_s # Always cast to a string
51
- fail ArgumentError, "project is missing" if project.empty?
52
- @connection = Connection.new project, credentials
53
- end
54
-
55
- ##
56
- # The ID of the current project.
57
- #
58
- # @return [String]
59
- #
60
- # @example
61
- # require "gcloud"
62
- #
63
- # gcloud = Gcloud.new "my-project", "/path/to/keyfile.json"
64
- # search = gcloud.search
65
- #
66
- # search.project #=> "my-project"
67
- #
68
- def project
69
- connection.project
70
- end
71
-
72
- ##
73
- # @private Default project.
74
- def self.default_project
75
- ENV["SEARCH_PROJECT"] ||
76
- ENV["GCLOUD_PROJECT"] ||
77
- ENV["GOOGLE_CLOUD_PROJECT"] ||
78
- Gcloud::GCE.project_id
79
- end
80
-
81
- ##
82
- # Retrieves an existing index by ID.
83
- #
84
- # @param [String] index_id The ID of an index.
85
- # @param [Boolean] skip_lookup Optionally create an Index object without
86
- # verifying the index resource exists on the Search service. Documents
87
- # saved on this object will create the index resource if the resource
88
- # does not yet exist. Default is `false`.
89
- #
90
- # @return [Gcloud::Search::Index, nil] nil if the index does not exist
91
- #
92
- # @example
93
- # require "gcloud"
94
- #
95
- # gcloud = Gcloud.new
96
- # search = gcloud.search
97
- #
98
- # index = search.index "books"
99
- # index.index_id #=> "books"
100
- #
101
- # @example A new index can be created with `index_id` and `skip_lookup`:
102
- # require "gcloud"
103
- #
104
- # gcloud = Gcloud.new
105
- # search = gcloud.search
106
- #
107
- # index = search.index "more-books"
108
- # index #=> nil
109
- # index = search.index "more-books", skip_lookup: true
110
- # index.index_id #=> "more-books"
111
- #
112
- def index index_id, skip_lookup: false
113
- if skip_lookup
114
- index_hash = { "indexId" => index_id, "projectId" => project }
115
- return Gcloud::Search::Index.from_raw index_hash, connection
116
- end
117
- indexes(prefix: index_id).all.detect do |ix|
118
- ix.index_id == index_id
119
- end
120
- end
121
-
122
- ##
123
- # Retrieves the list of indexes belonging to the project.
124
- #
125
- # @param [String] prefix The prefix of the index name. It is used to list
126
- # all indexes with names that have this prefix.
127
- # @param [String] token A previously-returned page token representing part
128
- # of the larger set of results to view.
129
- # @param [Integer] max Maximum number of indexes to return. The default is
130
- # `100`.
131
- #
132
- # @return [Array<Gcloud::Search::Index>] (See
133
- # {Gcloud::Search::Index::List})
134
- #
135
- # @example
136
- # require "gcloud"
137
- #
138
- # gcloud = Gcloud.new
139
- # search = gcloud.search
140
- #
141
- # indexes = search.indexes
142
- # indexes.each do |index|
143
- # puts index.index_id
144
- # end
145
- #
146
- # @example Using pagination: (See {Gcloud::Search::Index::List})
147
- # require "gcloud"
148
- #
149
- # gcloud = Gcloud.new
150
- # search = gcloud.search
151
- #
152
- # indexes = search.indexes
153
- # loop do
154
- # indexes.each do |index|
155
- # puts index.index_id
156
- # end
157
- # break unless indexes.next?
158
- # indexes = indexes.next
159
- # end
160
- #
161
- def indexes prefix: nil, token: nil, max: nil
162
- ensure_connection!
163
- options = { prefix: prefix, token: token, max: max }
164
- resp = connection.list_indexes options
165
- if resp.success?
166
- Index::List.from_response resp, connection
167
- else
168
- fail ApiError.from_response(resp)
169
- end
170
- end
171
-
172
- protected
173
-
174
- ##
175
- # Raise an error unless an active connection is available.
176
- def ensure_connection!
177
- fail "Must have active connection" unless connection
178
- end
179
- end
180
- end
181
- end
@@ -1,165 +0,0 @@
1
- # Copyright 2015 Google Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- require "gcloud/search/result/list"
17
- require "gcloud/search/fields"
18
-
19
- module Gcloud
20
- module Search
21
- ##
22
- # # Result
23
- #
24
- # See {Gcloud#search}
25
- class Result
26
- ##
27
- # @private Creates a new Result instance.
28
- def initialize
29
- @fields = Fields.new
30
- @raw = {}
31
- end
32
-
33
- ##
34
- # The unique identifier of the document referenced in the search result.
35
- #
36
- # @return [String]
37
- def doc_id
38
- @raw["docId"]
39
- end
40
-
41
- ##
42
- # The token for the next page of results.
43
- #
44
- # @return [String]
45
- def token
46
- @raw["nextPageToken"]
47
- end
48
-
49
- ##
50
- # Retrieve the field values associated to a field name.
51
- #
52
- # @param [String] name The name of the field. New values will be
53
- # configured with this name.
54
- #
55
- # @return [FieldValue]
56
- #
57
- # @example
58
- # require "gcloud"
59
- #
60
- # gcloud = Gcloud.new
61
- # search = gcloud.search
62
- # index = search.index "products"
63
- #
64
- # documents = index.search "best T-shirt ever"
65
- # document = documents.first
66
- # puts "The best match for your search is:"
67
- # document["description"].each do |value|
68
- # puts "* #{value} (#{value.type}) [#{value.lang}]"
69
- # end
70
- #
71
- def [] name
72
- @fields[name]
73
- end
74
-
75
- # Trivial accessor because we want .fields to be listed with methods.
76
-
77
- ##
78
- # The fields in the search result. Each field has a name (String) and a
79
- # list of values ({FieldValues}). (See {Fields})
80
- def fields
81
- @fields
82
- end
83
-
84
- ##
85
- # Calls block once for each field, passing the field name and values pair
86
- # as parameters. If no block is given an enumerator is returned instead.
87
- # (See {Fields#each})
88
- #
89
- # @example
90
- # require "gcloud"
91
- #
92
- # gcloud = Gcloud.new
93
- # search = gcloud.search
94
- # index = search.index "products"
95
- #
96
- # documents = index.search "best T-shirt ever"
97
- # document = documents.first
98
- # puts "The best match for your search is:"
99
- # document.each do |name, values|
100
- # puts "* #{name}:"
101
- # values.each do |value|
102
- # puts " * #{value} (#{value.type})"
103
- # end
104
- # end
105
- #
106
- def each &block
107
- @fields.each(&block)
108
- end
109
-
110
- ##
111
- # Returns a new array populated with all the field names.
112
- # (See {Fields#names})
113
- #
114
- # @return [Array<String>]
115
- #
116
- # @example
117
- # require "gcloud"
118
- #
119
- # gcloud = Gcloud.new
120
- # search = gcloud.search
121
- # index = search.index "products"
122
- #
123
- # documents = index.search "best T-shirt ever"
124
- # document = documents.first
125
- # puts "The best match has the following fields:"
126
- # document.names.each do |name|
127
- # puts "* #{name}:"
128
- # end
129
- #
130
- def names
131
- @fields.names
132
- end
133
-
134
- ##
135
- # @private Override to keep working in interactive shells manageable.
136
- def inspect
137
- insp_token = ""
138
- if token
139
- trunc_token = "#{token[0, 8]}...#{token[-5..-1]}"
140
- trunc_token = token if token.length < 20
141
- insp_token = ", token: #{trunc_token.inspect}"
142
- end
143
- insp_fields = ", fields: (#{fields.names.map(&:inspect).join ', '})"
144
- "#{self.class}(doc_id: #{doc_id.inspect}#{insp_token}#{insp_fields})"
145
- end
146
-
147
- ##
148
- # @private New Result from a raw data object.
149
- def self.from_hash hash
150
- result = new
151
- result.instance_variable_set "@raw", hash
152
- result.instance_variable_set "@fields", Fields.from_raw(hash["fields"])
153
- result
154
- end
155
-
156
- ##
157
- # @private Returns the Result data as a hash
158
- def to_hash
159
- hash = @raw.dup
160
- hash["fields"] = @fields.to_raw
161
- hash
162
- end
163
- end
164
- end
165
- end
@@ -1,95 +0,0 @@
1
- # Copyright 2015 Google Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- module Gcloud
17
- module Search
18
- class Result
19
- ##
20
- # Result::List is a special case Array with additional values.
21
- class List < DelegateClass(::Array)
22
- ##
23
- # If not empty, indicates that there are more records that match
24
- # the request and this value should be passed to continue.
25
- attr_reader :token
26
-
27
- ##
28
- # The number of documents that match the query. It is greater than or
29
- # equal to the number of documents actually returned. This is an
30
- # approximation and not an exact count unless it is less than or equal
31
- # to the {Index#search} `matched_count_accuracy` option.
32
- attr_reader :matched_count
33
-
34
- ##
35
- # Create a new Result::List with an array of {Result} instances.
36
- def initialize arr = []
37
- super arr
38
- end
39
-
40
- ##
41
- # Whether there a next page of results.
42
- def next?
43
- !token.nil?
44
- end
45
-
46
- ##
47
- # Retrieve the next page of results.
48
- def next
49
- return nil unless next?
50
- ensure_index!
51
- @index.search @query, @search_options.merge(token: token)
52
- end
53
-
54
- ##
55
- # Retrieves all results by repeatedly loading pages until {#next?}
56
- # returns false. Returns the list instance for method chaining.
57
- def all
58
- while next?
59
- next_results = self.next
60
- push(*next_results)
61
- self.token = next_results.token
62
- end
63
- self
64
- end
65
-
66
- ##
67
- # @private New Result::List from a response object.
68
- def self.from_response resp, index, query, search_options
69
- data = JSON.parse resp.body
70
- results = new(Array(data["results"]).map do |raw|
71
- Result.from_hash raw
72
- end)
73
- results.instance_eval do
74
- @token = data["results"].last["nextPageToken"]
75
- @matched_count = data["matchedCount"]
76
- @index = index
77
- @query = query
78
- @search_options = search_options
79
- end
80
- results
81
- rescue JSON::ParserError
82
- ApiError.from_response_status resp
83
- end
84
-
85
- protected
86
-
87
- ##
88
- # Raise an error unless an active connection is available.
89
- def ensure_index!
90
- fail "Must have active connection" unless @index
91
- end
92
- end
93
- end
94
- end
95
- end