lib/adknowledge/performance.rb in adknowledge-0.0.3 vs lib/adknowledge/performance.rb in adknowledge-0.0.4

- old
+ new

@@ -1,8 +1,8 @@ require 'faraday' require 'faraday_middleware' -require 'faraday_middleware/parse_oj' +require 'faraday_middleware/multi_json' require 'active_support/core_ext/module/delegation' module Adknowledge class Performance include Enumerable @@ -64,28 +64,20 @@ # Specify the measure(s) to select in the query # # @param [Array] selection(s) # @return [Adknowledge::Performance] query object def select *selection - selection = selection.map{|x| x.to_sym} # handle strings & symbols equally - unless (selection - VALID_MEASURES).empty? - raise ArgumentError, 'Invalid measurement selection' - end - @measures.merge! Hash[selection.zip([1] * selection.count)] + @measures.merge! paramerize(selection, VALID_MEASURES, 'Invalid measurement selection') self end # Specify the dimension(s) to group measures by # # @param [Array] grouping(s) # @return [Adknowledge::Performance] query object def group_by *groupings - groupings = groupings.map{|x| x.to_sym} # handle strings & symbols equally - unless (groupings - VALID_DIMENSIONS).empty? - raise ArgumentError, 'Invalid dimension grouping' - end - @dimensions.merge! Hash[groupings.zip([1] * groupings.count)] + @dimensions.merge! paramerize(groupings, VALID_DIMENSIONS, 'Invalid dimension group') self end # Specify the filter criteria to limit query by # @@ -127,38 +119,29 @@ # Specify whether to display the full set even if entries are 0 # # @param [Boolean] full # @return [Adknowledge::Performance] query object def full full - unless !!full == full #Boolean check - raise ArgumentError, 'Full option must be a boolean' - end - @options[:full] = full ? '1' : '0' + @options[:full] = booleanize 'Full', full self end # Disable caching of queries. By default queries are cached for 60 seconds # # @param [Boolean] nocache # @return [Adknowledge::Performance] query object def nocache nocache - unless !!nocache == nocache #Boolean check - raise ArgumentError, 'NoCache option must be a boolean' - end - @options[:nocache] = nocache ? '1' : '0' + @options[:nocache] = booleanize 'NoCache', nocache self end # Force query to show filtered dimensions to be shown # # @param [Boolean] display_all # @return [Adknowledge::Performance] query object def display_all display_all - unless !!display_all == display_all #Boolean check - raise ArgumentError, "display_all option must be a boolean" - end - @options[:all] = display_all ? '1' : '0' + @options[:all] = booleanize 'DisplayAll', display_all self end # Specify pivot options # @@ -221,12 +204,27 @@ end end def conn @conn ||= Faraday.new(:url => URL) do |b| - b.response :oj + b.response :multi_json b.adapter Faraday.default_adapter end + end + + def paramerize array, valid, error_str + array = array.map{|x| x.to_sym} # handle strings & symbols equally + unless (array - valid).empty? + raise ArgumentError, error_str + end + Hash[array.zip([1] * array.count)] + end + + def booleanize name, value + unless !!value == value #Boolean check + raise ArgumentError, "#{name} option must be a boolean" + end + value ? '1' : '0' end def valid_pivot_values dimensions.keys + ['*'] end