Class: Glyr::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/glyr/query.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pointer, options = {}) ⇒ Query

Returns a new instance of Query.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/glyr/query.rb', line 35

def initialize (pointer, options = {})
	@internal = pointer.is_a?(FFI::Pointer) ? C::Query.new(pointer) : pointer

	options.each {|name, value|
		if value == true
			__send__ "#{name}!"
		elsif value == false
			__send__ "no_#{name}!"
		else
			__send__ name, *value
		end
	}
end

Class Method Details

.create(options = {}) ⇒ Object

create a Query object with the passed options



15
16
17
18
19
20
21
# File 'lib/glyr/query.rb', line 15

def self.create (options = {})
	struct = C::Query.new

	C.glyr_query_init(struct.pointer)

	wrap(struct.pointer, options)
end

.finalizer(pointer) ⇒ Object

:nodoc:



29
30
31
32
33
# File 'lib/glyr/query.rb', line 29

def self.finalizer (pointer) # :nodoc:
	proc {
		C.glyr_query_destroy(pointer)
	}
end

.wrap(pointer, options = {}) ⇒ Object



23
24
25
26
27
# File 'lib/glyr/query.rb', line 23

def self.wrap (pointer, options = {})
	new(pointer, options).tap {|x|
		ObjectSpace.define_finalizer x, finalizer(pointer)
	}
end

Instance Method Details

#album(value = nil) ⇒ Object

set the album



68
69
70
71
72
73
74
# File 'lib/glyr/query.rb', line 68

def album (value = nil)
	if value
		raise_if_error C.glyr_opt_album(to_native, value)
	else
		to_native[:album]
	end
end

#allowed_formats(*formats) ⇒ Object

use only the passed formats



192
193
194
195
196
197
198
# File 'lib/glyr/query.rb', line 192

def allowed_formats (*formats)
	unless formats.empty?
		raise_if_error C.glyr_opt_allowed_formats(to_native, formats.flatten.compact.uniq.join(';'))
	else
		to_native[:allowed_formats] && to_native[:allowed_formats].split(/\s*;\s*/)
	end
end

#artist(value = nil) ⇒ Object

set the artist



59
60
61
62
63
64
65
# File 'lib/glyr/query.rb', line 59

def artist (value = nil)
	if value
		raise_if_error C.glyr_opt_artist(to_native, value)
	else
		to_native[:artist]
	end
end

#cache(value = nil) ⇒ Object



200
201
202
203
204
205
206
# File 'lib/glyr/query.rb', line 200

def cache (value = nil)
	if value
		@cache = Database.create(value)
	else
		@cache
	end
end

#download!Object

download the data instead of just passing a URL



213
214
215
# File 'lib/glyr/query.rb', line 213

def download!
	raise_if_error C.glyr_opt_download(to_native, true)
end

#download?Boolean

Returns:

  • (Boolean)


208
209
210
# File 'lib/glyr/query.rb', line 208

def download?
	to_native[:download]
end

#force_utf8!Object



267
268
269
# File 'lib/glyr/query.rb', line 267

def force_utf8!
	raise_if_error C.glyr_opt_force_utf8(to_native, true)
end

#force_utf8?Boolean

Returns:

  • (Boolean)


263
264
265
# File 'lib/glyr/query.rb', line 263

def force_utf8?
	to_native[:force_utf8]
end

#from(*providers) ⇒ Object

use only the passed providers (you can use all and disable single providers by prefixing them with a -)



183
184
185
186
187
188
189
# File 'lib/glyr/query.rb', line 183

def from (*providers)
	unless providers.empty?
		raise_if_error C.glyr_opt_from(to_native, providers.flatten.compact.uniq.join(';'))
	else
		to_native[:from] && to_native[:from].split(/\s*;\s*/)
	end
end

#fuzzyness(value = nil) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/glyr/query.rb', line 222

def fuzzyness (value = nil)
	if value
		raise_if_error C.glyr_opt_fuzzyness(to_native, value)
	else
		to_native[:fuzzyness]
	end
end

#get(&block) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/glyr/query.rb', line 275

def get (&block)
	error  = FFI::MemoryPointer.new :int
	length = FFI::MemoryPointer.new :int

	if block
		raise_if_error C.glyr_opt_dlcallback(to_native, proc {|data, query|
			result = block.call(Result.copy(C::MemCache.new(data)))

			if result.respond_to? :to_i
				C::Error[result.to_i]
			elsif result.respond_to? :to_sym
				result.to_sym.downcase
			else
				:ok
			end
		}, nil)
	else
		raise_if_error C.glyr_opt_dlcallback(to_native, nil, nil)
	end

	if db = cache || Glyr.cache_at
		raise_if_error C.glyr_opt_lookup_db(to_native, db.to_native)
		raise_if_error C.glyr_opt_db_autoread(to_native, true) if db.read?
		raise_if_error C.glyr_opt_db_autowrite(to_native, true) if db.write?
	end

	result = C.glyr_get(to_native, error, length)

	raise_if_error error.typecast(:int)

	Results.wrap(result, length.typecast(:int))
end

#image_boundaries(min = nil, max = min) ⇒ Object

set the image size boundaries



86
87
88
89
90
91
92
93
# File 'lib/glyr/query.rb', line 86

def image_boundaries (min = nil, max = min)
	if min && max
		raise_if_error C.glyr_opt_img_minsize(to_native, min)
		raise_if_error C.glyr_opt_img_maxsize(to_native, max)
	else
		[to_native[:img_min_size], to_native[:img_max_size]]
	end
end

