Coder Social home page Coder Social logo

togglv8's Introduction

Toggl API v8

Gem Version Build Status Coverage Status Code Climate

Toggl is a time tracking tool.

togglv8 is a Ruby Wrapper for Toggl API v8. It is designed to mirror the Toggl API as closely as possible.

togglv8 supports both Toggl API and Reports API

Change Log

See CHANGELOG for a summary of notable changes in each version.

Installation

Add this line to your application's Gemfile:

gem 'togglv8'

And then execute:

$ bundle

Or install it yourself as:

$ gem install togglv8

Initialization

TogglV8::API

TogglV8::API communicates with Toggl API v8 and can be initialized in one of three ways.

TogglV8::API.new                      # reads API token from file ~/.toggl
TogglV8::API.new(api_token)           # explicit API token
TogglV8::API.new(email, password)     # email & password

TogglV8::ReportsV2

TogglV8::ReportsV2 communicates with Toggl Reports API v2 and can be initialized in one of three ways. Toggl.com requires authentication with an API token for Reports API v2.

TogglV8::ReportsV2.new                              # reads API token from file ~/.toggl
TogglV8::ReportsV2.new(toggl_api_file: toggl_file)  # reads API token from toggl_file
TogglV8::ReportsV2.new(api_token: api_token)        # explicit API token

Note: workspace_id must be set in order to generate reports.

toggl = TogglV8::API.new
reports = TogglV8::ReportsV2.new
reports.workspace_id = toggl.workspaces.first['id']

Usage

This short example shows one way to create a time entry for the first workspace of the user identified by <API_TOKEN>. It then generates various reports containing that time entry.

require 'togglv8'
require 'json'

toggl_api    = TogglV8::API.new(<API_TOKEN>)
user         = toggl_api.me(all=true)
workspaces   = toggl_api.my_workspaces(user)
workspace_id = workspaces.first['id']
time_entry   = toggl_api.create_time_entry({
  'description' => "My awesome workspace time entry",
  'wid' => workspace_id,
  'duration' => 1200,
  'start' => toggl_api.iso8601((Time.now - 3600).to_datetime),
  'created_with' => "My awesome Ruby application"
})

begin
  reports               = TogglV8::ReportsV2.new(api_token: <API_TOKEN>)
  begin
    reports.summary
  rescue Exception => e
    puts e.message      # workspace_id is required
  end
  reports.workspace_id  = workspace_id
  summary               = reports.summary
  puts "Generating summary JSON..."
  puts JSON.pretty_generate(summary)
  puts "Generating summary PDF..."
  reports.write_summary('toggl_summary.pdf')
  puts "Generating weekly CSV..."
  reports.write_weekly('toggl_weekly.csv')
  puts "Generating details XLS..."
  reports.write_details('toggl_details.xls')
  # Note: toggl.com does not generate Weekly XLS report (as of 2016-07-24)
ensure
  toggl_api.delete_time_entry(time_entry['id'])
end

See specs for more examples.

Note: Requests are rate-limited. The togglv8 gem will handle a 429 response by pausing for 1 second and trying again, for up to 3 attempts. See Toggl API docs:

For rate limiting we have implemented a Leaky bucket. When a limit has been hit the request will get a HTTP 429 response and it's the task of the client to sleep/wait until bucket is empty. Limits will and can change during time, but a safe window will be 1 request per second. Limiting is applied per api token per IP, meaning two users from the same IP will get their rate allocated separately.

Debugging

The TogglV8::API#debug method determines if debug output is printed to STDOUT. This code snippet demonstrates the debug output.

require 'togglv8'

toggl = TogglV8::API.new

toggl.debug(true)  # or simply toggl.debug
user1 = toggl.me
puts "user: #{user1['fullname']}, debug: true"

puts '-'*80

toggl.debug(false)
user2 = toggl.me
puts "user: #{user2['fullname']}, debug: false"

Documentation

Run rdoc to generate documentation. Open doc/index.html in your browser.

