Class: Beatport::Support::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/beatport/support/query_builder.rb

Overview

Converts a set of arguments into a format that beatport will understand

Constant Summary collapse

SPECIAL_OPTIONS =
['sortBy', 'facets', 'returnFacets', 'subgenres']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.process(*args) ⇒ Object



11
12
13
# File 'lib/beatport/support/query_builder.rb', line 11

def self.process(*args)
  new.process(*args)
end

Instance Method Details

#camelize_keys(options) ⇒ Object

Camelizes all the keys in the options hash



53
54
55
# File 'lib/beatport/support/query_builder.rb', line 53

def camelize_keys(options)
  Inflector.process_keys(options) { |k| Inflector.camelize(k.to_s, false) }
end

#map_values(values) ⇒ Object

Map values delimited by ,



85
86
87
88
89
90
# File 'lib/beatport/support/query_builder.rb', line 85

def map_values(values)
  values = values.split(/,\s*/) if values.is_a?(String)
  values.map do |value|
    yield value
  end.join(",") 
end

#process(*args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/beatport/support/query_builder.rb', line 19

def process(*args)
  @single_result = false
  
  options = args.last.is_a?(Hash) ? args.pop : {}      
  string_key_type = options.delete(:string_key_type) || :name
  collection = options.delete(:collection)
  key = options.delete(:key) || (args.length > 1 ? args.compact : args.first)
    
  case key
  when Integer
    options[:id] = key
    @single_result = true unless collection
  when String, Symbol
    options[string_key_type] = key.to_s
    @single_result = true unless collection
  when Array
    options[:ids] = key.flatten
  end

  options = camelize_keys(options)

  # Handle special options that need to be generated in a specific manner
  options.map do |key, value|
    if special_option?(key)
      options[key] = send(Inflector.underscore("process_#{key}"), value) 
    elsif value.is_a?(Array)
      options[key] = value.join(',')
    end
  end

  options
end

#process_facets(values) ⇒ Object

Special processing for facets



65
66
67
68
69
70
# File 'lib/beatport/support/query_builder.rb', line 65

def process_facets(values)
  map_values(values) do |value|
    k, v = split_value(value, ':')
    Array(v).map {|v| "#{k}:#{v}"}.join(',')
  end       
end

#process_return_facets(values) ⇒ Object

Special processing for return_facets



73
74
75
76
77
# File 'lib/beatport/support/query_builder.rb', line 73

def process_return_facets(values)
  map_values(values) do |value|
    Inflector.camelize(value, false)
  end
end

#process_sort_by(values) ⇒ Object

Special processing for sort_by keys



58
59
60
61
62
# File 'lib/beatport/support/query_builder.rb', line 58

def process_sort_by(values)
  map_values(values) do |value|
    split_value(value, " ").join(" ")
  end       
end

#process_subgenres(value) ⇒ Object

Special processing for subgenres



80
81
82
# File 'lib/beatport/support/query_builder.rb', line 80

def process_subgenres(value)
  value ? 'true' : 'false'
end

#single_result?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/beatport/support/query_builder.rb', line 7

def single_result?
  @single_result
end

#special_option?(key) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/beatport/support/query_builder.rb', line 15

def special_option?(key)
  SPECIAL_OPTIONS.include?(key)
end

#split_value(value, seperator) ⇒ Object



92
93
94
95
96
# File 'lib/beatport/support/query_builder.rb', line 92

def split_value(value, seperator)
  value = value.split(seperator) if value.is_a?(String)
  
  [Inflector.camelize(value.first, false), value.last]
end