Class: Beatport::Support::QueryBuilder
- Inherits:
-
Object
- Object
- Beatport::Support::QueryBuilder
- 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
-
#camelize_keys(options) ⇒ Object
Camelizes all the keys in the options hash.
-
#map_values(values) ⇒ Object
Map values delimited by ,.
- #process(*args) ⇒ Object
-
#process_facets(values) ⇒ Object
Special processing for facets.
-
#process_return_facets(values) ⇒ Object
Special processing for return_facets.
-
#process_sort_by(values) ⇒ Object
Special processing for sort_by keys.
-
#process_subgenres(value) ⇒ Object
Special processing for subgenres.
- #single_result? ⇒ Boolean
- #special_option?(key) ⇒ Boolean
- #split_value(value, seperator) ⇒ Object
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() Inflector.process_keys() { |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 = args.last.is_a?(Hash) ? args.pop : {} string_key_type = .delete(:string_key_type) || :name collection = .delete(:collection) key = .delete(:key) || (args.length > 1 ? args.compact : args.first) case key when Integer [:id] = key @single_result = true unless collection when String, Symbol [string_key_type] = key.to_s @single_result = true unless collection when Array [:ids] = key.flatten end = camelize_keys() # Handle special options that need to be generated in a specific manner .map do |key, value| if special_option?(key) [key] = send(Inflector.underscore("process_#{key}"), value) elsif value.is_a?(Array) [key] = value.join(',') end end 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
7 8 9 |
# File 'lib/beatport/support/query_builder.rb', line 7 def single_result? @single_result end |
#special_option?(key) ⇒ Boolean
15 16 17 |
# File 'lib/beatport/support/query_builder.rb', line 15 def special_option?(key) SPECIAL_OPTIONS.include?(key) end |