Also available on DocumentUp

Acknowledgements

Contributing

  1. Fork it ( https://github.com/kanet77/togglv8/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Pull Requests that include tests are much more likely to be accepted and merged quickly.

License

Copyright (c) 2013-2016 Tom Kane. Released under the MIT License. See LICENSE.txt for details.

togglv8's People

Contributors

dylanlewis89 avatar kanet77 avatar limitusus avatar loadkpi avatar shaungrey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

togglv8's Issues

Catch up with ddiatmb's fork

You've had a pending pull request for the reports API for years. ddiatmb's fork is the furthest ahead.

Is there some other up to date Ruby Toggl API wrapper everyone else knows about?

Awesome_print dependency

Hi,

it looks like togglv8 depends on awesome_print gem, however awesome_print is a development dependency. Requiring togglv8 without awesome_print installed causes an error.

Here is irb output trying to use togglv8 w/o awesome_print installed.

workspace|⇒ gem install togglv8
Successfully installed togglv8-1.2.1
1 gem installed
workspace|⇒ irb
irb(main):001:0> require 'togglv8'
LoadError: cannot load such file -- awesome_print
	from /Users/kamil/.rbenv/versions/2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /Users/kamil/.rbenv/versions/2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /Users/kamil/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/togglv8-1.2.1/lib/logging.rb:3:in `<top (required)>'
	from /Users/kamil/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/togglv8-1.2.1/lib/togglv8/connection.rb:4:in `require_relative'
	from /Users/kamil/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/togglv8-1.2.1/lib/togglv8/connection.rb:4:in `<top (required)>'
	from /Users/kamil/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/togglv8-1.2.1/lib/togglv8.rb:3:in `require_relative'
	from /Users/kamil/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/togglv8-1.2.1/lib/togglv8.rb:3:in `<top (required)>'
	from /Users/kamil/.rbenv/versions/2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:127:in `require'
	from /Users/kamil/.rbenv/versions/2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:127:in `rescue in require'
	from /Users/kamil/.rbenv/versions/2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:40:in `require'
	from (irb):1
	from /Users/kamil/.rbenv/versions/2.3.0/bin/irb:11:in `<main>'

Related files:
https://github.com/kanet77/togglv8/blob/master/lib/logging.rb#L3
https://github.com/kanet77/togglv8/blob/master/togglv8.gemspec#L26

301 Redirect moved permanently

I started getting this error this morning:

/Library/Ruby/Gems/2.3.0/gems/togglv8-1.2.1/lib/togglv8/connection.rb:51:in `_call_api': HTTP Status: 301 (RuntimeError)
	from /Library/Ruby/Gems/2.3.0/gems/togglv8-1.2.1/lib/togglv8/connection.rb:61:in `get'
	from /Library/Ruby/Gems/2.3.0/gems/togglv8-1.2.1/lib/togglv8/users.rb:26:in `me'
	from /Users/me/bin/app.rb:14:in `<main>'

It happens on a call to toggl.me - https://toggl.com/api/v8/me?with_related_data=true

The API works for me using a different tool, so I rule out it being the network I am in. I also switched the token. Same issue.

Remove tasks from project

I have a problem with deleting tasks from a given project, especially here https://github.com/kanet77/togglv8/blob/master/lib/togglv8/tasks.rb#L36-L50 is something which has not got any sense. In a first function, you pass a task_id and call join on it and in a second function you do exactly the same thing but you change name of variable for task_ids then gem received 404 error from Toggl API and there is raised

NoMemoryError at /toggl/sync
Too deeply nested.

Here is a log:

D, [2015-12-09T12:34:46.803273 #96339] DEBUG -- : [httplog] Connecting: www.toggl.com:443
D, [2015-12-09T12:34:47.332799 #96339] DEBUG -- : [httplog] Sending: DELETE http://www.toggl.com:443/api/v8/tasks/12636535
D, [2015-12-09T12:34:47.333476 #96339] DEBUG -- : [httplog] Data:
D, [2015-12-09T12:34:47.507476 #96339] DEBUG -- : [httplog] Status: 404
D, [2015-12-09T12:34:47.508035 #96339] DEBUG -- : [httplog] Benchmark: 0.1732813100097701 seconds
D, [2015-12-09T12:34:47.508595 #96339] DEBUG -- : [httplog] Response:

Remove logger dependency

Logger's version v1.2.8 was yanked which broke some of the projects using this gem. Could you consider removing this dependency as logger is a part of ruby standard library?

More context on the issue here: nahi/logger#3

NoMethod Error from Faraday http basic

Faraday has changed their API regarding HTTP basic:

We're using: faraday.basic_auth username, password
The new API is: faraday.request :authorization, :basic, username, password

Evaluating body of response

I'm looking at the eval of the post response.
https://github.com/kanet77/togglv8/blob/master/togglV8.rb#L373

It appears that if the response does not have a 200 code, that we are running whatever comes back.

In the specific case where we create a task with a name that already exists, the script fails with "undefined local variable or method `taken' ", because 'taken' is the last word in the body of the response. In general, it looks to me like we're blindly running whatever script might come back from the server.

What is the intent of this eval?

In the specific case I mentioned, I would consider this a bug. In general, this seems like a serious security issue.

Last time entry in project

Hi there.

I'm trying to retrieve the latest time entry from a specific project.

The only way I think I could do it is with the User method my_time_entries but this method doesn't retrieve all User's time entries. Currently the method is retrieving my last 10 time entries, and none of them are from the project I want to use.

There is another method or another way to retrieve the last entry from a specific project??

I hope I've explained it well. Thanks :)

Detailed Reports does not return all data

Currently, it looks like a query using detailed reports in toggl V8 only returns 50 records. You complete the query by doing queries using the 'page' parameter after looking at the total_count returned by the first request.

But in the ruby bindings, the get method in togglv8::Connection ignores everything but the 'data' array, so it's impossible to know if there is more data to be returned. The detailed report in the ruby bindings should automatically iterate over total_count and then return all the data.

Improve initialization

Background

Use of TogglV8::API#initialize() is convoluted. It supports three authentication alternatives:

toggl = TogglV8::API.new() # with API token in ~/.toggl
toggl = TogglV8::API.new(api_token)
toggl = TogglV8::API.new(username, password)

Problems

The source code is not clear on how it can be used.

def initialize(username=nil, password=API_TOKEN, opts={})
  1. Passing an API token as username with no corresponding password is unexpected.
  2. The no-argument option is perhaps too magical.
  3. It is only possible to pass a value for opts with the username/password authentication (or by explicitly passing "api_token" as the second parameter.

Solution

  • Separate the three types of authentication and require one to be called explicitly.
  • Provide the credentials and create the connection after new(), not during.
  • Consider allowing other options in opts (e.g. enable debugging) or remove it entirely and replace with explicit method calls

Broken method get_time_entries({start_date: @now - 1, end_date: @now + 1})

Methods, which described in specs seems to be broken (Outdated API calls?)

https://github.com/kanet77/togglv8/blob/master/spec/lib/togglv8/time_entries_spec.rb

require 'togglv8'
require 'json'

config = JSON.parse(File.read('config.json'))

toggl    = TogglV8::API.new(config['toggl_api_token'])

@now = DateTime.now
r = toggl.get_time_entries({start_date: @now - 1, end_date: @now + 1})
r.each do |e|
  p e
  p '------'
end

Gives 400 response from server:

/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/togglv8-1.0.2/lib/togglv8.rb:93:in `_call_api': HTTP Status: 400 (RuntimeError)
    from /Users/garmoshka-mo/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/togglv8-1.0.2/lib/togglv8.rb:100:in `get'
    from /Users/garmoshka-mo/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/togglv8-1.0.2/lib/togglv8/time_entries.rb:82:in `get_time_entries'
    from import.rb:12:in `<main>'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.