Permalink
Browse files

Add filters, only AND operations at the moment.

  • Loading branch information...
1 parent b69fe01 commit bc79df49f0b313cf036d59199061794610333456 @knaveofdiamonds committed Aug 29, 2012
Showing with 82 additions and 0 deletions.
  1. +1 −0 google_analytics_feeds.gemspec
  2. +68 −0 lib/google_analytics_feeds.rb
  3. +13 −0 spec/request_spec.rb
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
"README.rdoc",
"Rakefile",
"VERSION",
+ "google_analytics_feeds.gemspec",
"lib/google_analytics_feeds.rb",
"lib/sample.rb",
"spec/google_analytics_feeds_spec.rb",
@@ -133,6 +133,67 @@ def handle_complete_property
end
end
+ class FilterBuilder
+ include Naming
+
+ def initialize
+ @filters = []
+ end
+
+ def build(&block)
+ instance_eval(&block)
+ @filters.join(';')
+ end
+
+ # TODO: remove duplication
+
+ def eql(name, value)
+ filter(name, value, '==')
+ end
+
+ def not_eql(name, value)
+ filter(name, value, '!=')
+ end
+
+ def contains(n, v)
+ filter(n, v, '=@')
+ end
+
+ def not_contains(n, v)
+ filter(n, v, '!@')
+ end
+
+ def gt(n, v)
+ filter(n, v, '>')
+ end
+
+ def gte(n, v)
+ filter(n, v, '>=')
+ end
+
+ def lt(n, v)
+ filter(n, v, '<')
+ end
+
+ def lte(n, v)
+ filter(n, v, '<=')
+ end
+
+ def match(n, v)
+ filter(n, v, '=~')
+ end
+
+ def not_match(n, v)
+ filter(n, v, '!~')
+ end
+
+ private
+
+ def filter(name, value, operation)
+ @filters << [symbol_to_name(name), operation, value.to_s].join('')
+ end
+ end
+
class DataFeed
include Naming
@@ -179,6 +240,13 @@ def max_results(i)
}
end
+ def filters(&block)
+ builder =
+ clone_and_set {|params|
+ params['filters'] = FilterBuilder.new.build(&block)
+ }
+ end
+
# Sorts the result set by a column.
#
# Direction can be :asc or :desc.
View
@@ -44,4 +44,17 @@
headers["Authorization"].should == "GoogleLogin auth=123"
end
+
+ it "can add filters" do
+ feed = described_class.new.
+ filters {
+ eql :baz, 4
+ contains :foo, "123"
+ }
+
+ uri = Addressable::URI.parse(feed.uri)
+ uri.query_values.should == {
+ "filters" => "ga:baz==4;ga:foo=@123"
+ }
+ end
end

0 comments on commit bc79df4

Please sign in to comment.