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,85 +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 Document
19
- ##
20
- # Document::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 Document::List with an array of {Document} instances.
29
- def initialize arr = []
30
- super arr
31
- end
32
-
33
- ##
34
- # Whether there a next page of documents.
35
- def next?
36
- !token.nil?
37
- end
38
-
39
- ##
40
- # Retrieve the next page of documents.
41
- def next
42
- return nil unless next?
43
- ensure_index!
44
- @index.documents token: token
45
- end
46
-
47
- ##
48
- # Retrieves all documents by repeatedly loading pages until {#next?}
49
- # returns false. Returns the list instance for method chaining.
50
- def all
51
- while next?
52
- next_documents = self.next
53
- push(*next_documents)
54
- self.token = next_documents.token
55
- end
56
- self
57
- end
58
-
59
- ##
60
- # @private New Documents::List from a response object.
61
- def self.from_response resp, index
62
- data = JSON.parse resp.body
63
- documents = new(Array(data["documents"]).map do |doc_hash|
64
- Document.from_hash doc_hash
65
- end)
66
- documents.instance_eval do
67
- @token = data["nextPageToken"]
68
- @index = index
69
- end
70
- documents
71
- rescue JSON::ParserError
72
- raise ApiError.from_response(resp)
73
- end
74
-
75
- protected
76
-
77
- ##
78
- # Raise an error unless an active connection is available.
79
- def ensure_index!
80
- fail "Must have active connection" unless @index
81
- end
82
- end
83
- end
84
- end
85
- end
@@ -1,158 +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
- ##
19
- # # FieldValue
20
- #
21
- # FieldValue is used to represent a value that belongs to a field. (See
22
- # {Fields} and {FieldValues})
23
- #
24
- # A field value must have a type. A value that is a Numeric will default to
25
- # `:number`, while a DateTime will default to `:datetime`. If a type is not
26
- # provided it will be determined by looking at the value.
27
- #
28
- # String values (text, html, atom) can also specify a lang value, which is
29
- # an [ISO 639-1
30
- # code](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes).
31
- #
32
- # @example
33
- # require "gcloud"
34
- #
35
- # gcloud = Gcloud.new
36
- # search = gcloud.search
37
- # index = search.index "products"
38
- #
39
- # document = index.document "product-sku-000001"
40
- # puts "The document description is:"
41
- # document["description"].each do |value|
42
- # puts "* #{value} (#{value.type}) [#{value.lang}]"
43
- # end
44
- #
45
- # @see https://cloud.google.com/search/documents_indexes Documents and
46
- # fields
47
- #
48
- class FieldValue < DelegateClass(::Object)
49
- attr_reader :type, :lang, :name
50
-
51
- # rubocop:disable Metrics/LineLength
52
- # Disabled because there are links in the docs that are long.
53
-
54
- ##
55
- # @private Create a new FieldValue object.
56
- #
57
- # @param [String, Datetime, Float] value The value to add to the field.
58
- # @param [Symbol] type The type of the field value. A field can have multiple values with
59
- # same or different types; however, it cannot have multiple Timestamp or
60
- # number values.
61
- #
62
- # The following values are supported:
63
- #
64
- # * `:text` - The value is a string with maximum length 1024**2
65
- # characters.
66
- # * `:html` - The value is an HTML-formatted string with maximum length
67
- # 1024**2 characters.
68
- # * `:atom` - The value is a string with maximum length 500 characters.
69
- # * `:geo` - The value is a point on earth described by latitude and
70
- # longitude coordinates, represented in string with any of the listed
71
- # [ways of writing
72
- # coordinates](http://en.wikipedia.org/wiki/Geographic_coordinate_conversion).
73
- # * `:datetime` - The value is a `DateTime`.
74
- # * `:number` - The value is a `Numeric` between -2,147,483,647 and
75
- # 2,147,483,647. The value will be stored as a double precision
76
- # floating point value in Cloud Search.
77
- # @param [String] lang The language of a string value. Must be a valid [ISO 639-1
78
- # code](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes).
79
- # @param [String] name The name of the field. New values will be configured with this name.
80
- #
81
- def initialize value, type: nil, lang: nil, name: nil
82
- super value
83
- if type
84
- @type = type.to_s.downcase.to_sym
85
- else
86
- @type = infer_type
87
- end
88
- @lang = lang if string_type?
89
- @name = name
90
- end
91
-
92
- # rubocop:enable Metrics/LineLength
93
-
94
- ##
95
- # Determines if the value a string type. The value is text or html or atom
96
- # (or default).
97
- def string_type?
98
- [:atom, :default, :html, :text].include? type
99
- end
100
-
101
- ##
102
- # @private Get the original value object.
103
- def value
104
- __getobj__
105
- end
106
-
107
- ##
108
- # @private Create a new FieldValue instance from a value Hash.
109
- def self.from_raw field_value, name = nil
110
- value = field_value["stringValue"]
111
- type = field_value["stringFormat"]
112
- if field_value["timestampValue"]
113
- value = DateTime.parse(field_value["timestampValue"])
114
- type = :datetime
115
- elsif field_value["geoValue"]
116
- value = field_value["geoValue"]
117
- type = :geo
118
- elsif field_value["numberValue"]
119
- value = Float(field_value["numberValue"])
120
- type = :number
121
- end
122
- fail "No value found in #{raw_field.inspect}" if value.nil?
123
- new value, type: type, lang: field_value["lang"], name: name
124
- end
125
-
126
- ##
127
- # @private Create a raw Hash object containing the field value.
128
- def to_raw
129
- case type
130
- when :atom, :default, :html, :text
131
- {
132
- "stringFormat" => type.to_s.upcase,
133
- "lang" => lang,
134
- "stringValue" => to_s
135
- }.delete_if { |_, v| v.nil? }
136
- when :geo
137
- { "geoValue" => to_s }
138
- when :number
139
- { "numberValue" => to_f }
140
- when :datetime
141
- { "timestampValue" => rfc3339 }
142
- end
143
- end
144
-
145
- protected
146
-
147
- def infer_type
148
- if respond_to? :rfc3339
149
- :datetime
150
- elsif value.is_a? Numeric # must call on original object...
151
- :number
152
- else
153
- :default
154
- end
155
- end
156
- end
157
- end
158
- end
@@ -1,233 +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/field_value"
17
-
18
- module Gcloud
19
- module Search
20
- ##
21
- # # FieldValues
22
- #
23
- # The list of values for a field.
24
- #
25
- # Each field has a name (String) and a list of values. Each field name
26
- # consists of only ASCII characters, must be unique within the document and
27
- # is case sensitive. A field name must start with a letter and can contain
28
- # letters, digits, or underscore, with a maximum of 500 characters.
29
- #
30
- # Each field on a document can have multiple values. FieldValues is the
31
- # object that manages the multiple values. Values can be the same or
32
- # different types; however, it cannot have multiple datetime (DateTime) or
33
- # number (Float) values. (See {FieldValue})
34
- #
35
- # @example
36
- # require "gcloud"
37
- #
38
- # gcloud = Gcloud.new
39
- # search = gcloud.search
40
- # index = search.index "products"
41
- #
42
- # document = index.document "product-sku-000001"
43
- # puts "The document description is:"
44
- # document["description"].each do |value|
45
- # puts "* #{value} (#{value.type}) [#{value.lang}]"
46
- # end
47
- #
48
- # @see https://cloud.google.com/search/documents_indexes Documents and
49
- # Indexes
50
- #
51
- class FieldValues
52
- include Enumerable
53
-
54
- ##
55
- # @private Create a new FieldValues object.
56
- #
57
- # @param [String] name The name of the field. New values will be
58
- # configured with this name.
59
- # @param [Array<FieldValue>] values A list of values to add to the field.
60
- #
61
- def initialize name, values = []
62
- @name = name
63
- @values = values
64
- end
65
-
66
- ##
67
- # Returns the element at index, or returns a subarray starting at the
68
- # start index and continuing for length elements, or returns a subarray
69
- # specified by range of indices.
70
- #
71
- # Negative indices count backward from the end of the array (-1 is the
72
- # last element). For start and range cases the starting index is just
73
- # before an element. Additionally, an empty array is returned when the
74
- # starting index for an element range is at the end of the array.
75
- #
76
- # @return [FieldValue, nil] Returns nil if the index (or starting index)
77
- # are out of range.
78
- def [] index
79
- @values[index]
80
- end
81
-
82
- ##
83
- # Add a new value. If the field name does not exist it will be added. If
84
- # the field value is a DateTime or Numeric, or the type is set to
85
- # `:datetime` or `:number`, then the added value will replace any existing
86
- # values of the same type (since there can be only one).
87
- #
88
- # @param [String, Datetime, Float] value The value to add to the field.
89
- # @param [Symbol] type The type of the field value. An attempt is made to
90
- # set the correct type when this option is missing, although it must be
91
- # provided for `:geo` values. A field can have multiple values with same
92
- # or different types; however, it cannot have multiple `:datetime` or
93
- # `:number` values.
94
- #
95
- # The following values are supported:
96
- #
97
- # * `:default` - The value is a string. The format will be automatically
98
- # detected. This is the default value for strings.
99
- # * `:text` - The value is a string with maximum length 1024**2
100
- # characters.
101
- # * `:html` - The value is an HTML-formatted string with maximum length
102
- # 1024**2 characters.
103
- # * `:atom` - The value is a string with maximum length 500 characters.
104
- # * `:geo` - The value is a point on earth described by latitude and
105
- # longitude coordinates, represented in string with any of the listed
106
- # [ways of writing coordinates](http://en.wikipedia.org/wiki/Geographic_coordinate_conversion).
107
- # * `:datetime` - The value is a `DateTime`.
108
- # * `:number` - The value is a `Numeric` between -2,147,483,647 and
109
- # 2,147,483,647. The value will be stored as a double precision
110
- # floating point value in Cloud Search.
111
- # @param [String] lang The language of a string value. Must be a valid
112
- # [ISO 639-1 code](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes).
113
- #
114
- # @return [FieldValue]
115
- #
116
- # @example
117
- # require "gcloud"
118
- #
119
- # gcloud = Gcloud.new
120
- # search = gcloud.search
121
- # index = search.index "products"
122
- #
123
- # document = index.document "product-sku-000001"
124
- # document["sku"].add "product-sku-000001", type: :atom
125
- # document["description"].add "The best T-shirt ever.",
126
- # type: :text, lang: "en"
127
- # document["description"].add "<p>The best T-shirt ever.</p>",
128
- # type: :html, lang: "en"
129
- # document["price"].add 24.95
130
- #
131
- def add value, type: nil, lang: nil
132
- new_field = FieldValue.new value, type: type, lang: lang, name: @name
133
- if [:datetime, :number].include? new_field.type
134
- @values.delete_if { |v| v.type == new_field.type }
135
- end
136
- @values << new_field
137
- end
138
-
139
- ##
140
- # Deletes all values that are equal to value.
141
- #
142
- # @param [String] value The value to remove from the list of values.
143
- #
144
- # @return [FieldValue, nil] The last deleted `FieldValue`, or `nil` if no
145
- # matching value is found.
146
- #
147
- # @example
148
- # require "gcloud"
149
- #
150
- # gcloud = Gcloud.new
151
- # search = gcloud.search
152
- # index = search.index "products"
153
- #
154
- # document = index.document "product-sku-000001"
155
- # document["description"].count #=> 2
156
- # document["description"].delete "The best T-shirt ever."
157
- # document["description"].count #=> 1
158
- #
159
- def delete value, &block
160
- fv = @values.detect { |v| v == value }
161
- @values.delete fv, &block
162
- end
163
-
164
- ##
165
- # Deletes the value at the specified index, returning that FieldValue, or
166
- # `nil` if the index is out of range.
167
- #
168
- # @param [String] index The index of the value to be removed from the list
169
- # of values.
170
- #
171
- # @return [FieldValue] The deleted `FieldValue` found at the specified
172
- # index, or # `nil` if the index is out of range.
173
- #
174
- # @example
175
- # require "gcloud"
176
- #
177
- # gcloud = Gcloud.new
178
- # search = gcloud.search
179
- # index = search.index "products"
180
- #
181
- # document = index.document "product-sku-000001"
182
- # document["description"].count #=> 2
183
- # document["description"].delete_at 0
184
- # document["description"].count #=> 1
185
- #
186
- def delete_at index
187
- @values.delete_at index
188
- end
189
-
190
- ##
191
- # Calls the given block once for each field value, passing the field value
192
- # as a parameter.
193
- #
194
- # An Enumerator is returned if no block is given.
195
- #
196
- # @example
197
- # require "gcloud"
198
- #
199
- # gcloud = Gcloud.new
200
- # search = gcloud.search
201
- # index = search.index "products"
202
- #
203
- # document = index.document "product-sku-000001"
204
- # puts "The document description is:"
205
- # document["description"].each do |value|
206
- # puts "* #{value} (#{value.type}) [#{value.lang}]"
207
- # end
208
- #
209
- def each &block
210
- @values.each(&block)
211
- end
212
-
213
- ##
214
- # Returns `true` if there are no values.
215
- def empty?
216
- @values.empty?
217
- end
218
-
219
- ##
220
- # @private Create a new FieldValues instance from a name and values Hash.
221
- def self.from_raw name, values
222
- field_values = values.map { |value| FieldValue.from_raw value, name }
223
- FieldValues.new name, field_values
224
- end
225
-
226
- ##
227
- # @private Create a raw Hash object containing all the field values.
228
- def to_raw
229
- { "values" => @values.map(&:to_raw) }
230
- end
231
- end
232
- end
233
- end