#language(value = nil) ⇒ Object

set or get the language to search for



132
133
134
135
136
137
138
# File 'lib/glyr/query.rb', line 132

def language (value = nil)
	if value
		raise_if_error C.glyr_opt_lang(to_native, value)
	else
		to_native[:lang]
	end
end

#language_aware!Object

only fetch from language aware providers



145
146
147
# File 'lib/glyr/query.rb', line 145

def language_aware!
	raise_if_error C.glyr_opt_lang_aware_only(to_native, true)
end

#language_aware?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/glyr/query.rb', line 140

def language_aware?
	to_native[:lang_aware_only]
end

#maximum(value = nil) ⇒ Object

set the maximum number of results



155
156
157
158
159
160
161
# File 'lib/glyr/query.rb', line 155

def maximum (value = nil)
	if value
		raise_if_error C.glyr_opt_number(to_native, value)
	else
		to_native[:number]
	end
end

#maximum_per_provider(value = nil) ⇒ Object

set the maximum number of results per provider



164
165
166
167
168
169
170
# File 'lib/glyr/query.rb', line 164

def maximum_per_provider (value = nil)
	if value
		raise_if_error C.glyr_opt_plugmax(to_native, value)
	else
		to_native[:plugmax]
	end
end

#no_download!Object

don’t download the data, just pass the URL



218
219
220
# File 'lib/glyr/query.rb', line 218

def no_download!
	raise_if_error C.glyr_opt_download(to_native, false)
end

#no_force_utf8!Object



271
272
273
# File 'lib/glyr/query.rb', line 271

def no_force_utf8!
	raise_if_error C.glyr_opt_force_utf8(to_native, false)
end

#no_language_aware!Object

don’t force language aware providers



150
151
152
# File 'lib/glyr/query.rb', line 150

def no_language_aware!
	raise_if_error C.glyr_opt_lang_aware_only(to_native, false)
end

#normalize(*args) ⇒ Object



255
256
257
258
259
260
261
# File 'lib/glyr/query.rb', line 255

def normalize (*args)
	unless args.empty?
		raise_if_error C.glyr_opt_normalize(to_native, Normalization[*args].to_i)
	else
		Normalization[to_native[:normalization]]
	end
end

#parallel(value = nil) ⇒ Object

set the number of parallel searches



96
97
98
99
100
101
102
# File 'lib/glyr/query.rb', line 96

def parallel (value = nil)
	if value
		raise_if_error C.glyr_opt_parallel(to_native, value)
	else
		to_native[:parallel]
	end
end

#path(value = nil) ⇒ Object



247
248
249
250
251
252
253
# File 'lib/glyr/query.rb', line 247

def path (value = nil)
	if value
		raise_if_error C.glyr_opt_musictree_path(to_native, value)
	else
		to_native[:musictree_path]
	end
end

#proxy(value = nil) ⇒ Object

use the passed proxy to fetch the stuff



239
240
241
242
243
244
245
# File 'lib/glyr/query.rb', line 239

def proxy (value = nil)
	if value
		raise_if_error C.glyr_opt_proxy(to_native, value)
	else
		to_native[:proxy]
	end
end

#ratio(value = nil) ⇒ Object



230
231
232
233
234
235
236
# File 'lib/glyr/query.rb', line 230

def ratio (value = nil)
	if value
		raise_if_error C.glyr_opt_qsratio(to_native, value)
	else
		to_native[:qsratio]
	end
end

#redirects(value = nil) ⇒ Object

set the max amount of redirects



114
115
116
117
118
119
120
# File 'lib/glyr/query.rb', line 114

def redirects (value = nil)
	if value
		raise_if_error C.glyr_opt_redirects(to_native, value)
	else
		to_native[:redirects]
	end
end

#stopObject

stop the current fetching



317
318
319
# File 'lib/glyr/query.rb', line 317

def stop
	C.glyr_signal_exit(to_native)
end

#timeout(value = nil) ⇒ Object

set the timeout for the searching



105
106
107
108
109
110
111
# File 'lib/glyr/query.rb', line 105

def timeout (value = nil)
	if value
		raise_if_error C.glyr_opt_timeout(to_native, value)
	else
		to_native[:timeout]
	end
end

#title(value = nil) ⇒ Object

set the title



77
78
79
80
81
82
83
# File 'lib/glyr/query.rb', line 77

def title (value = nil)
	if value
		raise_if_error C.glyr_opt_title(to_native, value)
	else
		to_native[:title]
	end
end

#to_nativeObject

get the FFI::Struct



322
323
324
# File 'lib/glyr/query.rb', line 322

def to_native
	@internal
end

#type(value = nil) ⇒ Object

tell the Query what you want to get



50
51
52
53
54
55
56
# File 'lib/glyr/query.rb', line 50

def type (value = nil)
	if value
		raise_if_error C.glyr_opt_type(to_native, value.is_a?(Symbol) ? value : C::GetType[value])
	else
		to_native[:type]
	end
end

#user_agent(value = nil) ⇒ Object

set the user agent to use



123
124
125
126
127
128
129
# File 'lib/glyr/query.rb', line 123

def user_agent (value = nil)
	if value
		raise_if_error C.glyr_opt_useragent(to_native, value)
	else
		to_native[:useragent]
	end
end

#verbosity(value = nil) ⇒ Object

set the level of verbosity



173
174
175
176
177
178
179
# File 'lib/glyr/query.rb', line 173

def verbosity (value = nil)
	if value
		raise_if_error C.glyr_opt_verbosity(to_native, value)
	else
		to_native[:verbosity]
	end
end