garb 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,45 @@
1
+ module Garb
2
+ class FilterParameters
3
+ def self.define_operator(key, operation)
4
+ class_eval <<-CODE
5
+ def #{key}(target, value)
6
+ self.parameters << {Operator.new(target, '#{operation}') => value}
7
+ end
8
+ CODE
9
+ end
10
+
11
+ define_operator(:eql, '==')
12
+ define_operator(:not_eql, '!=')
13
+ define_operator(:gt, '>')
14
+ define_operator(:gte, '>=')
15
+ define_operator(:lt, '<')
16
+ define_operator(:lte, '<=')
17
+ define_operator(:matches, '==')
18
+ define_operator(:does_not_match, '!=')
19
+ define_operator(:contains, '=~')
20
+ define_operator(:does_not_contain, '!~')
21
+ define_operator(:substring, '=@')
22
+ define_operator(:not_substring, '!@')
23
+
24
+ attr_accessor :parameters
25
+
26
+ def initialize
27
+ self.parameters = []
28
+ end
29
+
30
+ def filters(&block)
31
+ instance_eval &block
32
+ end
33
+
34
+ def to_params
35
+ value = self.parameters.map do |param|
36
+ param.map do |k,v|
37
+ next unless k.is_a?(Garb::Operator)
38
+ "#{k.target}#{URI.encode(k.operator.to_s, /[=<>]/)}#{CGI::escape(v.to_s)}"
39
+ end.join(',') # Hash AND
40
+ end.join(';') # Array OR
41
+
42
+ value.empty? ? {} : {'filters' => value}
43
+ end
44
+ end
45
+ end
data/lib/garb/operator.rb CHANGED
@@ -4,11 +4,11 @@ module Garb
4
4
  attr_reader :target, :operator, :prefix
5
5
 
6
6
  def initialize(target, operator, prefix=false)
7
- @target = target.to_google_analytics
7
+ @target = Garb.to_google_analytics(target)
8
8
  @operator = operator
9
9
  @prefix = prefix
10
10
  end
11
-
11
+
12
12
  def to_google_analytics
13
13
  @prefix ? "#{operator}#{target}" : "#{target}#{operator}"
14
14
  end
data/lib/garb/profile.rb CHANGED
@@ -13,7 +13,7 @@ module Garb
13
13
  attribute :value, String
14
14
 
15
15
  def instance_name
16
- name.from_google_analytics.underscore
16
+ Garb.from_google_analytics(name)
17
17
  end
18
18
  end
19
19
 
@@ -38,7 +38,7 @@ module Garb
38
38
  end
39
39
 
40
40
  def id
41
- @table_id.from_google_analytics
41
+ Garb.from_google_analytics(@table_id)
42
42
  end
43
43
 
44
44
  def self.all
data/lib/garb/report.rb CHANGED
@@ -15,7 +15,6 @@ module Garb
15
15
 
16
16
  metrics opts.fetch(:metrics, [])
17
17
  dimensions opts.fetch(:dimensions, [])
18
- filters opts.fetch(:filters, [])
19
18
  sort opts.fetch(:sort, [])
20
19
  end
21
20
 
@@ -18,19 +18,8 @@ module Garb
18
18
  end
19
19
 
20
20
  def to_params
21
- params = self.elements.map do |elem|
22
- case elem
23
- when Hash
24
- elem.collect do |k,v|
25
- next unless k.is_a?(Operator)
26
- "#{k.target}#{URI.encode(k.operator.to_s, /[=<>]/)}#{CGI::escape(v.to_s)}"
27
- end.join(';')
28
- else
29
- elem.to_google_analytics
30
- end
31
- end.join(',')
32
-
33
- params.empty? ? {} : {self.name => params}
21
+ value = self.elements.map{|param| Garb.to_google_analytics(param)}.join(',')
22
+ value.empty? ? {} : {self.name => value}
34
23
  end
35
24
  end
36
25
  end
data/lib/garb/resource.rb CHANGED
@@ -3,7 +3,7 @@ module Garb
3
3
  MONTH = 2592000
4
4
  URL = "https://www.google.com/analytics/feeds/data"
5
5
 
6
- %w(metrics dimensions filters sort).each do |parameter|
6
+ %w(metrics dimensions sort).each do |parameter|
7
7
  class_eval <<-CODE
8
8
  def #{parameter}(*fields)
9
9
  @#{parameter} ||= ReportParameter.new(:#{parameter})
