toggl_cache 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +10 -0
- data/.env.test +7 -0
- data/.gitignore +15 -0
- data/.rspec +2 -0
- data/.rubocop.yml +14 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +15 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +26 -0
- data/Guardfile +1 -0
- data/HISTORY.md +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +50 -0
- data/Rakefile +7 -0
- data/VERSION +1 -0
- data/bin/console +14 -0
- data/bin/db/migrate +7 -0
- data/bin/db/psql +7 -0
- data/bin/db/reset +7 -0
- data/bin/setup +7 -0
- data/config/boot.rb +11 -0
- data/config/db_migrations/001_create_toggl_cache_reports.rb +29 -0
- data/docker-compose.yml +25 -0
- data/lib/toggl_api/base_client.rb +52 -0
- data/lib/toggl_api/client.rb +62 -0
- data/lib/toggl_api/reports_client.rb +81 -0
- data/lib/toggl_cache.rb +101 -0
- data/lib/toggl_cache/data.rb +10 -0
- data/lib/toggl_cache/data/report_repository.rb +96 -0
- data/lib/toggl_cache/version.rb +3 -0
- data/lib/toggl_cli.rb +198 -0
- data/scripts/sync.rb +20 -0
- data/spec/spec_helper.rb +46 -0
- data/spec/unit/toggl_api/reports_client_spec.rb +80 -0
- data/toggl_cache.gemspec +40 -0
- metadata +237 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f3e59b8333c1e639df56bc5b28487def4ed4fbce
|
4
|
+
data.tar.gz: '0218ed7d70e6f03026cddab8654b509f1e26c7e3'
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5019fcfa24a11cc789d1083aec3aa5bf42ee45daea85eb593a335fa540a25287802a34a8c4381f215b6c2a9599c5d2778c473e278adb530f3cbf1bece21bd0c4
|
7
|
+
data.tar.gz: dc4af37451d09cdea3b344c377981f53b29432c74307d73a41419679ca1c2a29c1da0129a75e9cf243b089fe81852e735d348e9cbdd7bbad90e481ca0ce79c96
|
data/.codeclimate.yml
ADDED
data/.env.test
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
Style/LineLength:
|
2
|
+
Max: 120
|
3
|
+
Style/StringLiterals:
|
4
|
+
EnforcedStyle: double_quotes
|
5
|
+
Style/EmptyLinesAroundBody:
|
6
|
+
Enabled: false
|
7
|
+
Style/EmptyLinesAroundModuleBody:
|
8
|
+
Enabled: false
|
9
|
+
Style/EmptyLinesAroundClassBody:
|
10
|
+
Enabled: false
|
11
|
+
Style/EmptyLinesAroundBlockBody:
|
12
|
+
Enabled: false
|
13
|
+
Style/AlignParameters:
|
14
|
+
EnforcedStyle: with_fixed_indentation
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
toggl_cache
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.3.3
|
data/.travis.yml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 2.3.1
|
4
|
+
before_install: gem install bundler
|
5
|
+
before_script:
|
6
|
+
- psql -c 'create database travis_ci_test;' -U postgres
|
7
|
+
after_success: bundle exec codeclimate-test-reporter
|
8
|
+
services:
|
9
|
+
- postgresql
|
10
|
+
addons:
|
11
|
+
postgresql: "9.4"
|
12
|
+
env:
|
13
|
+
global:
|
14
|
+
- APP_ENV=test
|
15
|
+
- DATABASE_URL=postgres://localhost:5432/travis_ci_test
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in toggl_cache.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group :development do
|
7
|
+
gem 'guard', require: false
|
8
|
+
gem 'guard-rspec', require: false
|
9
|
+
gem 'terminal-notifier-guard'
|
10
|
+
|
11
|
+
# For CLI
|
12
|
+
gem 'thor'
|
13
|
+
gem 'tty-pager'
|
14
|
+
gem 'tty-progressbar'
|
15
|
+
gem 'tty-spinner'
|
16
|
+
gem 'tty-table'
|
17
|
+
end
|
18
|
+
|
19
|
+
group :test do
|
20
|
+
gem 'rspec'
|
21
|
+
gem 'rack-test'
|
22
|
+
gem 'webmock'
|
23
|
+
gem 'coveralls', require: false
|
24
|
+
gem 'simplecov', require: false
|
25
|
+
gem 'simplecov-json', require: false
|
26
|
+
end
|
data/Guardfile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
./config/Guardfile
|
data/HISTORY.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Romain Champourlier
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# TogglCache
|
2
|
+
|
3
|
+
Fetches reports data from Toggl API and caches them in a MongoDB store.
|
4
|
+
|
5
|
+
This allows you to build applications performing complex operations on a large number of Toggl content without the Toggl API latency. You may also use it to backup your precious data!
|
6
|
+
|
7
|
+
[![Build Status](https://travis-ci.org/rchampourlier/toggl_cache.svg)](https://travis-ci.org/rchampourlier/toggl_cache)
|
8
|
+
[![Code Climate](https://codeclimate.com/github/rchampourlier/toggl_cache/badges/gpa.svg)](https://codeclimate.com/github/rchampourlier/toggl_cache)
|
9
|
+
[![Coverage Status](https://coveralls.io/repos/github/rchampourlier/toggl_cache/badge.svg?branch=master)](https://coveralls.io/github/rchampourlier/toggl_cache?branch=master)
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'toggl_cache'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install toggl_cache
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
Inside of `bin/console`:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
logger = Logger.new(STDOUT)
|
33
|
+
logger.level = Logger::DEBUG
|
34
|
+
client = TogglCache::Client.new(
|
35
|
+
api_token: 'SOME-API-TOKEN',
|
36
|
+
user_agent: 'MyCustomTogglCache',
|
37
|
+
logger: logger
|
38
|
+
)
|
39
|
+
TogglCache.sync_reports(client, 'TOGGL-WORKSPACE-ID')
|
40
|
+
```
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it ( https://github.com/rchampourlier/toggl_cache/fork )
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create a new Pull Request
|
49
|
+
|
50
|
+
Check the [code of conduct](CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Load dependencies
|
4
|
+
require "rubygems"
|
5
|
+
require "bundler/setup"
|
6
|
+
|
7
|
+
$LOAD_PATH.unshift File.expand_path("../..", __FILE__)
|
8
|
+
require "config/boot"
|
9
|
+
|
10
|
+
require "toggl_api/client"
|
11
|
+
require "toggl_api/reports_client"
|
12
|
+
|
13
|
+
require "pry"
|
14
|
+
Pry.start
|
data/bin/db/migrate
ADDED
data/bin/db/psql
ADDED
data/bin/db/reset
ADDED
data/bin/setup
ADDED
data/config/boot.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
env = ENV["APP_ENV"] || "development"
|
3
|
+
if env == "development" || env == "test"
|
4
|
+
require "dotenv"
|
5
|
+
Dotenv.load(".env.#{env}")
|
6
|
+
end
|
7
|
+
|
8
|
+
root_dir = File.expand_path("../..", __FILE__)
|
9
|
+
$LOAD_PATH.unshift root_dir
|
10
|
+
$LOAD_PATH.unshift File.join(root_dir, "lib")
|
11
|
+
require "toggl_cache"
|
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
Sequel.migration do
|
5
|
+
up do
|
6
|
+
create_table :toggl_cache_reports do
|
7
|
+
String :description
|
8
|
+
Integer :duration
|
9
|
+
DateTime :end
|
10
|
+
Integer :pid
|
11
|
+
String :project
|
12
|
+
String :uid
|
13
|
+
String :user
|
14
|
+
DateTime :start
|
15
|
+
String :task
|
16
|
+
Integer :tid
|
17
|
+
String :id, unique: true
|
18
|
+
DateTime :toggl_updated
|
19
|
+
|
20
|
+
# Timestamps
|
21
|
+
DateTime :created_at
|
22
|
+
DateTime :updated_at
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
down do
|
27
|
+
drop_table(:toggl_cache_reports)
|
28
|
+
end
|
29
|
+
end
|
data/docker-compose.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
version: '2'
|
2
|
+
|
3
|
+
services:
|
4
|
+
pg-dev:
|
5
|
+
env_file:
|
6
|
+
- .env.development
|
7
|
+
image: postgres:9.4
|
8
|
+
ports:
|
9
|
+
- "5432:5432"
|
10
|
+
volumes:
|
11
|
+
- pg-dev-data:/var/lib/postgresql/data
|
12
|
+
pg-test:
|
13
|
+
env_file:
|
14
|
+
- .env.test
|
15
|
+
image: postgres:9.4
|
16
|
+
ports:
|
17
|
+
- "5433:5432"
|
18
|
+
volumes:
|
19
|
+
- pg-test-data:/var/lib/postgresql/data
|
20
|
+
|
21
|
+
volumes:
|
22
|
+
pg-dev-data:
|
23
|
+
driver: local
|
24
|
+
pg-test-data:
|
25
|
+
driver: local
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TogglAPI
|
4
|
+
DEFAULT_WORKSPACE_ID = ENV["TOGGL_WORKSPACE_ID"]
|
5
|
+
Error = Class.new(StandardError)
|
6
|
+
|
7
|
+
# Superclass for API clients.
|
8
|
+
class BaseClient
|
9
|
+
DEFAULT_USER_AGENT = "TogglAPI"
|
10
|
+
attr_reader :api_token, :logger
|
11
|
+
|
12
|
+
# Returns a new instance of the client, configured with
|
13
|
+
# the specified parameters.
|
14
|
+
#
|
15
|
+
# @param api_token [String] Toggl API token, for authentication
|
16
|
+
# @param user_agent [String] defaults to "TogglCache"
|
17
|
+
# @param logger [Logger] used to log messages (in particular fetch
|
18
|
+
# events). Defaults to nil.
|
19
|
+
#
|
20
|
+
def initialize(api_token: default_api_token, user_agent: default_user_agent, logger: nil)
|
21
|
+
raise "Missing api_token" if api_token.nil? || api_token.empty?
|
22
|
+
@api_token = api_token
|
23
|
+
@user_agent = user_agent
|
24
|
+
@logger = logger
|
25
|
+
end
|
26
|
+
|
27
|
+
def default_api_token
|
28
|
+
ENV["TOGGL_API_TOKEN"]
|
29
|
+
end
|
30
|
+
|
31
|
+
def default_user_agent
|
32
|
+
DEFAULT_USER_AGENT
|
33
|
+
end
|
34
|
+
|
35
|
+
def credentials
|
36
|
+
{
|
37
|
+
username: @api_token,
|
38
|
+
password: "api_token"
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
def default_headers
|
43
|
+
{
|
44
|
+
"Content-Type" => "application/json"
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
48
|
+
def default_workspace_id
|
49
|
+
DEFAULT_WORKSPACE_ID
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "uri"
|
3
|
+
require "httparty"
|
4
|
+
require "toggl_api/base_client"
|
5
|
+
|
6
|
+
module TogglAPI
|
7
|
+
|
8
|
+
# The Toggl API Client
|
9
|
+
class Client < BaseClient
|
10
|
+
API_URL = "https://www.toggl.com/api/v8"
|
11
|
+
|
12
|
+
# Update the Toggl time entry with the specified `ìd` using
|
13
|
+
# the provided `time_entry` data.
|
14
|
+
def update_time_entry(id:, time_entry:)
|
15
|
+
perform_request(
|
16
|
+
verb: :put,
|
17
|
+
url: "#{API_URL}/time_entries/#{id}",
|
18
|
+
body: { time_entry: time_entry }
|
19
|
+
)["data"]
|
20
|
+
end
|
21
|
+
|
22
|
+
# Get the projects from the specified workspace.
|
23
|
+
def get_workspace_projects(workspace_id: default_workspace_id, active: "true")
|
24
|
+
perform_request(
|
25
|
+
url: "#{API_URL}/workspaces/#{workspace_id}/projects",
|
26
|
+
query: { active: active }
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
def get_project_tasks(project_id:)
|
31
|
+
perform_request(
|
32
|
+
url: "#{API_URL}/projects/#{project_id}/tasks"
|
33
|
+
) || []
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def perform_request(verb: :get, url:, headers: default_headers, query: {}, body: {})
|
39
|
+
response = HTTParty.send(
|
40
|
+
verb,
|
41
|
+
url,
|
42
|
+
headers: headers,
|
43
|
+
query: { user_agent: default_user_agent }.merge(query),
|
44
|
+
body: body.to_json,
|
45
|
+
basic_auth: credentials
|
46
|
+
)
|
47
|
+
|
48
|
+
begin
|
49
|
+
if response.code == 200 || response.code == 201
|
50
|
+
return nil if response.body == 'null'
|
51
|
+
JSON.parse(response.body)
|
52
|
+
else
|
53
|
+
logger.error "Error (response code #{response.code}, content '#{response.body.strip}')"
|
54
|
+
nil
|
55
|
+
end
|
56
|
+
# rescue => e
|
57
|
+
# TODO: fix this too large rescue
|
58
|
+
# logger.error "Exception (#{e})"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|