toggl_cache 0.1.0

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/scripts/sync.rb ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Usage:
4
+ #
5
+ # - Syncs reports since 1 month:
6
+ # ruby scripts/sync.rb
7
+ #
8
+ # - Sync reports since the specified date (use Ruby-parseable
9
+ # format):
10
+ # ruby scripts/sync.rb 2015-01-01
11
+
12
+ # Load dependencies
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ $LOAD_PATH.unshift File.expand_path('../..', __FILE__)
17
+ require 'config/boot'
18
+
19
+ date_since = Time.parse(ARGV[0]) unless ARGV[0].nil? || ARGV[0].empty?
20
+ TogglCache.sync_reports(date_since: date_since)
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+ # Load dependencies
3
+ require "rubygems"
4
+ require "bundler/setup"
5
+ require "rspec"
6
+ require "webmock/rspec"
7
+ require "rack/test"
8
+ require "pry"
9
+
10
+ require "simplecov"
11
+ SimpleCov.start do
12
+ add_filter do |src|
13
+ # Ignoring files from the spec directory
14
+ src.filename =~ %r{/spec/}
15
+ end
16
+ end
17
+
18
+ # ENV["APP_ENV"] replaces "RACK_ENV" since we're not in
19
+ # a Rack context.
20
+ ENV["APP_ENV"] = "test"
21
+ require File.expand_path("../../config/boot", __FILE__)
22
+
23
+ Dir[File.expand_path("../support/**/*.rb", __FILE__)].each { |f| require(f) }
24
+
25
+ # Database setup, teardown and cleanup during tests
26
+ require "sequel/extensions/migration"
27
+ require "toggl_cache/data/report_repository"
28
+ client = TogglCache::Data::DB
29
+
30
+ MIGRATIONS_DIR = File.expand_path("../../config/db_migrations", __FILE__)
31
+ RSpec.configure do |config|
32
+
33
+ config.before(:all) do
34
+ Sequel::Migrator.apply(client, MIGRATIONS_DIR)
35
+ end
36
+
37
+ config.after(:each) do
38
+ TogglCache::Data::ReportRepository.delete_where("TRUE")
39
+ end
40
+
41
+ config.after(:all) do
42
+ Sequel::Migrator.apply(client, MIGRATIONS_DIR, 0)
43
+ end
44
+ end
45
+
46
+ require "toggl_cache"
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+ require "spec_helper"
3
+ require "toggl_api/reports_client"
4
+
5
+ describe TogglAPI::ReportsClient do
6
+ let(:options) { {} }
7
+ let(:expected_api_call_url) { "https://toggl_api_token:api_token@toggl.com/reports/api/v2/details?page=1&user_agent=TogglAPI" }
8
+
9
+ let(:api_response_status) { 200 }
10
+ let(:api_response_body) do
11
+ {
12
+ total_count: total_count,
13
+ data: data_page1
14
+ }.to_json
15
+ end
16
+ let(:total_count) { 0 }
17
+ let(:data_page1) do
18
+ 50.times.map { { id: 1 } } # faking 50 entries
19
+ end
20
+
21
+ before do
22
+ stub_request(:get, expected_api_call_url)
23
+ .with(headers: { "Content-Type" => "application/json" })
24
+ .to_return(status: api_response_status, body: api_response_body, headers: {})
25
+ end
26
+
27
+ describe "#fetch_reports(params)" do
28
+
29
+ context "only 1 page" do
30
+ let(:total_count) { 50 }
31
+
32
+ it "fetches the data only once" do
33
+ described_class.new.fetch_reports(options)
34
+ # It will fail if it fetches several times because
35
+ # the second request, with page=2, is not mocked.
36
+ end
37
+
38
+ it "returns the received data" do
39
+ results = described_class.new.fetch_reports(options)
40
+ expect(results.count).to eq(50)
41
+ end
42
+ end
43
+
44
+ context "several pages" do
45
+ let(:total_count) { 60 }
46
+ let(:data_page2) do
47
+ 10.times.map { { id: 2 } } # faking 10 entries
48
+ end
49
+
50
+ before do
51
+ api_response_body_page2 = {
52
+ total_count: total_count,
53
+ data: data_page2
54
+ }.to_json
55
+ stub_request(:get, expected_api_call_url.gsub('page=1', 'page=2'))
56
+ .with(headers: { "Content-Type" => "application/json" })
57
+ .to_return(status: api_response_status, body: api_response_body_page2, headers: {})
58
+ end
59
+
60
+ it "fetches the additional pages" do
61
+ results = described_class.new.fetch_reports(options)
62
+ expect(results.count).to eq(60)
63
+ end
64
+
65
+ it "returns the merged data" do
66
+ results = described_class.new.fetch_reports(options)
67
+ expect(results.inject(0) { |s, i| s + i["id"] }).to eq(50 * 1 + 10 * 2)
68
+ end
69
+ end
70
+
71
+ context "receiving non-successful response" do
72
+ let(:api_response_status) { 500 }
73
+
74
+ it "raises a TogglAPI::Error" do
75
+ expect { described_class.new.fetch_reports(options) }
76
+ .to raise_error(TogglAPI::Error)
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "toggl_cache/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "toggl_cache"
9
+ spec.version = TogglCache::VERSION
10
+ spec.authors = ["Romain Champourlier"]
11
+ spec.email = ["pro@rchampourlier.com"]
12
+ spec.summary = "Fetches reports from Toggl and caches them in a MongoDB store."
13
+ spec.homepage = "https://github.com/rchampourlier/toggl_cache"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting "allowed_push_host", or
17
+ # delete this section to allow pushing this gem to any host.
18
+ unless spec.respond_to?(:metadata)
19
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
20
+ end
21
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
22
+
23
+ spec.files = `git ls-files -z`.split("\x0")
24
+ spec.executables = []
25
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency "i18n"
29
+ spec.add_dependency "activesupport-inflector"
30
+ spec.add_dependency "pg"
31
+ spec.add_dependency "sequel"
32
+ spec.add_dependency "sequel_pg"
33
+ spec.add_dependency "httparty"
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.7"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "pry"
38
+ spec.add_development_dependency "awesome_print"
39
+ spec.add_development_dependency "dotenv"
40
+ end
metadata ADDED
@@ -0,0 +1,237 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: toggl_cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Romain Champourlier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: i18n
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport-inflector
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pg
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sequel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sequel_pg
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: httparty
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: bundler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.7'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.7'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '10.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '10.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: awesome_print
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: dotenv
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description:
168
+ email:
169
+ - pro@rchampourlier.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".codeclimate.yml"
175
+ - ".env.test"
176
+ - ".gitignore"
177
+ - ".rspec"
178
+ - ".rubocop.yml"
179
+ - ".ruby-gemset"
180
+ - ".ruby-version"
181
+ - ".travis.yml"
182
+ - CODE_OF_CONDUCT.md
183
+ - Gemfile
184
+ - Guardfile
185
+ - HISTORY.md
186
+ - LICENSE.txt
187
+ - README.md
188
+ - Rakefile
189
+ - VERSION
190
+ - bin/console
191
+ - bin/db/migrate
192
+ - bin/db/psql
193
+ - bin/db/reset
194
+ - bin/setup
195
+ - config/boot.rb
196
+ - config/db_migrations/001_create_toggl_cache_reports.rb
197
+ - docker-compose.yml
198
+ - lib/toggl_api/base_client.rb
199
+ - lib/toggl_api/client.rb
200
+ - lib/toggl_api/reports_client.rb
201
+ - lib/toggl_cache.rb
202
+ - lib/toggl_cache/data.rb
203
+ - lib/toggl_cache/data/report_repository.rb
204
+ - lib/toggl_cache/version.rb
205
+ - lib/toggl_cli.rb
206
+ - scripts/sync.rb
207
+ - spec/spec_helper.rb
208
+ - spec/unit/toggl_api/reports_client_spec.rb
209
+ - toggl_cache.gemspec
210
+ homepage: https://github.com/rchampourlier/toggl_cache
211
+ licenses:
212
+ - MIT
213
+ metadata:
214
+ allowed_push_host: https://rubygems.org
215
+ post_install_message:
216
+ rdoc_options: []
217
+ require_paths:
218
+ - lib
219
+ required_ruby_version: !ruby/object:Gem::Requirement
220
+ requirements:
221
+ - - ">="
222
+ - !ruby/object:Gem::Version
223
+ version: '0'
224
+ required_rubygems_version: !ruby/object:Gem::Requirement
225
+ requirements:
226
+ - - ">="
227
+ - !ruby/object:Gem::Version
228
+ version: '0'
229
+ requirements: []
230
+ rubyforge_project:
231
+ rubygems_version: 2.6.10
232
+ signing_key:
233
+ specification_version: 4
234
+ summary: Fetches reports from Toggl and caches them in a MongoDB store.
235
+ test_files:
236
+ - spec/spec_helper.rb
237
+ - spec/unit/toggl_api/reports_client_spec.rb