google_analytics_feeds 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +14 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +62 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/google_analytics_feeds.gemspec +70 -0
- data/lib/google_analytics_feeds.rb +244 -0
- data/lib/sample.rb +15 -0
- data/spec/google_analytics_feeds_spec.rb +11 -0
- data/spec/request_spec.rb +47 -0
- data/spec/spec_helper.rb +12 -0
- metadata +177 -0
    
        data/.document
    ADDED
    
    
    
        data/.rspec
    ADDED
    
    | @@ -0,0 +1 @@ | |
| 1 | 
            +
            --color
         | 
    
        data/Gemfile
    ADDED
    
    | @@ -0,0 +1,14 @@ | |
| 1 | 
            +
            source "http://rubygems.org"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            gem "ox", "< 2"
         | 
| 4 | 
            +
            gem "faraday"
         | 
| 5 | 
            +
            gem "addressable"
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            # Add dependencies to develop your gem here.
         | 
| 8 | 
            +
            # Include everything needed to run rake, tests, features, etc.
         | 
| 9 | 
            +
            group :development do
         | 
| 10 | 
            +
              gem "rspec", "~> 2"
         | 
| 11 | 
            +
              gem "rdoc", "~> 3.12"
         | 
| 12 | 
            +
              gem "jeweler", "~> 1.8"
         | 
| 13 | 
            +
              gem "rcov", ">= 0"
         | 
| 14 | 
            +
            end
         | 
    
        data/LICENSE.txt
    ADDED
    
    | @@ -0,0 +1,20 @@ | |
| 1 | 
            +
            Copyright (c) 2012 Roland Swingler
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Permission is hereby granted, free of charge, to any person obtaining
         | 
| 4 | 
            +
            a copy of this software and associated documentation files (the
         | 
| 5 | 
            +
            "Software"), to deal in the Software without restriction, including
         | 
| 6 | 
            +
            without limitation the rights to use, copy, modify, merge, publish,
         | 
| 7 | 
            +
            distribute, sublicense, and/or sell copies of the Software, and to
         | 
| 8 | 
            +
            permit persons to whom the Software is furnished to do so, subject to
         | 
| 9 | 
            +
            the following conditions:
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            The above copyright notice and this permission notice shall be
         | 
| 12 | 
            +
            included in all copies or substantial portions of the Software.
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
         | 
| 15 | 
            +
            EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
         | 
| 16 | 
            +
            MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
         | 
| 17 | 
            +
            NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
         | 
| 18 | 
            +
            LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
         | 
| 19 | 
            +
            OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
         | 
| 20 | 
            +
            WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
         | 
    
        data/README.rdoc
    ADDED
    
    | @@ -0,0 +1,62 @@ | |
| 1 | 
            +
            = Google Analytics Feeds
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Allows access to google analytics feeds from ruby.
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            Does not support any of the OAuth-related authentication methods.
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            == Installation
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                gem install google_analytics_feeds
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            == Usage
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                require 'google_analytics_feeds'
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                # Define your report. Reports are built up like an ActiveRecord or 
         | 
| 16 | 
            +
                # Sequel::Dataset query.
         | 
| 17 | 
            +
                report = GoogleAnalyticsFeeds::DataFeed.new.
         | 
| 18 | 
            +
                  profile(123456).
         | 
| 19 | 
            +
                  dimensions(:visitor_type).
         | 
| 20 | 
            +
                  metrics(:visits)
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                # Create a session and login. You can optionally pass a
         | 
| 23 | 
            +
                # Faraday::Connection to +new+ to use a different adapter, deal 
         | 
| 24 | 
            +
                # with proxies etc.
         | 
| 25 | 
            +
                session = GoogleAnalyticsFeeds::Session.new
         | 
| 26 | 
            +
                session.login("username", "password")
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                # Fetch a report. Rows are handled with a 
         | 
| 29 | 
            +
                # GoogleAnalyticsFeed::RowHandler - this may just be an anonymous
         | 
| 30 | 
            +
                # block as demonstrated here.
         | 
| 31 | 
            +
                session.fetch_report(report) do
         | 
| 32 | 
            +
                  def row(r)
         | 
| 33 | 
            +
                    puts r.inspect # => outputs the row as a hash.
         | 
| 34 | 
            +
                  end
         | 
| 35 | 
            +
                end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
            == TODO
         | 
| 38 | 
            +
             | 
| 39 | 
            +
            * Tests, documentation
         | 
| 40 | 
            +
            * Filters
         | 
| 41 | 
            +
            * Access to the management data feed.
         | 
| 42 | 
            +
            * Auto-follow of paginated result sets.
         | 
| 43 | 
            +
             | 
| 44 | 
            +
            == Versioning
         | 
| 45 | 
            +
             | 
| 46 | 
            +
            Patch-level releases will not change the API. Minor releases may make breaking changes to the API until a 1.0 release.
         | 
| 47 | 
            +
             | 
| 48 | 
            +
            == Contributing to google_analytics_feeds
         | 
| 49 | 
            +
             
         | 
| 50 | 
            +
            * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
         | 
| 51 | 
            +
            * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
         | 
| 52 | 
            +
            * Fork the project.
         | 
| 53 | 
            +
            * Start a feature/bugfix branch.
         | 
