Class: ItunesConnect::Store
- Inherits:
-
Object
- Object
- ItunesConnect::Store
- Defined in:
- lib/itunes_connect/store.rb
Overview
Represents a database stored on disk.
Constant Summary collapse
- VALID_COUNT_OPTIONS =
[:from, :to, :country]
Instance Method Summary collapse
-
#add(date, country, install_count, update_count) ⇒ Object
Add a record to this instance.
-
#country_counts(opts = { }) ⇒ Object
Get summed counts by country, optionally constrained by dates and/or country codes.
-
#counts(opts = { }) ⇒ Object
Get counts optionally constrained by dates and/or country codes.
-
#initialize(file, verbose = false) ⇒ Store
constructor
Creates a new instance.
-
#verbose? ⇒ Boolean
:nodoc:.
Constructor Details
#initialize(file, verbose = false) ⇒ Store
Creates a new instance. If no database file exists at the given path a new one is created and the correct tables and indexes are added.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/itunes_connect/store.rb', line 10 def initialize(file, verbose=false) @db = SQLite3::Database.new(file) if @db.table_info("reports").empty? @db.execute("CREATE TABLE reports (id INTEGER PRIMARY KEY, " + "report_date DATE NOT NULL, country TEXT NOT NULL, " + "install_count INTEGER, " + "update_count INTEGER)") @db.execute("CREATE UNIQUE INDEX u_reports_idx ON reports " + "(report_date, country)") end @verbose = verbose end |
Instance Method Details
#add(date, country, install_count, update_count) ⇒ Object
Add a record to this instance
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/itunes_connect/store.rb', line 28 def add(date, country, install_count, update_count) ret = @db.execute("INSERT INTO reports (report_date, country, " + "install_count, update_count) VALUES (?, ?, ?, ?)", [format_date(date), country, install_count, update_count]) true rescue SQLite3::ConstraintException => e if e. =~ /columns .* are not unique/ $stdout.puts "Skipping existing row for #{country} on #{date}" if verbose? false else raise e end end |
#country_counts(opts = { }) ⇒ Object
Get summed counts by country, optionally constrained by dates and/or country codes. Available options are:
:from
-
The from date, defaults to the beginning
:to
-
The end date, defaults to now
:country
-
The country code, defaults to
nil
which means no country code restriction
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/itunes_connect/store.rb', line 93 def country_counts(opts={ }) unless (leftovers = opts.keys - VALID_COUNT_OPTIONS).empty? raise "Invalid keys: #{leftovers.join(', ')}" end params = [] clauses = [] sql = "SELECT country, SUM(install_count), SUM(update_count) FROM reports" if opts[:from] clauses << "report_date >= ?" params << format_date(opts[:from]) end if opts[:to] clauses << "report_date <= ?" params << format_date(opts[:to]) end if opts[:country] clauses << "country = ?" params << opts[:country] end sql << " WHERE " unless clauses.empty? sql << clauses.join(" AND ") unless params.empty? sql << " GROUP BY country ORDER BY country" @db.execute(sql, params).map do |row| OpenStruct.new({ :country => row[0], :install_count => row[1].to_i, :update_count => row[2].to_i }) end end |
#counts(opts = { }) ⇒ Object
Get counts optionally constrained by dates and/or country codes. Available options are:
:from
-
The from date, defaults to the beginning
:to
-
The end date, defaults to now
:country
-
The country code, defaults to
nil
which means no country code restriction
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/itunes_connect/store.rb', line 49 def counts(opts={ }) unless (leftovers = opts.keys - VALID_COUNT_OPTIONS).empty? raise "Invalid keys: #{leftovers.join(', ')}" end params = [] clauses = [] sql = "SELECT * FROM reports" if opts[:from] clauses << "report_date >= ?" params << format_date(opts[:from]) end if opts[:to] clauses << "report_date <= ?" params << format_date(opts[:to]) end if opts[:country] clauses << "country = ?" params << opts[:country] end sql << " WHERE " unless clauses.empty? sql << clauses.join(" AND ") unless params.empty? sql << " ORDER BY report_date DESC" @db.execute(sql, params).map do |row| OpenStruct.new({ :report_date => row[1] ? Date.parse(row[1]) : nil, :country => row[2], :install_count => row[3].to_i, :update_count => row[4].to_i }) end end |
#verbose? ⇒ Boolean
:nodoc:
23 24 25 |
# File 'lib/itunes_connect/store.rb', line 23 def verbose? # :nodoc: !!@verbose end |