@@ -16,33 +16,15 @@ module Garb
16
16
  CODE
17
17
  end
18
18
 
19
- # def metrics(*fields)
20
- # @metrics ||= ReportParameter.new(:metrics)
21
- # @metrics << fields
22
- # end
23
- #
24
- # def dimensions(*fields)
25
- # @dimensions ||= ReportParameter.new(:dimensions)
26
- # @dimensions << fields
27
- # end
28
- #
29
- # def filters(*hashes)
30
- # @filters ||= ReportParameter.new(:filters)
31
- # @filters << hashes
32
- # end
33
- #
34
- # def sort(*fields)
35
- # @sort ||= ReportParameter.new(:sort)
36
- # @sort << fields
37
- # end
38
- #
39
- # def clear_filters
40
- # @filters = ReportParameter.new(:filters)
41
- # end
42
- #
43
- # def clear_sort
44
- # @sort = ReportParameter.new(:sort)
45
- # end
19
+ def filters(&block)
20
+ @filter_parameters ||= FilterParameters.new
21
+ @filter_parameters.filters(&block) if block_given?
22
+ @filter_parameters
23
+ end
24
+
25
+ def clear_filters
26
+ @filter_parameters = FilterParameters.new
27
+ end
46
28
 
47
29
  def results(profile, opts = {}, &block)
48
30
  @profile = profile.is_a?(Profile) ? profile : Profile.first(profile)
data/lib/garb/version.rb CHANGED
@@ -2,8 +2,8 @@ module Garb
2
2
  module Version
3
3
 
4
4
  MAJOR = 0
5
- MINOR = 4
6
- TINY = 2
5
+ MINOR = 5
6
+ TINY = 0
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
data/lib/garb.rb CHANGED
@@ -15,15 +15,21 @@ require 'garb/data_request'
15
15
  require 'garb/session'
16
16
  require 'garb/profile'
17
17
  require 'garb/account'
18
+ require 'garb/filter_parameters'
18
19
  require 'garb/report_parameter'
19
20
  require 'garb/report_response'
20
21
  require 'garb/resource'
21
22
  require 'garb/report'
22
23
  require 'garb/operator'
23
24
 
24
- require 'extensions/string'
25
- require 'extensions/symbol'
26
-
27
25
  module Garb
28
26
  GA = "http://schemas.google.com/analytics/2008"
27
+
28
+ def self.to_google_analytics(thing)
29
+ "ga:#{thing.to_s.camelize(:lower)}"
30
+ end
31
+
32
+ def self.from_google_analytics(thing)
33
+ thing.to_s.gsub(/^ga\:/, '').underscore
34
+ end
29
35
  end
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '/test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
2
 
3
3
  module Garb
4
4
  class AccountTest < MiniTest::Unit::TestCase
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '/test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
2
 
3
3
  CA_CERT_FILE = File.join(File.dirname(__FILE__), '..', '/cacert.pem')
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '/test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
2
 
3
3
  module Garb
4
4
  class DataRequestTest < MiniTest::Unit::TestCase
@@ -0,0 +1,65 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
+
3
+ module Garb
4
+ class FilterParametersTest < MiniTest::Unit::TestCase
5
+ def self.should_define_operator(operator, param)
6
+ should "create an operator and add to parameters for the #{operator} method" do
7
+ new_operator = stub
8
+ symbol = :foo
9
+
10
+ Garb::Operator.expects(:new).with(:foo, param).returns(new_operator)
11
+ @filter_parameters.send(operator.to_sym, :foo, 100)
12
+
13
+ parameter = {new_operator => 100}
14
+ assert_equal parameter, @filter_parameters.parameters.last
15
+ end
16
+ end
17
+
18
+ context "A FilterParameters" do
19
+ setup do
20
+ @filter_parameters = FilterParameters.new
21
+ end
22
+
23
+ should_define_operator(:eql, '==')
24
+ should_define_operator(:not_eql, '!=')
25
+ should_define_operator(:gt, '>')
26
+ should_define_operator(:gte, '>=')
27
+ should_define_operator(:lt, '<')
28
+ should_define_operator(:lte, '<=')
29
+ should_define_operator(:matches, '==')
30
+ should_define_operator(:does_not_match, '!=')
31
+ should_define_operator(:contains, '=~')
32
+ should_define_operator(:does_not_contain, '!~')
33
+ should_define_operator(:substring, '=@')
34
+ should_define_operator(:not_substring, '!@')
35
+
36
+ should "instance eval for filters" do
37
+ blk = lambda {"in a block"}
38
+
39
+ @filter_parameters.expects(:instance_eval)
40
+ @filter_parameters.filters(&blk)
41
+ end
42
+
43
+ context "when converting parameters hash into query string parameters" do
44
+ should "parameterize hash operators and join elements" do
45
+ @filter_parameters.filters do
46
+ eql(:city, 'New York')
47
+ eql(:state, 'New York')
48
+ end
49
+
50
+ params = {'filters' => 'ga:city%3D%3DNew+York;ga:state%3D%3DNew+York'}
51
+ assert_equal params, @filter_parameters.to_params
52
+ end
53
+
54
+ should "properly encode operators" do
55
+ @filter_parameters.filters do
56
+ contains(:request_uri, 'New York')
57
+ end
58
+
59
+ params = {'filters' => 'ga:requestUri%3D~New+York'}
60
+ assert_equal params, @filter_parameters.to_params
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '/test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
2
 