| 54 | 
            +
            * Commit and push until you are happy with your contribution.
         | 
| 55 | 
            +
            * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
         | 
| 56 | 
            +
            * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
         | 
| 57 | 
            +
             | 
| 58 | 
            +
            == Copyright
         | 
| 59 | 
            +
             | 
| 60 | 
            +
            Copyright (c) 2012 Roland Swingler. See LICENSE.txt for
         | 
| 61 | 
            +
            further details.
         | 
| 62 | 
            +
             | 
    
        data/Rakefile
    ADDED
    
    | @@ -0,0 +1,49 @@ | |
| 1 | 
            +
            # encoding: utf-8
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'rubygems'
         | 
| 4 | 
            +
            require 'bundler'
         | 
| 5 | 
            +
            begin
         | 
| 6 | 
            +
              Bundler.setup(:default, :development)
         | 
| 7 | 
            +
            rescue Bundler::BundlerError => e
         | 
| 8 | 
            +
              $stderr.puts e.message
         | 
| 9 | 
            +
              $stderr.puts "Run `bundle install` to install missing gems"
         | 
| 10 | 
            +
              exit e.status_code
         | 
| 11 | 
            +
            end
         | 
| 12 | 
            +
            require 'rake'
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            require 'jeweler'
         | 
| 15 | 
            +
            Jeweler::Tasks.new do |gem|
         | 
| 16 | 
            +
              # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
         | 
| 17 | 
            +
              gem.name = "google_analytics_feeds"
         | 
| 18 | 
            +
              gem.homepage = "http://github.com/knaveofdiamonds/google_analytics_feeds"
         | 
| 19 | 
            +
              gem.license = "MIT"
         | 
| 20 | 
            +
              gem.summary = %Q{Google Analytics Feed Access}
         | 
| 21 | 
            +
              gem.description = %Q{Fairly low-level client library to access Google Analytics Feeds}
         | 
| 22 | 
            +
              gem.email = "roland.swingler@gmail.com"
         | 
| 23 | 
            +
              gem.authors = ["Roland Swingler"]
         | 
| 24 | 
            +
              # dependencies defined in Gemfile
         | 
| 25 | 
            +
            end
         | 
| 26 | 
            +
            Jeweler::RubygemsDotOrgTasks.new
         | 
| 27 | 
            +
             | 
| 28 | 
            +
            require 'rspec/core'
         | 
| 29 | 
            +
            require 'rspec/core/rake_task'
         | 
| 30 | 
            +
            RSpec::Core::RakeTask.new(:spec) do |spec|
         | 
| 31 | 
            +
              spec.pattern = FileList['spec/**/*_spec.rb']
         | 
| 32 | 
            +
            end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
            RSpec::Core::RakeTask.new(:rcov) do |spec|
         | 
| 35 | 
            +
              spec.pattern = 'spec/**/*_spec.rb'
         | 
| 36 | 
            +
              spec.rcov = true
         | 
| 37 | 
            +
            end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
            task :default => :spec
         | 
| 40 | 
            +
             | 
| 41 | 
            +
            require 'rdoc/task'
         | 
| 42 | 
            +
            Rake::RDocTask.new do |rdoc|
         | 
| 43 | 
            +
              version = File.exist?('VERSION') ? File.read('VERSION') : ""
         | 
| 44 | 
            +
             | 
| 45 | 
            +
              rdoc.rdoc_dir = 'rdoc'
         | 
| 46 | 
            +
              rdoc.title = "google_analytics_feeds #{version}"
         | 
| 47 | 
            +
              rdoc.rdoc_files.include('README*')
         | 
| 48 | 
            +
              rdoc.rdoc_files.include('lib/**/*.rb')
         | 
| 49 | 
            +
            end
         | 
    
        data/VERSION
    ADDED
    
    | @@ -0,0 +1 @@ | |
| 1 | 
            +
            0.0.1
         | 
| @@ -0,0 +1,70 @@ | |
| 1 | 
            +
            # Generated by jeweler
         | 
| 2 | 
            +
            # DO NOT EDIT THIS FILE DIRECTLY
         | 
| 3 | 
            +
            # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
         | 
| 4 | 
            +
            # -*- encoding: utf-8 -*-
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            Gem::Specification.new do |s|
         | 
| 7 | 
            +
              s.name = %q{google_analytics_feeds}
         | 
| 8 | 
            +
              s.version = "0.0.1"
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
         | 
| 11 | 
            +
              s.authors = [%q{Roland Swingler}]
         | 
| 12 | 
            +
              s.date = %q{2012-08-29}
         | 
| 13 | 
            +
              s.description = %q{Fairly low-level client library to access Google Analytics Feeds}
         | 
| 14 | 
            +
              s.email = %q{roland.swingler@gmail.com}
         | 
| 15 | 
            +
              s.extra_rdoc_files = [
         | 
| 16 | 
            +
                "LICENSE.txt",
         | 
| 17 | 
            +
                "README.rdoc"
         | 
| 18 | 
            +
              ]
         | 
