rescuetime 0.1.0 → 0.2.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.
- checksums.yaml +7 -7
- data/.gitignore +0 -1
- data/.travis.yml +1 -0
- data/CHANGELOG.md +13 -0
- data/CONTRIBUTING.md +48 -0
- data/Gemfile +5 -0
- data/LICENSE +1 -1
- data/README.md +65 -20
- data/bin/console +0 -0
- data/bin/setup +0 -0
- data/lib/rescuetime/activities.rb +160 -10
- data/lib/rescuetime/api.rb +6 -1
- data/lib/rescuetime/client.rb +99 -1
- data/lib/rescuetime/errors.rb +10 -0
- data/lib/rescuetime/version.rb +3 -1
- data/lib/rescuetime.rb +4 -1
- data/rescuetime.gemspec +8 -8
- metadata +120 -80
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
5
|
-
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 26e48ef7046be4852886e8a2f7854e4b328002d3
|
4
|
+
data.tar.gz: 1e7201de9e2011839b55755a52e0e0f3224cb710
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4ee47235b7780e6bbd9e54673722c9a6e44eaadc1eddc2621002943641648bf2fc275deca712c5696435e40025f2e4755a89301c0d07d3b8d47ef08b885f9f4b
|
7
|
+
data.tar.gz: 59127d45d97eb47d2ff464410835d724e64be87867f9500e0a847abb40187432369dbe3c6aa5c56dd8d6e05fa00e18fd92ed68afb2b138de234bda2ae5c908a6
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## v0.1.0 (April 15, 2015)
|
4
|
+
|
5
|
+
Initial gem release
|
6
|
+
|
7
|
+
### Features
|
8
|
+
|
9
|
+
* Rescuetime has a version number
|
10
|
+
* `Rescuetime::Client` exists
|
11
|
+
* `Rescuetime::Client#api_key?` returns existence of api key
|
12
|
+
* `Rescuetime::Client#api_key=` overwrites api key
|
13
|
+
* `Rescuetime::Client#activities` returns list of activities
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
The contributing guide includes the following sections:
|
4
|
+
|
5
|
+
* [Getting Started](#getting-started)
|
6
|
+
* [Standards](#standards)
|
7
|
+
* [Questions](#questions)
|
8
|
+
|
9
|
+
## Getting Started
|
10
|
+
|
11
|
+
Here are the basic steps for contributing to the codebase:
|
12
|
+
|
13
|
+
1. Fork it ( https://github.com/leesharma/rescuetime/fork )
|
14
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
15
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
16
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
17
|
+
5. Create a new Pull Request
|
18
|
+
|
19
|
+
You will need to install [bundler](http://gembundler.com/) and use it to install all the development dependencies:
|
20
|
+
|
21
|
+
```console
|
22
|
+
gem install bundler
|
23
|
+
bundle install
|
24
|
+
```
|
25
|
+
|
26
|
+
You should be able to run the test specs now:
|
27
|
+
|
28
|
+
```console
|
29
|
+
bundle exec rake
|
30
|
+
```
|
31
|
+
|
32
|
+
Some great ways to contribute include:
|
33
|
+
- fixing [bugs](https://github.com/leesharma/rescuetime/issues?q=is%3Aopen+is%3Aissue+-label%3A%22in+progress%22+label%3Abug)
|
34
|
+
- adding [features listed in our current milestone](https://github.com/leesharma/rescuetime/issues?q=is%3Aopen+is%3Aissue+-label%3A%22in+progress%22) (filter by milestone)
|
35
|
+
- adding/improving documentation, comments, etc.
|
36
|
+
- refactoring existing code
|
37
|
+
|
38
|
+
Check our [issue tracker](https://github.com/leesharma/rescuetime/issues) for more ideas.
|
39
|
+
|
40
|
+
## Standards
|
41
|
+
|
42
|
+
rescuetime uses RSpec for testing along with VCR and WebMock for mocking HTTP responses. Any pull request that includes changes to the code (ie. everything but comments and documentation) requires complete test coverage.
|
43
|
+
|
44
|
+
Be careful not to commit sensitive information (API keys, OAuth credentials, etc.) to public repositories!
|
45
|
+
|
46
|
+
## Questions
|
47
|
+
|
48
|
+
Have any questions? Feel free to send me (@leesharma) a message!
|
data/Gemfile
CHANGED
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,21 +1,35 @@
|
|
1
|
-
rescuetime
|
1
|
+
rescuetime
|
2
2
|
==========
|
3
3
|
|
4
|
-
[](https://codeclimate.com/github/leesharma/rescuetime)
|
5
4
|
[](https://codeclimate.com/github/leesharma/rescuetime)
|
5
|
+
[](https://codeclimate.com/github/leesharma/rescuetime)
|
6
6
|
[](https://travis-ci.org/leesharma/rescuetime)
|
7
7
|
[](https://gemnasium.com/leesharma/rescuetime)
|
8
|
-
[](http://opensource.org/licenses/MIT)
|
9
8
|
|
10
|
-
|
9
|
+
[](https://rubygems.org/gems/rescuetime)
|
10
|
+
[](https://github.com/leesharma/rescuetime/releases/)
|
11
|
+
[](http://opensource.org/licenses/MIT)
|
11
12
|
|
12
13
|
A Ruby interface to the RescueTime APIs. Rescuetime provides a simple DSL for interacting
|
13
|
-
with your personal or team RescueTime data.
|
14
|
-
|
15
|
-
Currently, this gem only supports the Data Analytics API with API key authorization.
|
14
|
+
with your personal or team RescueTime data. Currently, this gem only supports the Data Analytics API with API key authorization.
|
16
15
|
|
17
16
|
For more information about RescueTime, visit [the RescueTime homepage](https://www.rescuetime.com).
|
18
17
|
|
18
|
+
#### README Navigation
|
19
|
+
|
20
|
+
* [Installation](#installation)
|
21
|
+
* [Usage](#usage)
|
22
|
+
* [Prerequisites](#prerequisites)
|
23
|
+
* [Getting Started](#getting-started)
|
24
|
+
* [Defaults](#defaults)
|
25
|
+
* [Rescuetime Exceptions](#rescuetime-exceptions)
|
26
|
+
* [Development](https://github.com/leesharma/rescuetime/wiki/Development) ([section](#development))
|
27
|
+
* [Contributing](CONTRIBUTING.md) ([section](#contributing))
|
28
|
+
|
29
|
+
#### Useful Links
|
30
|
+
* [RDoc](http://www.rubydoc.info/gems/rescuetime)
|
31
|
+
* [Wiki](https://github.com/leesharma/rescuetime/wiki)
|
32
|
+
|
19
33
|
## Installation
|
20
34
|
|
21
35
|
Add this line to your application's Gemfile:
|
@@ -34,31 +48,62 @@ Or install it yourself as:
|
|
34
48
|
|
35
49
|
## Usage
|
36
50
|
|
37
|
-
###
|
51
|
+
### Prerequisites
|
52
|
+
|
53
|
+
Ensure that you are using a [supported ruby version](https://github.com/leesharma/rescuetime/wiki/Supported-Rubies) for your project.
|
38
54
|
|
39
|
-
In order to use access your RescueTime data, you will need an API key. If you do not already have a key,
|
55
|
+
In order to use access your RescueTime data, you will need an API key. If you do not already have a key, visit the [API key management page](https://www.rescuetime.com/anapi/manage).
|
40
56
|
|
41
|
-
|
57
|
+
### Getting Started
|
58
|
+
|
59
|
+
Using the rescuetime gem is simple. Here is some example code using the rescuetime gem (a full feature list can be found [here](https://github.com/leesharma/rescuetime/wiki#full-specs):
|
42
60
|
|
43
61
|
```ruby
|
44
62
|
require 'rescuetime'
|
45
63
|
|
46
64
|
@client = Rescuetime::Client.new(api_key: <YOUR_API_KEY>)
|
65
|
+
@client.api_key? #=> true
|
66
|
+
@client.valid_credentials? #=> true
|
67
|
+
|
68
|
+
@client.activities # Returns a list of activities, ordered by "rank"
|
69
|
+
@client.productivity # Returns a productivity report
|
70
|
+
@client.efficiency # Returns an efficiency report, ordered by "time"
|
47
71
|
|
48
|
-
@client.activities
|
49
|
-
# =>
|
72
|
+
@client.activities.class # => Array
|
73
|
+
@client.activities[0].class # => Hash
|
74
|
+
|
75
|
+
@client.efficiency( from: '2015-03-20', # returns weekly efficiency report between March 20th and
|
76
|
+
to: '2015-04-20' , # April 20th of 2015 by member in csv format
|
77
|
+
interval: 'week',
|
78
|
+
format: 'csv' )
|
50
79
|
```
|
51
80
|
|
52
|
-
|
81
|
+
For more details, please see [official gem documentation](http://www.rubydoc.info/gems/rescuetime/0.1.0) or [read the wiki](https://github.com/leesharma/rescuetime/wiki).
|
82
|
+
|
83
|
+
### Defaults
|
84
|
+
|
85
|
+
The `Rescuetime::Client#activities` action has the following defaults:
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
|
89
|
+
{ by: 'rank'
|
90
|
+
time_interval: 'hour'
|
91
|
+
date: <TODAY>
|
92
|
+
detail: 'activity' }
|
53
93
|
|
54
|
-
|
94
|
+
```
|
95
|
+
|
96
|
+
### Rescuetime Exceptions
|
97
|
+
|
98
|
+
There are a number of exceptions that extend from the custom Rescuetime::Error class:
|
99
|
+
|
100
|
+
* **Rescuetime::MissingCredentials** is raised when a request is attempted by a client with no credentials. Try setting credentials with `@client.api_key=<YOUR_API_KEY>`.
|
101
|
+
* **Rescuetime::InvalidCredentials** is raised when a request is attempted by a client with invalid credentials. Double-check your API key and fix your client with `@client.api_key=<VALID_API_KEY>`.
|
102
|
+
|
103
|
+
## Development
|
55
104
|
|
56
|
-
|
105
|
+
See the [development page](https://github.com/leesharma/rescuetime/wiki/Development) of the wiki.
|
57
106
|
|
58
107
|
## Contributing
|
59
108
|
|
60
|
-
|
61
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
62
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
63
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
64
|
-
5. Create a new Pull Request
|
109
|
+
See the [contributing page](CONTRIBUTING.md).
|
data/bin/console
CHANGED
File without changes
|
data/bin/setup
CHANGED
File without changes
|
@@ -1,26 +1,176 @@
|
|
1
1
|
module Rescuetime
|
2
|
+
# The Rescuetime::Activities module contains client methods relating to user
|
3
|
+
# activities on the /data endpoint of the Data Analytics API.
|
4
|
+
#
|
5
|
+
# @since v0.1.0
|
2
6
|
module Activities
|
3
|
-
|
7
|
+
# Base URL for RescueTime Data Analytics API endpoint
|
8
|
+
# @since v0.1.0
|
4
9
|
BASE_URL = 'https://www.rescuetime.com/anapi/data'
|
10
|
+
# Map of numeric productivity levels to meaning
|
11
|
+
# @since v0.2.0
|
12
|
+
PRODUCTIVITY_LEVELS = { -2 => 'Very Unproductive',
|
13
|
+
-1 => 'Unproductive',
|
14
|
+
0 => 'Neutral',
|
15
|
+
1 => 'Productive',
|
16
|
+
2 => 'Very Productive' }
|
17
|
+
|
5
18
|
# Returns array of all activities.
|
6
19
|
#
|
20
|
+
# @example Basic behavior
|
21
|
+
# @activities = @client.activities
|
22
|
+
# @activities.class # => Array
|
23
|
+
# @acitvities[0].class # => Hash
|
24
|
+
#
|
25
|
+
# @activities[0]
|
26
|
+
# # => {
|
27
|
+
# # :rank=>1,
|
28
|
+
# # :time_spent_seconds=>5307,
|
29
|
+
# # :number_of_people=>1,
|
30
|
+
# # :activity=>"github.com",
|
31
|
+
# # :category=>"General Software Development",
|
32
|
+
# # :productivity=>2
|
33
|
+
# # }
|
34
|
+
#
|
35
|
+
# @example Set level of detail
|
36
|
+
# @client.activities(detail: 'overview')[0]
|
37
|
+
# # => { :rank=>1, :time_spent_seconds=>13140, :number_of_people=>1, :category=>'Software Development' }
|
38
|
+
#
|
39
|
+
# @client.activities(detail: 'category')[0]
|
40
|
+
# # => { :rank=>1, :time_spent_seconds=>5835, :number_of_people=>1, :category=>'Editing and IDEs' }
|
41
|
+
#
|
42
|
+
# @client.activities(detail: 'activity')[0]
|
43
|
+
# # => { :rank=>1,
|
44
|
+
# # :time_spent_seconds=>5835,
|
45
|
+
# # :number_of_people=>1,
|
46
|
+
# # :category=>'Editing and IDEs',
|
47
|
+
# # :activity=>'RubyMine',
|
48
|
+
# # :productivity=>2 }
|
49
|
+
#
|
50
|
+
# @client.activities(detail: 'productivity') # Equivalent to @client.productivity
|
51
|
+
# @client.activities(detail: 'efficiency', by: 'time') # Equivalent to @client.efficiency
|
52
|
+
#
|
53
|
+
# @example Set perspective
|
54
|
+
# @client.activities(by: 'rank') # Returns activities by rank (time spent per activity)
|
55
|
+
# @client.activities(by: 'interval') # Returns activities chronologically
|
56
|
+
# @client.activities(by: 'member') # Returns activities grouped by member
|
57
|
+
#
|
58
|
+
# @example Set date range (note: no time constraints are allowed)
|
59
|
+
# @client.activities(date: '2014-05-06') # Returns report for May 6, 2014
|
60
|
+
# @client.activities(from: '2014-05-06', to: '2014-06-06') # Returns report for May 6 to June 6, 2014
|
61
|
+
# @client.activities(from: '2015-04-10') # Returns report for April 10, 2015 to today
|
62
|
+
#
|
63
|
+
# @example Set time interval
|
64
|
+
# @client.efficiency( from: '2015-01-01', # Returns an efficiency report by
|
65
|
+
# to: Time.now, # week from the Jan 1, 2015 through
|
66
|
+
# interval: 'week') # today.
|
67
|
+
#
|
68
|
+
# @example Format
|
69
|
+
# @client.activities # Returns array of hashes
|
70
|
+
# @client.activities format: 'csv' # Returns Mime::CSV
|
71
|
+
#
|
72
|
+
# @param [Hash] options Query parameters to be passed to RescueTime
|
73
|
+
# @option options [String] :detail
|
74
|
+
# Restricts the level of detail of report returned
|
75
|
+
# 1. 'overview': sums statistics for all activities into their top level category
|
76
|
+
# 2. 'category': sums statistics for all activities into their sub category
|
77
|
+
# 3. 'activity' (default): sums statistics for individual applications / web sites / activities
|
78
|
+
# 4. 'productivity': productivity calculation (@see #productivity)
|
79
|
+
# 5. 'efficiency': efficiency calculation (not applicable in "rank" perspective, @see #efficiency)
|
80
|
+
# @option options [String] :by
|
81
|
+
# Lets you set the perspective of your report
|
82
|
+
# 1. 'rank' (default): returns a ranked report of activities by total time spent
|
83
|
+
# 2. 'time': returns a chronological report of activities
|
84
|
+
# 3. 'member': returns an activity report grouped by member
|
85
|
+
# @option options [String] :date
|
86
|
+
# Lets you set a single date for your report in a 'YYYY-MM-DD' format. Not
|
87
|
+
# valid with :to or :from. Cannot be a future date.
|
88
|
+
# @option options [String] :from
|
89
|
+
# Lets you set a start date for your report in a 'YYYY-MM-DD' format. Valid
|
90
|
+
# with the :to option or alone (:to defaults to current date). Cannot be a
|
91
|
+
# future date or after the :to date (if supplied).
|
92
|
+
# @option options [String] :to
|
93
|
+
# Lets you set an end date for your report in a 'YYYY-MM-DD' format. If a
|
94
|
+
# :to is supplied, :from must be supplied as well. Cannot be a future date or
|
95
|
+
# before the :from date.
|
96
|
+
# @option options [String] :interval
|
97
|
+
# Lets you set the time interval for your report (ie. client.efficiency(interval:'day')
|
98
|
+
# returns the efficiency report by day.) Possible values include:
|
99
|
+
# 1. 'minute': returns data in 5-minute increments
|
100
|
+
# 2. 'hour' (default): returns data in 1-hour increments
|
101
|
+
# 3. 'day': returns data in 1-day increments
|
102
|
+
# 4. 'week': returns data in 1-week increments
|
103
|
+
# 5. 'month': returns data in 1-month increments
|
104
|
+
# @option options [String] :format
|
105
|
+
# Lets you specify a return type for your report
|
106
|
+
# 1. default: returns an array of hashes with symbolized keys
|
107
|
+
# 2. 'csv': returns a Mime::CSV object
|
108
|
+
#
|
109
|
+
# @return [Array<Hash>]
|
110
|
+
#
|
111
|
+
# @raise [Rescuetime::MissingCredentials] if the Rescuetime::Client has no set api key
|
112
|
+
# @raise [Rescuetime::InvalidCredentials] if the provided api key is rejected by RescueTime
|
113
|
+
# @since v0.1.0
|
114
|
+
def activities(options={})
|
115
|
+
response = self.get BASE_URL, options
|
116
|
+
|
117
|
+
case options[:format]
|
118
|
+
when 'csv' then CSV.new(response.body, headers: true)
|
119
|
+
else array_of_hashes_from_csv(response.body)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
# Returns efficiency report. Equivalent to #activities(by: time, detail: efficiency)
|
124
|
+
# @see #activities valid options
|
125
|
+
#
|
126
|
+
# @param [Hash] options options hash (same as #activities)
|
127
|
+
# @return [Array<Hash>]
|
128
|
+
def efficiency(options={})
|
129
|
+
self.activities({by: 'time'}.merge(options.merge(detail: 'efficiency')))
|
130
|
+
end
|
131
|
+
|
132
|
+
# Returns productivity report. Equivalent to #activities(detail: productivity)
|
133
|
+
# @see #activities valid options
|
134
|
+
#
|
135
|
+
# @example
|
136
|
+
# @client.productivity # Equivalent to @client.activities(detail: 'productivity')
|
137
|
+
# # => [
|
138
|
+
# # { :rank=>1, :time_spent_seconds=>6956, :number_of_people=>1, :productivity=>2 },
|
139
|
+
# # { :rank=>2, :time_spent_seconds=>2635, :number_of_people=>1, :productivity=>-2 },
|
140
|
+
# # { :rank=>3, :time_spent_seconds=>2415, :number_of_people=>1, :productivity=>1 },
|
141
|
+
# # { :rank=>4, :time_spent_seconds=>1210, :number_of_people=>1, :productivity=>0 },
|
142
|
+
# # { :rank=>5, :time_spent_seconds=>93, :number_of_people=>1, :productivity=>-1 }
|
143
|
+
# # ]
|
144
|
+
#
|
145
|
+
# @param [Hash] options options hash (same as #activities)
|
7
146
|
# @return [Array<Hash>]
|
8
|
-
def
|
9
|
-
|
10
|
-
|
11
|
-
format: 'csv' }
|
12
|
-
response = connection.get '', options
|
147
|
+
def productivity(options={})
|
148
|
+
self.activities(options.merge(detail: 'productivity'))
|
149
|
+
end
|
13
150
|
|
14
|
-
|
151
|
+
# Returns map of numeric productivity levels to meaning
|
152
|
+
#
|
153
|
+
# @example
|
154
|
+
# @activity = @client.activities(restrict_kind: 'productivity')[0]
|
155
|
+
# @primary_productivity = @client.productivity_levels[@activity[:productivity]]
|
156
|
+
#
|
157
|
+
# puts "I have spent most of my time being #{@primary_productivity}."
|
158
|
+
# # => I have spent most of my time being Very Productive.
|
159
|
+
#
|
160
|
+
# @return [Hash] productivity levels, with integers from -2 to 2 as keys
|
161
|
+
# @since v0.2.0
|
162
|
+
def productivity_levels
|
163
|
+
PRODUCTIVITY_LEVELS
|
15
164
|
end
|
16
165
|
|
17
166
|
private
|
18
167
|
|
19
168
|
# Takes a CSV with headers and returns an array of hashes
|
20
169
|
#
|
21
|
-
# @param body[CSV]
|
22
|
-
# @return [Array]
|
23
|
-
|
170
|
+
# @param body[CSV] the original CSV file
|
171
|
+
# @return [Array] an array of hashes, containing keys of the CSV headers
|
172
|
+
# @since v0.1.0
|
173
|
+
def array_of_hashes_from_csv(body)
|
24
174
|
activities = CSV.new(body,
|
25
175
|
headers: true,
|
26
176
|
header_converters: :symbol,
|
data/lib/rescuetime/api.rb
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
require 'rescuetime/activities'
|
2
2
|
|
3
3
|
module Rescuetime
|
4
|
+
# The Rescuetime::API module includes all modules corresponding to RescueTime
|
5
|
+
# API endpoints.
|
6
|
+
#
|
7
|
+
# For more information on the RescueTime Client, see #Rescuetime::Client.
|
8
|
+
# For more information on included methods, see method summary below.
|
9
|
+
# @since v0.1.0
|
4
10
|
module Api
|
5
|
-
# @note All methods have been separated into modules
|
6
11
|
include Rescuetime::Activities
|
7
12
|
end
|
8
13
|
end
|
data/lib/rescuetime/client.rb
CHANGED
@@ -4,15 +4,34 @@ require 'csv'
|
|
4
4
|
require 'rescuetime/api'
|
5
5
|
|
6
6
|
module Rescuetime
|
7
|
+
# Rescuetime::Client makes HTTP requests to the RescueTime API and returns
|
8
|
+
# the appropriate values
|
9
|
+
#
|
10
|
+
# @since v0.1.0
|
7
11
|
class Client
|
8
12
|
include Rescuetime::Api
|
9
13
|
|
14
|
+
# Default options passed in any request
|
15
|
+
# @since v0.2.0
|
16
|
+
DEFAULT_OPTIONS = {format: 'csv', version: 0, operation: 'select' }
|
17
|
+
|
18
|
+
# Overwrites the set RescueTime API key
|
19
|
+
#
|
20
|
+
# @!attribute [w] api_key
|
21
|
+
# @since v0.1.0
|
10
22
|
attr_writer :api_key
|
11
23
|
|
12
24
|
# Initializes a new Client object
|
13
25
|
#
|
14
|
-
# @
|
26
|
+
# @example
|
27
|
+
# @client = Rescuetime::Client.new # options must be set before requests can be made
|
28
|
+
# @example
|
29
|
+
# @client = Rescuetime::Client.new(api_key: '1234567890abcdefg')
|
30
|
+
#
|
31
|
+
# @param [Hash] options hash of RescueTime client options
|
32
|
+
# @option options [String] :api_key RescueTime API key
|
15
33
|
# @return [Rescuetime::Client]
|
34
|
+
# @since v0.1.0
|
16
35
|
def initialize(options={})
|
17
36
|
@api_key = options[:api_key]
|
18
37
|
end
|
@@ -20,8 +39,87 @@ module Rescuetime
|
|
20
39
|
# Checks whether an api key is set
|
21
40
|
#
|
22
41
|
# @return [Boolean]
|
42
|
+
# @since v0.1.0
|
23
43
|
def api_key?
|
24
44
|
!!@api_key
|
25
45
|
end
|
46
|
+
|
47
|
+
# Checks whether client credentials are valid. If credentials are present, this
|
48
|
+
# action involves an HTTP request.
|
49
|
+
#
|
50
|
+
# @example Three cases of checking credentials (missing, invalid, and valid)
|
51
|
+
# @client = Rescuetime::Client.new
|
52
|
+
# @client.valid_credentials? # => false
|
53
|
+
#
|
54
|
+
# # Note: The following scenarios involve an HTTP request.
|
55
|
+
# @client.api_key = 'Invalid Key'
|
56
|
+
# @client.valid_credentials? # => false
|
57
|
+
#
|
58
|
+
# @client.api_key = VALID_API_KEY
|
59
|
+
# @client.valid_credentials? # => true
|
60
|
+
#
|
61
|
+
# @return [Boolean]
|
62
|
+
# @since v0.2.0
|
63
|
+
def valid_credentials?
|
64
|
+
return false unless api_key?
|
65
|
+
!!self.activities rescue false
|
66
|
+
end
|
67
|
+
|
68
|
+
protected
|
69
|
+
|
70
|
+
# Performs an HTTP get request
|
71
|
+
#
|
72
|
+
# @param [String] url request url
|
73
|
+
# @param [Hash] options query params for request
|
74
|
+
#
|
75
|
+
# @raise [Rescuetime::MissingCredentials] if the Rescuetime::Client has no set api key
|
76
|
+
# @raise [Rescuetime::InvalidCredentials] if the provided api key is rejected by RescueTime
|
77
|
+
# @since v0.2.0
|
78
|
+
def get(url, options={})
|
79
|
+
raise Rescuetime::MissingCredentials unless api_key?
|
80
|
+
response = Faraday.get url, query_params(options).
|
81
|
+
merge(DEFAULT_OPTIONS).
|
82
|
+
merge({key: @api_key})
|
83
|
+
|
84
|
+
invalid_credentials_body = '{"error":"# key not found","messages":"key not found"}'
|
85
|
+
raise Rescuetime::InvalidCredentials if response.body == invalid_credentials_body
|
86
|
+
|
87
|
+
response
|
88
|
+
end
|
89
|
+
|
90
|
+
# Takes client request options hash and returns correct key/value pairs for HTTP request
|
91
|
+
#
|
92
|
+
# @param [Hash] options options hash of client request
|
93
|
+
# @return [Hash]
|
94
|
+
# @since v0.2.0
|
95
|
+
def query_params(options)
|
96
|
+
params = {}
|
97
|
+
params_mapping = { detail: :restrict_kind, by: :perspective, interval: :resolution_time }
|
98
|
+
|
99
|
+
params_mapping.each do |local, server|
|
100
|
+
params[server] = options[local] if options[local]
|
101
|
+
end
|
102
|
+
|
103
|
+
# Special Cases
|
104
|
+
params[:perspective] = 'interval' if params[:perspective] == 'time'
|
105
|
+
if options[:date]
|
106
|
+
params[:restrict_begin] = date_string options[:date]
|
107
|
+
params[:restrict_end] = params[:restrict_begin]
|
108
|
+
end
|
109
|
+
if options[:from]
|
110
|
+
params[:restrict_begin] = date_string options[:from]
|
111
|
+
params[:restrict_end] = date_string(options[:to] || Time.now)
|
112
|
+
end
|
113
|
+
params
|
114
|
+
end
|
115
|
+
|
116
|
+
# Takes a date in either "YYYY-MM-DD" format or as a Time object and
|
117
|
+
# returns a date string in "YYYY-MM-DD" format
|
118
|
+
#
|
119
|
+
# @return [String]
|
120
|
+
def date_string(date)
|
121
|
+
return date if date.is_a? String
|
122
|
+
date.strftime('%Y-%m-%d')
|
123
|
+
end
|
26
124
|
end
|
27
125
|
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module Rescuetime
|
2
|
+
# Error class for rescuing all RescueTime errors
|
3
|
+
class Error < StandardError; end
|
4
|
+
|
5
|
+
# Raised when a method requires credentials but none are provided
|
6
|
+
class MissingCredentials < Error; end
|
7
|
+
|
8
|
+
# Raised when a method requires credentials but provided credentials are invalid
|
9
|
+
class InvalidCredentials < Error; end
|
10
|
+
end
|
data/lib/rescuetime/version.rb
CHANGED
data/lib/rescuetime.rb
CHANGED
data/rescuetime.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ['lee@leesharma.com']
|
11
11
|
|
12
12
|
spec.summary = %q{Ruby interface for RescueTime}
|
13
|
-
spec.description = %q{
|
13
|
+
spec.description = %q{Ruby interface for the RescueTime Data Analytics API.}
|
14
14
|
spec.homepage = 'https://github.com/leesharma/rescuetime'
|
15
15
|
spec.license = 'MIT'
|
16
16
|
|
@@ -19,14 +19,14 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
20
|
spec.require_paths = ['lib']
|
21
21
|
|
22
|
-
spec.
|
22
|
+
spec.required_ruby_version = '>= 1.9.3'
|
23
|
+
|
24
|
+
spec.add_dependency 'faraday', '~> 0.9.1'
|
23
25
|
|
24
26
|
spec.add_development_dependency 'bundler'
|
25
|
-
spec.add_development_dependency 'rake'
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.4', '>= 10.4.2'
|
26
28
|
|
27
|
-
spec.add_development_dependency 'rspec'
|
28
|
-
spec.add_development_dependency 'vcr'
|
29
|
-
spec.add_development_dependency 'webmock'
|
30
|
-
spec.add_development_dependency 'codeclimate-test-reporter'
|
31
|
-
spec.add_development_dependency 'simplecov'
|
29
|
+
spec.add_development_dependency 'rspec', '~> 3.2', '>= 3.2.0'
|
30
|
+
spec.add_development_dependency 'vcr', '~> 2.9', '>= 2.9.3'
|
31
|
+
spec.add_development_dependency 'webmock', '~> 1.21', '>= 1.21.0'
|
32
32
|
end
|
metadata
CHANGED
@@ -1,97 +1,136 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: rescuetime
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
|
-
authors:
|
6
|
+
authors:
|
7
7
|
- Lee Sharma
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
- !ruby/object:Gem::Dependency
|
11
|
+
date: 2015-04-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
15
14
|
name: faraday
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: "0"
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.9.1
|
23
20
|
type: :runtime
|
24
|
-
version_requirements: *id001
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: bundler
|
27
21
|
prerelease: false
|
28
|
-
|
29
|
-
requirements:
|
30
|
-
-
|
31
|
-
|
32
|
-
|
33
|
-
- !ruby/object:Gem::Dependency
|
34
|
-
name:
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.9.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
39
34
|
type: :development
|
40
|
-
version_requirements: *id004
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: rspec
|
43
35
|
prerelease: false
|
44
|
-
|
45
|
-
requirements:
|
46
|
-
-
|
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: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.4'
|
48
|
+
- - '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 10.4.2
|
47
51
|
type: :development
|
48
|
-
version_requirements: *id005
|
49
|
-
- !ruby/object:Gem::Dependency
|
50
|
-
name: vcr
|
51
52
|
prerelease: false
|
52
|
-
|
53
|
-
requirements:
|
54
|
-
-
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ~>
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '10.4'
|
58
|
+
- - '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 10.4.2
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rspec
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ~>
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '3.2'
|
68
|
+
- - '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 3.2.0
|
55
71
|
type: :development
|
56
|
-
version_requirements: *id006
|
57
|
-
- !ruby/object:Gem::Dependency
|
58
|
-
name: webmock
|
59
72
|
prerelease: false
|
60
|
-
|
61
|
-
requirements:
|
62
|
-
-
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '3.2'
|
78
|
+
- - '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 3.2.0
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: vcr
|
83
|
+
requirement: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ~>
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '2.9'
|
88
|
+
- - '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: 2.9.3
|
63
91
|
type: :development
|
64
|
-
version_requirements: *id007
|
65
|
-
- !ruby/object:Gem::Dependency
|
66
|
-
name: codeclimate-test-reporter
|
67
92
|
prerelease: false
|
68
|
-
|
69
|
-
requirements:
|
70
|
-
-
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ~>
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '2.9'
|
98
|
+
- - '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: 2.9.3
|
101
|
+
- !ruby/object:Gem::Dependency
|
102
|
+
name: webmock
|
103
|
+
requirement: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ~>
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '1.21'
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 1.21.0
|
71
111
|
type: :development
|
72
|
-
version_requirements: *id008
|
73
|
-
- !ruby/object:Gem::Dependency
|
74
|
-
name: simplecov
|
75
112
|
prerelease: false
|
76
|
-
|
77
|
-
requirements:
|
78
|
-
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
113
|
+
version_requirements: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '1.21'
|
118
|
+
- - '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: 1.21.0
|
121
|
+
description: Ruby interface for the RescueTime Data Analytics API.
|
122
|
+
email:
|
83
123
|
- lee@leesharma.com
|
84
124
|
executables: []
|
85
|
-
|
86
125
|
extensions: []
|
87
|
-
|
88
126
|
extra_rdoc_files: []
|
89
|
-
|
90
|
-
files:
|
127
|
+
files:
|
91
128
|
- .gitignore
|
92
129
|
- .rspec
|
93
130
|
- .travis.yml
|
131
|
+
- CHANGELOG.md
|
94
132
|
- CODE_OF_CONDUCT.md
|
133
|
+
- CONTRIBUTING.md
|
95
134
|
- Gemfile
|
96
135
|
- LICENSE
|
97
136
|
- README.md
|
@@ -102,30 +141,31 @@ files:
|
|
102
141
|
- lib/rescuetime/activities.rb
|
103
142
|
- lib/rescuetime/api.rb
|
104
143
|
- lib/rescuetime/client.rb
|
144
|
+
- lib/rescuetime/errors.rb
|
105
145
|
- lib/rescuetime/version.rb
|
106
146
|
- rescuetime.gemspec
|
107
147
|
homepage: https://github.com/leesharma/rescuetime
|
108
|
-
licenses:
|
148
|
+
licenses:
|
109
149
|
- MIT
|
110
150
|
metadata: {}
|
111
|
-
|
112
151
|
post_install_message:
|
113
152
|
rdoc_options: []
|
114
|
-
|
115
|
-
require_paths:
|
153
|
+
require_paths:
|
116
154
|
- lib
|
117
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
-
requirements:
|
119
|
-
-
|
120
|
-
|
121
|
-
|
122
|
-
|
155
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - '>='
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: 1.9.3
|
160
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
161
|
+
requirements:
|
162
|
+
- - '>='
|
163
|
+
- !ruby/object:Gem::Version
|
164
|
+
version: '0'
|
123
165
|
requirements: []
|
124
|
-
|
125
166
|
rubyforge_project:
|
126
|
-
rubygems_version: 2.4.
|
167
|
+
rubygems_version: 2.4.5
|
127
168
|
signing_key:
|
128
169
|
specification_version: 4
|
129
170
|
summary: Ruby interface for RescueTime
|
130
171
|
test_files: []
|
131
|
-
|