3
3
  module Garb
4
4
  class OAuthSessionTest < MiniTest::Unit::TestCase
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '/test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
2
 
3
3
  module Garb
4
4
  class OperatorTest < MiniTest::Unit::TestCase
@@ -6,21 +6,21 @@ module Garb
6
6
  should "lower camelize the target" do
7
7
  assert_equal "ga:uniqueVisits=", Operator.new(:unique_visits, "=").to_google_analytics
8
8
  end
9
-
9
+
10
10
  should "return target and operator together" do
11
11
  assert_equal "ga:metric=", Operator.new(:metric, "=").to_google_analytics
12
12
  end
13
-
13
+
14
14
  should "prefix the operator to the target" do
15
15
  assert_equal "-ga:metric", Operator.new(:metric, "-", true).to_google_analytics
16
16
  end
17
-
17
+
18
18
  should "know if it is equal to another operator" do
19
19
  op1 = Operator.new(:hello, "==")
20
20
  op2 = Operator.new(:hello, "==")
21
21
  assert_equal op1, op2
22
22
  end
23
-
23
+
24
24
  should "not be equal to another operator if target, operator, or prefix is different" do
25
25
  op1 = Operator.new(:hello, "==")
26
26
  op2 = Operator.new(:hello, "==", true)
@@ -36,4 +36,4 @@ module Garb
36
36
  end
37
37
  end
38
38
  end
39
- end
39
+ end
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '/test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
2
 
3
3
  module Garb
4
4
  class ProfileTest < MiniTest::Unit::TestCase
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '/test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
2
 
3
3
  module Garb
4
4
  class ReportParameterTest < MiniTest::Unit::TestCase
@@ -36,25 +36,6 @@ module Garb
36
36
  @metrics << :request_uri << :city
37
37
  assert_equal({'metrics' => 'ga:requestUri,ga:city'}, @metrics.to_params)
38
38
  end
39
-
40
- should "join operator elements" do
41
- @metrics << :city.desc
42
- assert_equal({'metrics' => '-ga:city'}, @metrics.to_params)
43
- end
44
-
45
- should "parameterize hash operators and join elements" do
46
- @metrics << {:city.eql => 'New York'}
47
- params = {'metrics' => 'ga:city%3D%3DNew+York'}
48
-
49
- assert_equal params, @metrics.to_params
50
- end
51
-
52
- should "properly encode operators" do
53
- @metrics << {:request_uri.contains => 'New York'}
54
- params = {'metrics' => 'ga:requestUri%3D~New+York'}
55
-
56
- assert_equal params, @metrics.to_params
57
- end
58
39
  end
59
40
  end
60
41
 
