Class: Rocksky::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/rocksky/filter.rb

Overview

Fluent builder for RSQL filter expressions, accepted by the filter: parameter of the catalog and scrobble-feed queries (app.rocksky.song.getSongs, app.rocksky.artist.getArtists, app.rocksky.album.getAlbums, app.rocksky.scrobble.getScrobbles).

require "rocksky"

filter = Rocksky::Filter.eq(:artist, "Daft Punk")
                        .and(Rocksky::Filter.gt(:duration, 200_000))
                        .or(Rocksky::Filter.is_in(:genre, %w[house electro]))

Rocksky.catalog_songs(filter: filter)
# artist=="Daft Punk";duration=gt=200000,genre=in=(house,electro)

Fields are Symbols (Strings work too); dotted selectors on the scrobble feed are written as :"track.artist". String values are quoted and escaped automatically when they contain characters RSQL reserves; * wildcards pass through unquoted so Filter.eq(:artist, "Daft*") performs a case-insensitive match.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expr, kind) ⇒ Filter

Returns a new instance of Filter.



109
110
111
112
# File 'lib/rocksky/filter.rb', line 109

def initialize(expr, kind)
  @expr = expr
  @kind = kind
end

Class Method Details

.eq(field, value) ⇒ Object

field==value — equals; * in string values is a wildcard.



33
34
35
# File 'lib/rocksky/filter.rb', line 33

def eq(field, value)
  comparison(field, "==", value)
end

.ge(field, value) ⇒ Object

field=ge=value — greater than or equal.



48
49
50
# File 'lib/rocksky/filter.rb', line 48

def ge(field, value)
  comparison(field, "=ge=", value)
end

.gt(field, value) ⇒ Object

field=gt=value — greater than.



43
44
45
# File 'lib/rocksky/filter.rb', line 43

def gt(field, value)
  comparison(field, "=gt=", value)
end

.is_in(field, values) ⇒ Object Also known as: in

field=in=(a,b) — matches any of the values.



63
64
65
# File 'lib/rocksky/filter.rb', line 63

def is_in(field, values)
  list(field, "=in=", values)
end

.is_not_null(field) ⇒ Object

field!=null — the field is not NULL.



81
82
83
# File 'lib/rocksky/filter.rb', line 81

def is_not_null(field)
  new("#{field}!=null", :comparison)
end

.is_null(field) ⇒ Object

field==null — the field is NULL.



76
77
78
# File 'lib/rocksky/filter.rb', line 76

def is_null(field)
  new("#{field}==null", :comparison)
end

.is_out(field, values) ⇒ Object Also known as: out

field=out=(a,b) — matches none of the values.



68
69
70
# File 'lib/rocksky/filter.rb', line 68

def is_out(field, values)
  list(field, "=out=", values)
end

.le(field, value) ⇒ Object

field=le=value — less than or equal.



58
59
60
# File 'lib/rocksky/filter.rb', line 58

def le(field, value)
  comparison(field, "=le=", value)
end

.lt(field, value) ⇒ Object

field=lt=value — less than.



53
54
55
# File 'lib/rocksky/filter.rb', line 53

def lt(field, value)
  comparison(field, "=lt=", value)
end

.ne(field, value) ⇒ Object

field!=value — not equals.



38
39
40
# File 'lib/rocksky/filter.rb', line 38

def ne(field, value)
  comparison(field, "!=", value)
end

Instance Method Details

#and(other) ⇒ Object

Both sides must match (+;+). An or operand is parenthesized to keep RSQL precedence.



116
117
118
# File 'lib/rocksky/filter.rb', line 116

def and(other)
  Filter.new("#{operand_in_and};#{other.operand_in_and}", :and)
end

#buildObject Also known as: to_s

The RSQL expression string to send as the filter query param.



126
127
128
# File 'lib/rocksky/filter.rb', line 126

def build
  @expr
end

#or(other) ⇒ Object

Either side may match (+,+). Never parenthesizes.



121
122
123
# File 'lib/rocksky/filter.rb', line 121

def or(other)
  Filter.new("#{@expr},#{other.build}", :or)
end