| 19 | 
            +
              s.files = [
         | 
| 20 | 
            +
                ".document",
         | 
| 21 | 
            +
                ".rspec",
         | 
| 22 | 
            +
                "Gemfile",
         | 
| 23 | 
            +
                "LICENSE.txt",
         | 
| 24 | 
            +
                "README.rdoc",
         | 
| 25 | 
            +
                "Rakefile",
         | 
| 26 | 
            +
                "VERSION",
         | 
| 27 | 
            +
                "google_analytics_feeds.gemspec",
         | 
| 28 | 
            +
                "lib/google_analytics_feeds.rb",
         | 
| 29 | 
            +
                "lib/sample.rb",
         | 
| 30 | 
            +
                "spec/google_analytics_feeds_spec.rb",
         | 
| 31 | 
            +
                "spec/request_spec.rb",
         | 
| 32 | 
            +
                "spec/spec_helper.rb"
         | 
| 33 | 
            +
              ]
         | 
| 34 | 
            +
              s.homepage = %q{http://github.com/knaveofdiamonds/google_analytics_feeds}
         | 
| 35 | 
            +
              s.licenses = [%q{MIT}]
         | 
| 36 | 
            +
              s.require_paths = [%q{lib}]
         | 
| 37 | 
            +
              s.rubygems_version = %q{1.8.6}
         | 
| 38 | 
            +
              s.summary = %q{Google Analytics Feed Access}
         | 
| 39 | 
            +
             | 
| 40 | 
            +
              if s.respond_to? :specification_version then
         | 
| 41 | 
            +
                s.specification_version = 3
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
         | 
| 44 | 
            +
                  s.add_runtime_dependency(%q<ox>, ["< 2"])
         | 
| 45 | 
            +
                  s.add_runtime_dependency(%q<faraday>, [">= 0"])
         | 
| 46 | 
            +
                  s.add_runtime_dependency(%q<addressable>, [">= 0"])
         | 
| 47 | 
            +
                  s.add_development_dependency(%q<rspec>, ["~> 2"])
         | 
| 48 | 
            +
                  s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
         | 
| 49 | 
            +
                  s.add_development_dependency(%q<jeweler>, ["~> 1.8"])
         | 
| 50 | 
            +
                  s.add_development_dependency(%q<rcov>, [">= 0"])
         | 
| 51 | 
            +
                else
         | 
| 52 | 
            +
                  s.add_dependency(%q<ox>, ["< 2"])
         | 
| 53 | 
            +
                  s.add_dependency(%q<faraday>, [">= 0"])
         | 
| 54 | 
            +
                  s.add_dependency(%q<addressable>, [">= 0"])
         | 
| 55 | 
            +
                  s.add_dependency(%q<rspec>, ["~> 2"])
         | 
| 56 | 
            +
                  s.add_dependency(%q<rdoc>, ["~> 3.12"])
         | 
| 57 | 
            +
                  s.add_dependency(%q<jeweler>, ["~> 1.8"])
         | 
| 58 | 
            +
                  s.add_dependency(%q<rcov>, [">= 0"])
         | 
| 59 | 
            +
                end
         | 
| 60 | 
            +
              else
         | 
| 61 | 
            +
                s.add_dependency(%q<ox>, ["< 2"])
         | 
| 62 | 
            +
                s.add_dependency(%q<faraday>, [">= 0"])
         | 
| 63 | 
            +
                s.add_dependency(%q<addressable>, [">= 0"])
         | 
| 64 | 
            +
                s.add_dependency(%q<rspec>, ["~> 2"])
         | 
| 65 | 
            +
                s.add_dependency(%q<rdoc>, ["~> 3.12"])
         | 
| 66 | 
            +
                s.add_dependency(%q<jeweler>, ["~> 1.8"])
         | 
| 67 | 
            +
                s.add_dependency(%q<rcov>, [">= 0"])
         | 
| 68 | 
            +
              end
         | 
| 69 | 
            +
            end
         | 
| 70 | 
            +
             | 
| @@ -0,0 +1,244 @@ | |
| 1 | 
            +
            require 'ox'
         | 
| 2 | 
            +
            require 'addressable/uri'
         | 
| 3 | 
            +
            require 'stringio'
         | 
| 4 | 
            +
            require 'faraday'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            module GoogleAnalyticsFeeds
         | 
| 7 | 
            +
              class AuthenticationError < StandardError ; end
         | 
| 8 | 
            +
              class HttpError < StandardError ; end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              class Session
         | 
| 11 | 
            +
                CLIENT_LOGIN_URI = "https://www.google.com/accounts/ClientLogin"
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                def initialize(connection=Faraday.default_connection)
         | 
| 14 | 
            +
                  @connection = connection
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                def login(username, password)
         | 
| 18 | 
            +
                  return @token if @token
         | 
| 19 | 
            +
                  response = @connection.post(CLIENT_LOGIN_URI,
         | 
| 20 | 
            +
                                              'Email'       => username,
         | 
| 21 | 
            +
                                              'Passwd'      => password,
         | 
| 22 | 
            +
                                              'accountType' => 'HOSTED_OR_GOOGLE',
         | 
| 23 | 
            +
                                              'service'     => 'analytics',
         | 
| 24 | 
            +
                                              'source'      => 'ruby-google-analytics-feeds')
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                  if response.success?
         | 
| 27 | 
            +
                    @token = response.body.match(/^Auth=(.*)$/)[1]
         | 
| 28 | 
            +
                  else
         | 
| 29 | 
            +
                    raise AuthenticationError
         | 
| 30 | 
            +
                  end
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                def fetch_report(report, handler=nil, &block)
         | 
| 34 | 
            +
                  handler  = block if handler.nil?
         | 
| 35 | 
            +
                  response = report.retrieve(@token, @connection)
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                  if response.success?
         | 
| 38 | 
            +
                    DataFeedParser.new(handler).parse_rows(StringIO.new(response.body))
         | 
| 39 | 
            +
                  else
         | 
| 40 | 
            +
                    raise HttpError.new
         | 
| 41 | 
            +
                  end
         | 
| 42 | 
            +
                end
         | 
| 43 | 
            +
              end
         | 
| 44 | 
            +
             | 
| 45 | 
            +
              # A SAX-style row handler.
         | 
| 46 | 
            +
              #
         | 
| 47 | 
            +
              # Extend this class and override the methods you care about to
         | 
| 48 | 
            +
              # handle data feed row data.
         | 
| 49 | 
            +
              class RowHandler
         | 
| 50 | 
            +
                def start_rows
         | 
| 51 | 
            +
                end
         | 
| 52 | 
            +
             | 
| 53 | 
            +
                def row(row)
         | 
| 54 | 
            +
                end
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                def end_rows
         | 
| 57 | 
            +
                end
         | 
| 58 | 
            +
              end
         | 
| 59 | 
            +
              
         | 
| 60 | 
            +
              module Naming
         | 
| 61 | 
            +
                # Returns a ruby-friendly symbol from a google analytics name.
         | 
| 62 | 
            +
                #
         | 
| 63 | 
            +
                # For example:
         | 
| 64 | 
            +
                #
         | 
| 65 | 
            +
                #     name_to_symbol("ga:visitorType") # => :visitor_type
         | 
| 66 | 
            +
                def name_to_symbol(name)
         | 
| 67 | 
            +
                  name.sub(/^ga\:/,'').gsub(/(.)([A-Z])/,'\1_\2').downcase.to_sym
         | 
| 68 | 
            +
                end
         | 
| 69 | 
            +
                
         | 
| 70 | 
            +
                # Returns a google analytics name from a ruby symbol.
         | 
| 71 | 
            +
                #
         | 
| 72 | 
            +
                # For example:
         | 
| 73 | 
            +
                #
         | 
| 74 | 
            +
                #     symbol_to_name(:visitor_type) # => "ga:visitorType"
         | 
| 75 | 
            +
                def symbol_to_name(sym)
         | 
| 76 | 
            +
                  parts = sym.to_s.split("_").map(&:capitalize)
         | 
| 77 | 
            +
                  parts[0].downcase!
         | 
| 78 | 
            +
                  "ga:" + parts.join('')
         | 
| 79 | 
            +
                end
         | 
| 80 | 
            +
              end
         | 
| 81 | 
            +
             | 
| 82 | 
            +
              # Parses rows from the GA feed via SAX. Clients shouldn't have to
         | 
| 83 | 
            +
              # use this - use a RowHandler instead.
         | 
| 84 | 
            +
              class RowParser < ::Ox::Sax
         | 
| 85 | 
            +
                include Naming
         | 
| 86 | 
            +
             | 
| 87 | 
            +
                def initialize(handler)
         | 
| 88 | 
            +
                  @handler = handler
         | 
| 89 | 
            +
                end
         | 
| 90 | 
            +
             | 
| 91 | 
            +
                def start_element(element)
         | 
| 92 | 
            +
                  case element
         | 
| 93 | 
            +
             | 
| 94 | 
            +
                  when :entry
         | 
| 95 | 
            +
                    @row = {}
         | 
| 96 | 
            +
                  when :"dxp:dimension", :"dxp:metric"
         | 
| 97 | 
            +
                    @property = {}
         | 
| 98 | 
            +
                  end
         | 
| 99 | 
            +
                end
         | 
| 100 | 
            +
             | 
| 101 | 
            +
                def attr(name, value)
         | 
| 102 | 
            +
                  if @property
         | 
| 103 | 
            +
                    @property[name] = value
         | 
| 104 | 
            +
                  end
         | 
| 105 | 
            +
                end
         | 
| 106 | 
            +
             | 
| 107 | 
            +
                def end_element(element)
         | 
| 108 | 
            +
                  case element
         | 
| 109 | 
            +
                  when :entry
         | 
| 110 | 
            +
                    handle_complete_row
         | 
| 111 | 
            +
                    @row = nil
         | 
| 112 | 
            +
                  when :"dxp:dimension", :"dxp:metric"
         | 
| 113 | 
            +
                    handle_complete_property
         | 
| 114 | 
            +
                    @property = nil
         | 
| 115 | 
            +
                  end
         | 
| 116 | 
            +
                end
         | 
| 117 | 
            +
                
         | 
| 118 | 
            +
                private
         | 
| 119 | 
            +
             | 
| 120 | 
            +
                def handle_complete_row
         | 
| 121 | 
            +
                  @handler.row(@row)
         | 
| 122 | 
            +
                end
         | 
| 123 | 
            +
             | 
| 124 | 
            +
                def handle_complete_property
         | 
| 125 | 
            +
                  if @row
         | 
| 126 | 
            +
                    value = @property[:value]
         | 
| 127 | 
            +
                    if @property[:type] == "integer"
         | 
| 128 | 
            +
                      value = Integer(value)
         | 
| 129 | 
            +
                    end
         | 
| 130 | 
            +
                    name = name_to_symbol(@property[:name])
         | 
| 131 | 
            +
                    @row[name] = value
         | 
| 132 | 
            +
                  end
         | 
| 133 | 
            +
                end
         | 
| 134 | 
            +
              end
         | 
| 135 | 
            +
             | 
| 136 | 
            +
              class DataFeed
         | 
| 137 | 
            +
                include Naming
         | 
| 138 | 
            +
                
         | 
| 139 | 
            +
                BASE_URI = "https://www.googleapis.com/analytics/v2.4/data"
         | 
| 140 | 
            +
             | 
| 141 | 
            +
                def initialize
         | 
| 142 | 
            +
                  @params = {}
         | 
| 143 | 
            +
                end
         | 
| 144 | 
            +
             | 
| 145 | 
            +
                def profile(id)
         | 
| 146 | 
            +
                  clone_and_set {|params|
         | 
| 147 | 
            +
                    params['ids'] = symbol_to_name(id)
         | 
| 148 | 
            +
                  }
         | 
| 149 | 
            +
                end
         | 
| 150 | 
            +
             | 
| 151 | 
            +
                def metrics(*vals)
         | 
| 152 | 
            +
                  clone_and_set {|params|
         | 
| 153 | 
            +
                    params['metrics'] = vals.map {|v| symbol_to_name(v) }.join(',')
         | 
| 154 | 
            +
                  }
         | 
| 155 | 
            +
                end
         | 
| 156 | 
            +
             | 
| 157 | 
            +
                def dimensions(*vals)
         | 
| 158 | 
            +
                  clone_and_set {|params|
         | 
| 159 | 
            +
                    params['dimensions'] = vals.map {|v| symbol_to_name(v) }.join(',')
         | 
| 160 | 
            +
                  }
         | 
| 161 | 
            +
                end
         | 
| 162 | 
            +
             | 
| 163 | 
            +
                def dates(start_date, end_date)
         | 
| 164 | 
            +
                  clone_and_set {|params|
         | 
| 165 | 
            +
                    params['start-date'] = start_date.strftime("%Y-%m-%d")
         | 
| 166 | 
            +
                    params['end-date'] = end_date.strftime("%Y-%m-%d")
         | 
| 167 | 
            +
                  }
         | 
| 168 | 
            +
                end
         | 
| 169 | 
            +
             | 
| 170 | 
            +
                def start_index(i)
         | 
| 171 | 
            +
                  clone_and_set {|params|
         | 
| 172 | 
            +
                    params['start-index'] = i.to_s
         | 
| 173 | 
            +
                  }
         | 
| 174 | 
            +
                end
         | 
| 175 | 
            +
             | 
| 176 | 
            +
                def max_results(i)
         | 
| 177 | 
            +
                  clone_and_set {|params|
         | 
| 178 | 
            +
                    params['max-results'] = i.to_s
         | 
| 179 | 
            +
                  }
         | 
| 180 | 
            +
                end
         | 
| 181 | 
            +
             | 
| 182 | 
            +
                # Sorts the result set by a column.
         | 
| 183 | 
            +
                #
         | 
| 184 | 
            +
                # Direction can be :asc or :desc.
         | 
| 185 | 
            +
                def sort(column, direction)
         | 
| 186 | 
            +
                  clone_and_set {|params|
         | 
| 187 | 
            +
                    c = symbol_to_name(column)
         | 
| 188 | 
            +
                    params['sort'] = (direction == :desc ? "-#{c}" : c)
         | 
| 189 | 
            +
                  }
         | 
| 190 | 
            +
                end
         | 
| 191 | 
            +
             | 
| 192 | 
            +
                def uri
         | 
| 193 | 
            +
                  uri = Addressable::URI.parse(BASE_URI)
         | 
| 194 | 
            +
                  uri.query_values = @params
         | 
| 195 | 
            +
                  uri.to_s
         | 
| 196 | 
            +
                end
         | 
| 197 | 
            +
             | 
| 198 | 
            +
                alias :to_s :uri
         | 
| 199 | 
            +
             | 
| 200 | 
            +
                def retrieve(session_token, connection)
         | 
| 201 | 
            +
                  connection.get(uri) do |request|
         | 
| 202 | 
            +
                    request.headers['Authorization'] = 
         | 
| 203 | 
            +
                      "GoogleLogin auth=#{session_token}"
         | 
| 204 | 
            +
                  end
         | 
| 205 | 
            +
                end
         | 
| 206 | 
            +
             | 
| 207 | 
            +
                def clone
         | 
| 208 | 
            +
                  obj = super
         | 
| 209 | 
            +
                  obj.instance_variable_set(:@params, @params.clone)
         | 
| 210 | 
            +
                  obj
         | 
| 211 | 
            +
                end
         | 
| 212 | 
            +
             | 
| 213 | 
            +
                protected
         | 
| 214 | 
            +
             | 
| 215 | 
            +
                attr_reader :params
         | 
| 216 | 
            +
             | 
| 217 | 
            +
                private
         | 
| 218 | 
            +
             | 
| 219 | 
            +
                def clone_and_set
         | 
| 220 | 
            +
                  obj = clone
         | 
| 221 | 
            +
                  yield obj.params
         | 
| 222 | 
            +
                  obj
         | 
| 223 | 
            +
                end
         | 
| 224 | 
            +
              end
         | 
| 225 | 
            +
             | 
| 226 | 
            +
              class DataFeedParser
         | 
| 227 | 
            +
                def initialize(handler)
         | 
| 228 | 
            +
                  if handler.kind_of?(Proc)
         | 
| 229 | 
            +
                    @handler = Class.new(RowHandler, &handler).new
         | 
| 230 | 
            +
                  elsif handler.kind_of?(Class)
         | 
| 231 | 
            +
                    @handler = handler.new
         | 
| 232 | 
            +
                  else
         | 
| 233 | 
            +
                    @handler = handler
         | 
| 234 | 
            +
                  end
         | 
| 235 | 
            +
                end
         | 
| 236 | 
            +
             | 
| 237 | 
            +
                # Parse rows from an IO object.
         | 
| 238 | 
            +
                def parse_rows(io)
         | 
| 239 | 
            +
                  @handler.start_rows
         | 
| 240 | 
            +
                  Ox.sax_parse(RowParser.new(@handler), io)
         | 
| 241 | 
            +
                  @handler.end_rows
         | 
| 242 | 
            +
                end
         | 
| 243 | 
            +
              end
         | 
| 244 | 
            +
            end
         | 
    
        data/lib/sample.rb
    ADDED
    
    | @@ -0,0 +1,15 @@ | |
| 1 | 
            +
            require File.dirname(__FILE__) + "/google_analytics_feeds" 
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class LoggingRowHandler < GoogleAnalyticsFeeds::RowHandler
         | 
| 4 | 
            +
              def row(r)
         | 
| 5 | 
            +
                puts r.inspect
         | 
| 6 | 
            +
              end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              def end_rows
         | 
| 9 | 
            +
                puts "Done!"
         | 
| 10 | 
            +
              end
         | 
| 11 | 
            +
            end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            File.open("/home/roland/gaout2.xml") do |fh|
         | 
| 14 | 
            +
              GoogleAnalyticsFeeds.parse_data_rows(fh, LoggingRowHandler)
         | 
| 15 | 
            +
            end
         | 
| @@ -0,0 +1,11 @@ | |
| 1 | 
            +
            require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe "Parsing property names" do
         | 
| 4 | 
            +
              it "snake cases, symbolizes and removes ga namespace" do
         | 
| 5 | 
            +
                GoogleAnalyticsFeeds::DataFeed.new.name_to_symbol("ga:visitorType").should == :visitor_type
         | 
| 6 | 
            +
              end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              it "converts a ruby symbol to a google analytics name" do
         | 
| 9 | 
            +
                GoogleAnalyticsFeeds::DataFeed.new.symbol_to_name(:visitor_type).should == "ga:visitorType"
         | 
| 10 | 
            +
              end
         | 
| 11 | 
            +
            end
         | 
| @@ -0,0 +1,47 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
            require 'addressable/uri'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            describe GoogleAnalyticsFeeds::DataFeed do
         | 
| 5 | 
            +
              it "builds a URI" do
         | 
| 6 | 
            +
                request = described_class.new.
         | 
| 7 | 
            +
                  profile(123).
         | 
| 8 | 
            +
                  metrics(:foo, :bar).
         | 
| 9 | 
            +
                  dimensions(:baz).
         | 
| 10 | 
            +
                  dates(Date.new(2010, 3, 14), Date.new(2010, 3, 14)).
         | 
| 11 | 
            +
                  max_results(50).
         | 
| 12 | 
            +
                  start_index(10)
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                uri = Addressable::URI.parse(request.uri)
         | 
| 15 | 
            +
                uri.scheme.should == "https"
         | 
| 16 | 
            +
                uri.host.should == "www.googleapis.com"
         | 
| 17 | 
            +
                uri.path.should == "/analytics/v2.4/data"
         | 
| 18 | 
            +
                uri.query_values.should == {
         | 
| 19 | 
            +
                  "ids" => "ga:123",
         | 
| 20 | 
            +
                  "metrics" => "ga:foo,ga:bar",
         | 
| 21 | 
            +
                  "dimensions" => "ga:baz",
         | 
| 22 | 
            +
                  "start-date" => "2010-03-14",
         | 
| 23 | 
            +
                  "end-date" => "2010-03-14",
         | 
| 24 | 
            +
                  "max-results" => "50",
         | 
| 25 | 
            +
                  "start-index" => "10",
         | 
| 26 | 
            +
                }
         | 
| 27 | 
            +
              end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
              it "doesn't modify the original request" do
         | 
| 30 | 
            +
                request = described_class.new.profile(123)
         | 
| 31 | 
            +
                request.metrics(:foo) # result thrown away
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                uri = Addressable::URI.parse(request.uri)
         | 
| 34 | 
            +
                uri.query_values.should == {"ids" => "ga:123"}
         | 
| 35 | 
            +
              end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
              it "adds the appropriate header and makes the request" do
         | 
| 38 | 
            +
                connection = mock(:connection)
         | 
| 39 | 
            +
                headers    = {}
         | 
| 40 | 
            +
                request    = mock(:request, :headers => headers).as_null_object
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                connection.should_receive(:get).and_yield(request)
         | 
| 43 | 
            +
                described_class.new.retrieve("123", connection)
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                headers["Authorization"].should == "GoogleLogin auth=123"
         | 
| 46 | 
            +
              end
         | 
| 47 | 
            +
            end
         | 
    
        data/spec/spec_helper.rb
    ADDED
    
    | @@ -0,0 +1,12 @@ | |
| 1 | 
            +
            $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
         | 
| 2 | 
            +
            $LOAD_PATH.unshift(File.dirname(__FILE__))
         | 
| 3 | 
            +
            require 'rspec'
         | 
| 4 | 
            +
            require 'google_analytics_feeds'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            # Requires supporting files with custom matchers and macros, etc,
         | 
| 7 | 
            +
            # in ./support/ and its subdirectories.
         | 
| 8 | 
            +
            Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            RSpec.configure do |config|
         | 
| 11 | 
            +
              
         | 
| 12 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,177 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification 
         | 
| 2 | 
            +
            name: google_analytics_feeds
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            +
              hash: 29
         | 
| 5 | 
            +
              prerelease: 
         | 
| 6 | 
            +
              segments: 
         | 
| 7 | 
            +
              - 0
         | 
| 8 | 
            +
              - 0
         | 
| 9 | 
            +
              - 1
         | 
| 10 | 
            +
              version: 0.0.1
         | 
| 11 | 
            +
            platform: ruby
         | 
| 12 | 
            +
            authors: 
         | 
| 13 | 
            +
            - Roland Swingler
         | 
| 14 | 
            +
            autorequire: 
         | 
| 15 | 
            +
            bindir: bin
         | 
| 16 | 
            +
            cert_chain: []
         | 
| 17 | 
            +
             | 
| 18 | 
            +
            date: 2012-08-29 00:00:00 Z
         | 
| 19 | 
            +
            dependencies: 
         | 
| 20 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 21 | 
            +
              requirement: &id001 !ruby/object:Gem::Requirement 
         | 
| 22 | 
            +
                none: false
         | 
| 23 | 
            +
                requirements: 
         | 
| 24 | 
            +
                - - <
         | 
| 25 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 26 | 
            +
                    hash: 7
         | 
| 27 | 
            +
                    segments: 
         | 
| 28 | 
            +
                    - 2
         | 
| 29 | 
            +
                    version: "2"
         | 
| 30 | 
            +
              version_requirements: *id001
         | 
| 31 | 
            +
              name: ox
         | 
| 32 | 
            +
              prerelease: false
         | 
| 33 | 
            +
              type: :runtime
         | 
| 34 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 35 | 
            +
              requirement: &id002 !ruby/object:Gem::Requirement 
         | 
| 36 | 
            +
                none: false
         | 
| 37 | 
            +
                requirements: 
         | 
| 38 | 
            +
                - - ">="
         | 
| 39 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 40 | 
            +
                    hash: 3
         | 
| 41 | 
            +
                    segments: 
         | 
| 42 | 
            +
                    - 0
         | 
| 43 | 
            +
                    version: "0"
         | 
| 44 | 
            +
              version_requirements: *id002
         | 
| 45 | 
            +
              name: faraday
         | 
| 46 | 
            +
              prerelease: false
         | 
| 47 | 
            +
              type: :runtime
         | 
| 48 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 49 | 
            +
              requirement: &id003 !ruby/object:Gem::Requirement 
         | 
| 50 | 
            +
                none: false
         | 
| 51 | 
            +
                requirements: 
         | 
| 52 | 
            +
                - - ">="
         | 
| 53 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 54 | 
            +
                    hash: 3
         | 
| 55 | 
            +
                    segments: 
         | 
| 56 | 
            +
                    - 0
         | 
| 57 | 
            +
                    version: "0"
         | 
| 58 | 
            +
              version_requirements: *id003
         | 
| 59 | 
            +
              name: addressable
         | 
| 60 | 
            +
              prerelease: false
         | 
| 61 | 
            +
              type: :runtime
         | 
| 62 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 63 | 
            +
              requirement: &id004 !ruby/object:Gem::Requirement 
         | 
| 64 | 
            +
                none: false
         | 
| 65 | 
            +
                requirements: 
         | 
| 66 | 
            +
                - - ~>
         | 
| 67 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 68 | 
            +
                    hash: 7
         | 
| 69 | 
            +
                    segments: 
         | 
| 70 | 
            +
                    - 2
         | 
| 71 | 
            +
                    version: "2"
         | 
| 72 | 
            +
              version_requirements: *id004
         | 
| 73 | 
            +
              name: rspec
         | 
| 74 | 
            +
              prerelease: false
         | 
| 75 | 
            +
              type: :development
         | 
| 76 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 77 | 
            +
              requirement: &id005 !ruby/object:Gem::Requirement 
         | 
| 78 | 
            +
                none: false
         | 
| 79 | 
            +
                requirements: 
         | 
| 80 | 
            +
                - - ~>
         | 
| 81 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 82 | 
            +
                    hash: 31
         | 
| 83 | 
            +
                    segments: 
         | 
| 84 | 
            +
                    - 3
         | 
| 85 | 
            +
                    - 12
         | 
| 86 | 
            +
                    version: "3.12"
         | 
| 87 | 
            +
              version_requirements: *id005
         | 
| 88 | 
            +
              name: rdoc
         | 
| 89 | 
            +
              prerelease: false
         | 
| 90 | 
            +
              type: :development
         | 
| 91 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 92 | 
            +
              requirement: &id006 !ruby/object:Gem::Requirement 
         | 
| 93 | 
            +
                none: false
         | 
| 94 | 
            +
                requirements: 
         | 
| 95 | 
            +
                - - ~>
         | 
| 96 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 97 | 
            +
                    hash: 31
         | 
| 98 | 
            +
                    segments: 
         | 
| 99 | 
            +
                    - 1
         | 
| 100 | 
            +
                    - 8
         | 
| 101 | 
            +
                    version: "1.8"
         | 
| 102 | 
            +
              version_requirements: *id006
         | 
| 103 | 
            +
              name: jeweler
         | 
| 104 | 
            +
              prerelease: false
         | 
| 105 | 
            +
              type: :development
         | 
| 106 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 107 | 
            +
              requirement: &id007 !ruby/object:Gem::Requirement 
         | 
| 108 | 
            +
                none: false
         | 
| 109 | 
            +
                requirements: 
         | 
| 110 | 
            +
                - - ">="
         | 
| 111 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 112 | 
            +
                    hash: 3
         | 
| 113 | 
            +
                    segments: 
         | 
| 114 | 
            +
                    - 0
         | 
| 115 | 
            +
                    version: "0"
         | 
| 116 | 
            +
              version_requirements: *id007
         | 
| 117 | 
            +
              name: rcov
         | 
| 118 | 
            +
              prerelease: false
         | 
| 119 | 
            +
              type: :development
         | 
| 120 | 
            +
            description: Fairly low-level client library to access Google Analytics Feeds
         | 
| 121 | 
            +
            email: roland.swingler@gmail.com
         | 
| 122 | 
            +
            executables: []
         | 
| 123 | 
            +
             | 
| 124 | 
            +
            extensions: []
         | 
| 125 | 
            +
             | 
| 126 | 
            +
            extra_rdoc_files: 
         | 
| 127 | 
            +
            - LICENSE.txt
         | 
| 128 | 
            +
            - README.rdoc
         | 
| 129 | 
            +
            files: 
         | 
| 130 | 
            +
            - .document
         | 
| 131 | 
            +
            - .rspec
         | 
| 132 | 
            +
            - Gemfile
         | 
| 133 | 
            +
            - LICENSE.txt
         | 
| 134 | 
            +
            - README.rdoc
         | 
| 135 | 
            +
            - Rakefile
         | 
| 136 | 
            +
            - VERSION
         | 
| 137 | 
            +
            - google_analytics_feeds.gemspec
         | 
| 138 | 
            +
            - lib/google_analytics_feeds.rb
         | 
| 139 | 
            +
            - lib/sample.rb
         | 
| 140 | 
            +
            - spec/google_analytics_feeds_spec.rb
         | 
| 141 | 
            +
            - spec/request_spec.rb
         | 
| 142 | 
            +
            - spec/spec_helper.rb
         | 
| 143 | 
            +
            homepage: http://github.com/knaveofdiamonds/google_analytics_feeds
         | 
| 144 | 
            +
            licenses: 
         | 
| 145 | 
            +
            - MIT
         | 
| 146 | 
            +
            post_install_message: 
         | 
| 147 | 
            +
            rdoc_options: []
         | 
| 148 | 
            +
             | 
| 149 | 
            +
            require_paths: 
         | 
| 150 | 
            +
            - lib
         | 
| 151 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement 
         | 
| 152 | 
            +
              none: false
         | 
| 153 | 
            +
              requirements: 
         | 
| 154 | 
            +
              - - ">="
         | 
| 155 | 
            +
                - !ruby/object:Gem::Version 
         | 
| 156 | 
            +
                  hash: 3
         | 
| 157 | 
            +
                  segments: 
         | 
| 158 | 
            +
                  - 0
         | 
| 159 | 
            +
                  version: "0"
         | 
| 160 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement 
         | 
| 161 | 
            +
              none: false
         | 
| 162 | 
            +
              requirements: 
         | 
| 163 | 
            +
              - - ">="
         | 
| 164 | 
            +
                - !ruby/object:Gem::Version 
         | 
| 165 | 
            +
                  hash: 3
         | 
| 166 | 
            +
                  segments: 
         | 
| 167 | 
            +
                  - 0
         | 
| 168 | 
            +
                  version: "0"
         | 
| 169 | 
            +
            requirements: []
         | 
| 170 | 
            +
             | 
| 171 | 
            +
            rubyforge_project: 
         | 
| 172 | 
            +
            rubygems_version: 1.8.6
         | 
| 173 | 
            +
            signing_key: 
         | 
| 174 | 
            +
            specification_version: 3
         | 
| 175 | 
            +
            summary: Google Analytics Feed Access
         | 
| 176 | 
            +
            test_files: []
         | 
| 177 | 
            +
             |