@@ -1,10 +1,10 @@
1
- require File.join(File.dirname(__FILE__), '..', '/test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
2
 
3
3
  module Garb
4
4
  class ReportResponseTest < MiniTest::Unit::TestCase
5
5
  context "An instance of the ReportResponse class" do
6
6
  setup do
7
- @xml = File.read(File.join(File.dirname(__FILE__), '..', "/fixtures/report_feed.xml"))
7
+ @xml = File.read(File.join(File.dirname(__FILE__), '..', '..', "/fixtures/report_feed.xml"))
8
8
  @response = ReportResponse.new(@xml)
9
9
  end
10
10
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '/test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
2
 
3
3
  module Garb
4
4
  # Also tests Garb::Resource, which is the basis for Garb::Report
@@ -11,7 +11,7 @@ module Garb
11
11
  @report = Report.new(@profile)
12
12
  end
13
13
 
14
- %w(metrics dimensions filters sort).each do |param|
14
+ %w(metrics dimensions sort).each do |param|
15
15
  should "have parameters for #{param}" do
16
16
  assert @report.send(:"#{param}").is_a?(ReportParameter)
17
17
  end
@@ -21,6 +21,14 @@ module Garb
21
21
  end
22
22
  end
23
23
 
24
+ should "have filter parameters for filters" do
25
+ assert @report.filters.is_a?(FilterParameters)
26
+ end
27
+
28
+ should "clear filter parameters for filters" do
29
+ assert_equal({}, @report.filters.to_params)
30
+ end
31
+
24
32
  should "have default parameters" do
25
33
  @report.stubs(:format_time).returns('2009-08-01')
26
34
  params = {'ids' => 'ga:1234', 'start-date' => '2009-08-01', 'end-date' => '2009-08-01'}
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '/test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
2
 
3
3
  class TestReport
4
4
  extend Garb::Resource
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '/test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
2
 
3
3
  module Garb
4
4
  class SessionTest < MiniTest::Unit::TestCase
@@ -1,9 +1,13 @@
1
1
  require File.join(File.dirname(__FILE__), '..', '/test_helper')
2
2
 
3
3
  class GarbTest < MiniTest::Unit::TestCase
4
- context "A green egg" do
5
- should "be served with ham" do
6
- assert true
4
+ context "The Garb module" do
5
+ should 'prefix a string with ga: for GA' do
6
+ assert_equal 'ga:bob', Garb.to_google_analytics('bob')
7
7
  end
8
- end
9
- end
8
+
9
+ should 'remove ga: prefix' do
10
+ assert_equal 'bob', Garb.from_google_analytics('ga:bob')
11
+ end
12
+ end
13
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: garb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Pitale
@@ -44,11 +44,10 @@ extra_rdoc_files: []
44
44
  files:
45
45
  - README.md
46
46
  - Rakefile
47
- - lib/extensions/string.rb
48
- - lib/extensions/symbol.rb
49
47
  - lib/garb/account.rb
50
48
  - lib/garb/authentication_request.rb
51
49
  - lib/garb/data_request.rb
50
+ - lib/garb/filter_parameters.rb
52
51
  - lib/garb/oauth_session.rb
53
52
  - lib/garb/operator.rb
54
53
  - lib/garb/profile.rb
@@ -64,20 +63,19 @@ files:
64
63
  - test/fixtures/profile_feed.xml
65
64
  - test/fixtures/report_feed.xml
66
65
  - test/test_helper.rb
67
- - test/unit/account_test.rb
68
- - test/unit/authentication_request_test.rb
69
- - test/unit/data_request_test.rb
66
+ - test/unit/garb/account_test.rb
67
+ - test/unit/garb/authentication_request_test.rb
68
+ - test/unit/garb/data_request_test.rb
69
+ - test/unit/garb/filter_parameters_test.rb
70
+ - test/unit/garb/oauth_session_test.rb
71
+ - test/unit/garb/operator_test.rb
72
+ - test/unit/garb/profile_test.rb
73
+ - test/unit/garb/report_parameter_test.rb
74
+ - test/unit/garb/report_response_test.rb
75
+ - test/unit/garb/report_test.rb
76
+ - test/unit/garb/resource_test.rb
77
+ - test/unit/garb/session_test.rb
70
78
  - test/unit/garb_test.rb
71
- - test/unit/oauth_session_test.rb
72
- - test/unit/operator_test.rb
73
- - test/unit/profile_test.rb
74
- - test/unit/report_parameter_test.rb
75
- - test/unit/report_response_test.rb
76
- - test/unit/report_test.rb
77
- - test/unit/resource_test.rb
78
- - test/unit/session_test.rb
79
- - test/unit/string_test.rb
80
- - test/unit/symbol_test.rb
81
79
  has_rdoc: true
82
80
  homepage: http://github.com/vigetlabs/garb
83
81
  licenses: []
@@ -111,17 +109,16 @@ test_files:
111
109
  - test/fixtures/profile_feed.xml
112
110
  - test/fixtures/report_feed.xml
113
111
  - test/test_helper.rb
114
- - test/unit/account_test.rb
115
- - test/unit/authentication_request_test.rb
116
- - test/unit/data_request_test.rb
112
+ - test/unit/garb/account_test.rb
113
+ - test/unit/garb/authentication_request_test.rb
114
+ - test/unit/garb/data_request_test.rb
115
+ - test/unit/garb/filter_parameters_test.rb
116
+ - test/unit/garb/oauth_session_test.rb
117
+ - test/unit/garb/operator_test.rb
118
+ - test/unit/garb/profile_test.rb
119
+ - test/unit/garb/report_parameter_test.rb
120
+ - test/unit/garb/report_response_test.rb
121
+ - test/unit/garb/report_test.rb
122
+ - test/unit/garb/resource_test.rb
123
+ - test/unit/garb/session_test.rb
117
124
  - test/unit/garb_test.rb
118
- - test/unit/oauth_session_test.rb
119
- - test/unit/operator_test.rb
120
- - test/unit/profile_test.rb
121
- - test/unit/report_parameter_test.rb
122
- - test/unit/report_response_test.rb
123
- - test/unit/report_test.rb
124
- - test/unit/resource_test.rb
125
- - test/unit/session_test.rb
126
- - test/unit/string_test.rb
127
- - test/unit/symbol_test.rb
@@ -1,9 +0,0 @@
1
- class String
2
- def to_google_analytics
3
- "ga:#{self}"
4
- end
5
-
6
- def from_google_analytics
7
- self.gsub(/^ga\:/, '')
8
- end
9
- end
@@ -1,36 +0,0 @@
1
- class Symbol
2
- # OPERATORS
3
-
4
- def self.operator(operators)
5
- operators.each do |method, operator|
6
- class_eval <<-CODE
7
- def #{method}
8
- Garb::Operator.new(self, '#{operator}')
9
- end
10
- CODE
11
- end
12
- end
13
-
14
- # Sorting
15
- def desc
16
- Garb::Operator.new(self, '-', true)
17
- end
18
-
19
- operator :eql => '==',
20
- :not_eql => '!=',
21
- :gt => '>',
22
- :gte => '>=',
23
- :lt => '<',
24
- :lte => '<=',
25
- :matches => '==',
26
- :does_not_match => '!=',
27
- :contains => '=~',
28
- :does_not_contain => '!~',
29
- :substring => '=@',
30
- :not_substring => '!@'
31
-
32
- # Metric filters
33
- def to_google_analytics
34
- self.to_s.camelize(:lower).to_google_analytics
35
- end
36
- end
@@ -1,13 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', '/test_helper')
2
-
3
- class StringTest < MiniTest::Unit::TestCase
4
- context "An instance of a String" do
5
- should 'prefix a string with ga: for GA' do
6
- assert_equal 'ga:bob', 'bob'.to_google_analytics
7
- end
8
-
9
- should 'remove ga: prefix' do
10
- assert_equal 'bob', 'ga:bob'.from_google_analytics
11
- end
12
- end
13
- end
@@ -1,44 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', '/test_helper')
2
-
3
- class SymbolTest < MiniTest::Unit::TestCase
4
-
5
- context "An instance of the Symbol class" do
6
-
7
- should "properly format itself for ga" do
8
- assert_equal "ga:requestUri", :request_uri.to_google_analytics
9
- end
10
-
11
- should "define a :desc operator" do
12
- operator = stub()
13
- symbol = :foo
14
-
15
- Garb::Operator.expects(:new).with(:foo, '-', true).returns(operator)
16
- assert_equal operator, :foo.desc
17
- end
18
-
19
- def self.should_define_operator(operators)
20
- operators.each do |method, operator|
21
- should "define an :#{method} operator" do
22
- new_operator = stub()
23
- symbol = :foo
24
-
25
- Garb::Operator.expects(:new).with(:foo, operator).returns(new_operator)
26
- assert_equal new_operator, :foo.send(method)
27
- end
28
- end
29
- end
30
-
31
- should_define_operator :eql => '==',
32
- :not_eql => '!=',
33
- :gt => '>',
34
- :gte => '>=',
35
- :lt => '<',
36
- :lte => '<=',
37
- :matches => '==',
38
- :does_not_match => '!=',
39
- :contains => '=~',
40
- :does_not_contain => '!~',
41
- :substring => '=@',
42
- :not_substring => '!@'
43
- end
44
- end