harvesting 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ba547e0b7559bb7e2fd813f033eea49bc081ba343eeb76f16a7fe6f5844d094b
4
+ data.tar.gz: 619c571ee6951d6ba1680c8ef884b8f65029ca398039f080dbaad9c58a10033c
5
+ SHA512:
6
+ metadata.gz: e67ebf3abc0d39326847c700cabc18ee9211457621fd2ff3aa1e9bad91bd5bee09fa1e5c06147f65f277d98bc1edd14bbdb9cd92db71f2ce2d302d6a91eab2b9
7
+ data.tar.gz: 64044c30588913da4bd2abdd71b30cc890644a8db84bdc048c4a29d36d88d32a7b99e452b474c3727b653c44a4dab11588b1b937f7115877a85da38e28952045
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # byebug
14
+ .byebug_history
15
+
16
+ # rspec
17
+ .rspec
18
+
19
+ # ruby versions
20
+ .ruby-version
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at ernesto+github@ombulabs.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in harvesting.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,103 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ harvesting (0.1.0)
5
+ http (~> 3.3, >= 3.3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.5.2)
11
+ public_suffix (>= 2.0.2, < 4.0)
12
+ byebug (10.0.2)
13
+ coderay (1.1.2)
14
+ crack (0.4.3)
15
+ safe_yaml (~> 1.0.0)
16
+ diff-lcs (1.3)
17
+ domain_name (0.5.20180417)
18
+ unf (>= 0.0.5, < 1.0.0)
19
+ ffi (1.9.23)
20
+ formatador (0.2.5)
21
+ guard (2.14.2)
22
+ formatador (>= 0.2.4)
23
+ listen (>= 2.7, < 4.0)
24
+ lumberjack (>= 1.0.12, < 2.0)
25
+ nenv (~> 0.1)
26
+ notiffany (~> 0.0)
27
+ pry (>= 0.9.12)
28
+ shellany (~> 0.0)
29
+ thor (>= 0.18.1)
30
+ guard-compat (1.2.1)
31
+ guard-rspec (4.7.3)
32
+ guard (~> 2.1)
33
+ guard-compat (~> 1.1)
34
+ rspec (>= 2.99.0, < 4.0)
35
+ hashdiff (0.3.7)
36
+ http (3.3.0)
37
+ addressable (~> 2.3)
38
+ http-cookie (~> 1.0)
39
+ http-form_data (~> 2.0)
40
+ http_parser.rb (~> 0.6.0)
41
+ http-cookie (1.0.3)
42
+ domain_name (~> 0.5)
43
+ http-form_data (2.1.1)
44
+ http_parser.rb (0.6.0)
45
+ listen (3.1.5)
46
+ rb-fsevent (~> 0.9, >= 0.9.4)
47
+ rb-inotify (~> 0.9, >= 0.9.7)
48
+ ruby_dep (~> 1.2)
49
+ lumberjack (1.0.13)
50
+ method_source (0.9.0)
51
+ nenv (0.3.0)
52
+ notiffany (0.1.1)
53
+ nenv (~> 0.1)
54
+ shellany (~> 0.0)
55
+ pry (0.11.3)
56
+ coderay (~> 1.1.0)
57
+ method_source (~> 0.9.0)
58
+ public_suffix (3.0.2)
59
+ rake (10.5.0)
60
+ rb-fsevent (0.10.3)
61
+ rb-inotify (0.9.10)
62
+ ffi (>= 0.5.0, < 2)
63
+ rspec (3.7.0)
64
+ rspec-core (~> 3.7.0)
65
+ rspec-expectations (~> 3.7.0)
66
+ rspec-mocks (~> 3.7.0)
67
+ rspec-core (3.7.1)
68
+ rspec-support (~> 3.7.0)
69
+ rspec-expectations (3.7.0)
70
+ diff-lcs (>= 1.2.0, < 2.0)
71
+ rspec-support (~> 3.7.0)
72
+ rspec-mocks (3.7.0)
73
+ diff-lcs (>= 1.2.0, < 2.0)
74
+ rspec-support (~> 3.7.0)
75
+ rspec-support (3.7.1)
76
+ ruby_dep (1.5.0)
77
+ safe_yaml (1.0.4)
78
+ shellany (0.0.1)
79
+ thor (0.20.0)
80
+ unf (0.1.4)
81
+ unf_ext
82
+ unf_ext (0.0.7.5)
83
+ vcr (4.0.0)
84
+ webmock (3.4.0)
85
+ addressable (>= 2.3.6)
86
+ crack (>= 0.3.2)
87
+ hashdiff
88
+
89
+ PLATFORMS
90
+ ruby
91
+
92
+ DEPENDENCIES
93
+ bundler (~> 1.16)
94
+ byebug (~> 10.0, >= 10.0)
95
+ guard-rspec (~> 4.7, >= 4.7)
96
+ harvesting!
97
+ rake (~> 10.0)
98
+ rspec (~> 3.0)
99
+ vcr (~> 4.0, >= 4.0)
100
+ webmock (~> 3.4, >= 3.4)
101
+
102
+ BUNDLED WITH
103
+ 1.16.1
data/Guardfile ADDED
@@ -0,0 +1,70 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ # Note: The cmd option is now required due to the increasing number of ways
19
+ # rspec may be run, below are examples of the most common uses.
20
+ # * bundler: 'bundle exec rspec'
21
+ # * bundler binstubs: 'bin/rspec'
22
+ # * spring: 'bin/rspec' (This will use spring if running and you have
23
+ # installed the spring binstubs per the docs)
24
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
25
+ # * 'just' rspec: 'rspec'
26
+
27
+ guard :rspec, cmd: "bundle exec rspec" do
28
+ require "guard/rspec/dsl"
29
+ dsl = Guard::RSpec::Dsl.new(self)
30
+
31
+ # Feel free to open issues for suggestions and improvements
32
+
33
+ # RSpec files
34
+ rspec = dsl.rspec
35
+ watch(rspec.spec_helper) { rspec.spec_dir }
36
+ watch(rspec.spec_support) { rspec.spec_dir }
37
+ watch(rspec.spec_files)
38
+
39
+ # Ruby files
40
+ ruby = dsl.ruby
41
+ dsl.watch_spec_files_for(ruby.lib_files)
42
+
43
+ # Rails files
44
+ rails = dsl.rails(view_extensions: %w(erb haml slim))
45
+ dsl.watch_spec_files_for(rails.app_files)
46
+ dsl.watch_spec_files_for(rails.views)
47
+
48
+ watch(rails.controllers) do |m|
49
+ [
50
+ rspec.spec.call("routing/#{m[1]}_routing"),
51
+ rspec.spec.call("controllers/#{m[1]}_controller"),
52
+ rspec.spec.call("acceptance/#{m[1]}")
53
+ ]
54
+ end
55
+
56
+ # Rails config changes
57
+ watch(rails.spec_helper) { rspec.spec_dir }
58
+ watch(rails.routes) { "#{rspec.spec_dir}/routing" }
59
+ watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
60
+
61
+ # Capybara features specs
62
+ watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
63
+ watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
64
+
65
+ # Turnip features and steps
66
+ watch(%r{^spec/acceptance/(.+)\.feature$})
67
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
68
+ Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
69
+ end
70
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Ernesto Tagwerker
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,111 @@
1
+ # Harvesting
2
+
3
+ A Ruby gem to interact with the Harvest API v2.0 and forward.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'harvesting'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install harvesting
20
+
21
+ ## Usage
22
+
23
+ In order to start using this gem you will need your personal token and an
24
+ account id:
25
+
26
+ ### Identity
27
+
28
+ You can find these details over here: https://id.getharvest.com/developers
29
+
30
+ If you don't specify values for `access_token` or `account_id`, it will default
31
+ to these environment variables:
32
+
33
+ * `ENV['HARVEST_ACCESS_TOKEN']`
34
+ * `ENV['HARVEST_ACCOUNT_ID']`
35
+
36
+ That means that you could build a client like this:
37
+
38
+ # $ export HARVEST_ACCESS_TOKEN=abc
39
+ # $ export HARVEST_ACCOUNT_ID=12345678
40
+ client = Harvesting::Client.new
41
+ client.me
42
+ > => #<Harvesting::Models::User:0x007ff8830658f0 @attributes={"id"=>2108614, "first_name"=>"Ernesto", "last_name"=>"Tagwerker", ... >
43
+
44
+ If you don't specify a valid combination of token and account id, your code will
45
+ raise this error:
46
+
47
+ client = Harvesting::Client.new(access_token: "foo", account_id: "bar")
48
+ client.me
49
+ > Harvesting::AuthenticationError: {"error":"invalid_token","error_description":"The access token provided is expired, revoked, malformed or invalid for other reasons."}
50
+
51
+ If your personal token and account id are valid, you should see something like
52
+ this:
53
+
54
+ client = Harvesting::Client.new(access_token: "<your token here>", account_id: "<your account id here>")
55
+ user = client.me
56
+ > => #<Harvesting::Models::User:0x007ff8830658f0 @attributes={"id"=>2108614, "first_name"=>"Ernesto", "last_name"=>"Tagwerker", ... >
57
+
58
+ user.id
59
+ > => 2108614
60
+
61
+ ### Clients
62
+
63
+ client.clients
64
+ > => [#<Harvesting::Models::Client:0x007ff718d65fd0 @attributes={"id"=>6760580, "name"=>"Toto", "is_active"=>true, "address"=>"" ... >
65
+
66
+ client = client.clients.first
67
+ > => #<Harvesting::Models::Client:0x007ff718cf5fc8 @attributes={"id"=>6760580, "name"=>"Toto",
68
+ ... >
69
+
70
+ ### Time Entries
71
+
72
+ time_entries = client.time_entries
73
+ > => #<Harvesting::Models::TimeEntries:0x007ff71913e3a0 @attributes={"per_page"=>100, "total_pages"=>1, "total_entries"=>14, "next_page"=>nil, "previous_page"=>nil, "page"=>1, "links"=>{"first"=>"https://api.harvestapp.com/v2/time_entries?limit=1&page=1&per_page=100", "next"=>nil, "previous"=>nil, "last"=>"https://api.harvestapp.com/v2/time_entries?limit=1&page=1&per_page=100"}}, ... >
74
+
75
+ entry = time_entries.first
76
+ > => #<Harvesting::Models::TimeEntry:0x007ff71913dfe0 @attributes={"id"=>792860513, "spent_date"=>"2018-05-14", "hours"=>1.0, "notes"=>"hacked the things", "is_locked"=>false, "locked_reason"=>nil, "is_closed"=>false, "is_billed"=>false, "timer_started_at"=>nil, "started_time"=>nil, "ended_time"=>nil, "is_running"=>false, "billable"=>true, "budgeted"=>false, "billable_rate"=>nil, "cost_rate ... >
77
+
78
+ ### Tasks
79
+
80
+ tasks = client.tasks
81
+ > => #<Harvesting::Models::Tasks:0x007ff718897990 @attributes={"per_page"=>100, "total_pages"=>1, "total_entries"=>6, "next_page"=>nil, "previous_page"=>nil, "page"=>1, "links"=>{"first"=>"https://api.harvestapp.com/v2/tasks?page=1&per_page=100", "next"=>nil, ... >
82
+
83
+ ### Projects
84
+
85
+ projects = client.projects
86
+ > => #<Harvesting::Models::Projects:0x007ff718e1c8e8 @attributes={"per_page"=>100, "total_pages"=>1, "total_entries"=>1, "next_page"=>nil, "previous_page"=>nil, "page"=>1, "links"=>{"first"=>"https://api.harvestapp.com/v2/projects?page=1&per_page=100", ... >
87
+
88
+ project = projects.first
89
+ > => #<Harvesting::Models::Project:0x007ff718e1c618 @attributes={"id"=>17367712, "name"=>"Foo", "code"=>"", "is_active"=>true, "is_billable"=>true, "is_fixed_fee"=>false, "bill_by"=>"none", "budget"=>nil, "budget_by"=>"none", "budget_is_monthly"=>false, "notify_when_over_budget"=>false, "over_budget_notification_percentage"=>80.0, "show_budget_to_all"=>false, "created_at"=>"2018-05-13T03:30:06Z", ... >
90
+
91
+ ## Roadmap
92
+
93
+ There are many things to be developed for this gem. For now they are tracked here: [TODO.md](https://github.com/ombulabs/harvesting/blob/master/TODO.md)
94
+
95
+ ## Development
96
+
97
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
98
+
99
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
100
+
101
+ ## Contributing
102
+
103
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ombulabs/harvesting. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
104
+
105
+ ## License
106
+
107
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
108
+
109
+ ## Code of Conduct
110
+
111
+ Everyone interacting in the Harvesting project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ombulabs/harvesting/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/TODO.md ADDED
@@ -0,0 +1,2 @@
1
+ * Implement methods like `project.time_entries` or `user.time_entries`
2
+ * Implement methods like `project.time_entries.create(attribute: value)`
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "harvesting"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "harvesting/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "harvesting"
8
+ spec.version = Harvesting::VERSION
9
+ spec.authors = ["Ernesto Tagwerker"]
10
+ spec.email = ["ernesto+github@ombulabs.com"]
11
+
12
+ spec.summary = %q{Ruby wrapper for the Harvest API v2.0}
13
+ spec.description = %q{Interact with the Harvest API v2.0 from your Ruby application}
14
+ spec.homepage = "https://github.com/ombulabs/harvesting"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|fixtures|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+ spec.add_dependency "http", "~> 3.3", ">= 3.3"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.16"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency "guard-rspec", "~> 4.7", ">= 4.7"
29
+ spec.add_development_dependency "byebug", "~> 10.0", ">= 10.0"
30
+ spec.add_development_dependency "vcr", "~> 4.0", ">= 4.0"
31
+ spec.add_development_dependency "webmock", "~> 3.4", ">= 3.4"
32
+ end
data/lib/harvesting.rb ADDED
@@ -0,0 +1,17 @@
1
+ require "harvesting/version"
2
+ require "harvesting/enumerable"
3
+ require "harvesting/errors"
4
+ require "harvesting/models/base"
5
+ require "harvesting/models/user"
6
+ require "harvesting/models/contact"
7
+ require "harvesting/models/client"
8
+ require "harvesting/models/projects"
9
+ require "harvesting/models/project"
10
+ require "harvesting/models/tasks"
11
+ require "harvesting/models/task"
12
+ require "harvesting/models/time_entries"
13
+ require "harvesting/models/time_entry"
14
+ require "harvesting/client"
15
+
16
+ module Harvesting
17
+ end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "byebug"
4
+ require "http"
5
+ require "json"
6
+
7
+ module Harvesting
8
+ class Client
9
+ DEFAULT_HOST = "https://api.harvestapp.com/v2"
10
+
11
+ attr_accessor :access_token, :account_id
12
+
13
+ #
14
+ # @param opts
15
+ #
16
+ def initialize(access_token: ENV['HARVEST_ACCESS_TOKEN'], account_id: ENV['HARVEST_ACCOUNT_ID'])
17
+ @access_token = access_token.to_s
18
+ @account_id = account_id.to_s
19
+
20
+ if @account_id.length == 0 || @access_token.length == 0
21
+ raise ArgumentError.new("Access token and account id are required. Access token: '#{@access_token}'. Account ID: '#{@account_id}'.")
22
+ end
23
+ end
24
+
25
+ def me
26
+ Harvesting::Models::User.new(get("users/me"), client: self)
27
+ end
28
+
29
+ def clients
30
+ get("clients")["clients"].map do |result|
31
+ Harvesting::Models::Client.new(result, client: self)
32
+ end
33
+ end
34
+
35
+ def contacts
36
+ get("contacts")["contacts"].map do |result|
37
+ Harvesting::Models::Contact.new(result, client: self)
38
+ end
39
+ end
40
+
41
+ def time_entries(opts = {})
42
+ Harvesting::Models::TimeEntries.new(get("time_entries", opts), client: self)
43
+ end
44
+
45
+ def projects(opts = {})
46
+ Harvesting::Models::Projects.new(get("projects", opts), client: self)
47
+ end
48
+
49
+ def tasks(opts = {})
50
+ Harvesting::Models::Tasks.new(get("tasks", opts), client: self)
51
+ end
52
+
53
+ def create(entity)
54
+ url = "#{DEFAULT_HOST}/#{entity.path}"
55
+ uri = URI(url)
56
+ response = http_response(:post, uri, body: entity.to_hash)
57
+ entity.attributes = JSON.parse(response.body)
58
+ entity
59
+ end
60
+
61
+ def update(entity)
62
+ url = "#{DEFAULT_HOST}/#{entity.path}"
63
+ uri = URI(url)
64
+ response = http_response(:patch, uri, body: entity.to_hash)
65
+ entity.attributes = JSON.parse(response.body)
66
+ entity
67
+ end
68
+
69
+ private
70
+
71
+ def get(path, opts = {})
72
+ url = "#{DEFAULT_HOST}/#{path}"
73
+ url += "?#{opts.map {|k, v| "#{k}=#{v}"}.join("&")}" if opts.any?
74
+ uri = URI(url)
75
+ response = http_response(:get, uri)
76
+ JSON.parse(response.body)
77
+ end
78
+
79
+ def http_response(method, uri, opts = {})
80
+ response = nil
81
+
82
+ http = HTTP["User-Agent" => "Ruby Harvest API Sample",
83
+ "Authorization" => "Bearer #{@access_token}",
84
+ "Harvest-Account-ID" => @account_id]
85
+ params = {}
86
+ if opts[:body]
87
+ params[:json] = opts[:body]
88
+ end
89
+ response = http.send(method, uri, params)
90
+
91
+ raise Harvesting::AuthenticationError.new(response.to_s) if auth_error?(response)
92
+ raise Harvesting::UnprocessableRequest.new(response.to_s) if response.code.to_i == 422
93
+
94
+ response
95
+ end
96
+
97
+ def auth_error?(response)
98
+ response.code.to_i == 403 || response.code.to_i == 401
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,30 @@
1
+ # @see https://github.com/sferik/twitter/blob/aa909b3b7733ca619d80f1c8cba961033d1fc7e6/lib/twitter/enumerable.rb
2
+ module Harvesting
3
+ module Enumerable
4
+ include ::Enumerable
5
+
6
+ # @return [Enumerator]
7
+ def each(start = 0)
8
+ @cursor = start
9
+ return to_enum(:each, start) unless block_given?
10
+ Array(@entries[start..-1]).each_with_index do |element, index|
11
+ @cursor = index
12
+ yield(element)
13
+ end
14
+
15
+ unless last?
16
+ start = [@entries.size, start].max
17
+ fetch_next_page
18
+ each(start, &Proc.new)
19
+ end
20
+ self
21
+ end
22
+
23
+ private
24
+
25
+ # @return [Boolean]
26
+ def last?
27
+ (((page - 1) * per_page) + @cursor) >= (total_entries - 1)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,7 @@
1
+ module Harvesting
2
+ class AuthenticationError < StandardError
3
+ end
4
+
5
+ class UnprocessableRequest < StandardError
6
+ end
7
+ end
@@ -0,0 +1,37 @@
1
+ module Harvesting
2
+ module Models
3
+ class Base
4
+ attr_accessor :attributes
5
+ attr_reader :client
6
+
7
+ def initialize(attrs, opts = {})
8
+ @attributes = attrs.dup
9
+ @client = opts[:client] || Harvesting::Client.new(opts)
10
+ end
11
+
12
+ def self.attributed(*attribute_names)
13
+ attribute_names.each do |attribute_name|
14
+ Harvesting::Models::Base.send :define_method, attribute_name.to_s do
15
+ @attributes[__method__.to_s]
16
+ end
17
+ end
18
+ end
19
+
20
+ def save
21
+ id.nil? ? create : update
22
+ end
23
+
24
+ def create
25
+ @client.create(self)
26
+ end
27
+
28
+ def update
29
+ @client.update(self)
30
+ end
31
+
32
+ def to_hash
33
+ @attributes
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,14 @@
1
+ module Harvesting
2
+ module Models
3
+ class Client < Base
4
+ attributed :id,
5
+ :name,
6
+ :is_active,
7
+ :address,
8
+ :created_at,
9
+ :updated_at,
10
+ :currency
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ module Harvesting
2
+ module Models
3
+ class Contact < Base
4
+ attributed :id,
5
+ :title,
6
+ :first_name,
7
+ :last_name,
8
+ :email,
9
+ :phone_office,
10
+ :phone_mobile,
11
+ :fax,
12
+ :created_at,
13
+ :updated_at
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,33 @@
1
+ module Harvesting
2
+ module Models
3
+ class Project < Base
4
+ attributed :id,
5
+ :client,
6
+ :name,
7
+ :code,
8
+ :is_active,
9
+ :is_billable,
10
+ :is_fixed_fee,
11
+ :bill_by,
12
+ :hourly_rate,
13
+ :budget,
14
+ :budget_by,
15
+ :notify_when_over_budget,
16
+ :over_budget_notification_percentage,
17
+ :over_budget_notification_date,
18
+ :show_budget_to_all,
19
+ :cost_budget,
20
+ :cost_budget_include_expenses,
21
+ :fee,
22
+ :notes,
23
+ :starts_on,
24
+ :ends_on,
25
+ :created_at,
26
+ :updated_at
27
+
28
+ def path
29
+ id.nil? ? "projects" : "projects/#{id}"
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,45 @@
1
+ module Harvesting
2
+ module Models
3
+ class Projects < Base
4
+ include Harvesting::Enumerable
5
+ extend Forwardable
6
+
7
+ attributed :per_page,
8
+ :total_pages,
9
+ :total_entries,
10
+ :next_page,
11
+ :previous_page,
12
+ :page,
13
+ :links
14
+
15
+ attr_reader :entries
16
+
17
+ def initialize(attrs, opts = {})
18
+ super(attrs.reject {|k,v| k == "projects" }, opts)
19
+ @api_page = attrs
20
+ @entries = attrs["projects"].map do |entry|
21
+ Project.new(entry, client: opts[:client])
22
+ end
23
+ end
24
+
25
+ # def each
26
+ # @entries.each_with_index do |time_entry, index|
27
+ # yield(time_entry)
28
+ # end
29
+ # end
30
+ def page
31
+ @attributes['page']
32
+ end
33
+
34
+ def size
35
+ total_entries
36
+ end
37
+
38
+ def fetch_next_page
39
+ new_page = page + 1
40
+ @entries += client.projects(page: new_page).entries
41
+ @attributes['page'] = new_page
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,18 @@
1
+ module Harvesting
2
+ module Models
3
+ class Task < Base
4
+ attributed :id,
5
+ :name,
6
+ :billable_by_default,
7
+ :default_hourly_rate,
8
+ :is_default,
9
+ :is_active,
10
+ :created_at,
11
+ :updated_at
12
+
13
+ def path
14
+ id.nil? ? "tasks" : "tasks/#{id}"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,45 @@
1
+ module Harvesting
2
+ module Models
3
+ class Tasks < Base
4
+ include Harvesting::Enumerable
5
+ extend Forwardable
6
+
7
+ attributed :per_page,
8
+ :total_pages,
9
+ :total_entries,
10
+ :next_page,
11
+ :previous_page,
12
+ :page,
13
+ :links
14
+
15
+ attr_reader :entries
16
+
17
+ def initialize(attrs, opts = {})
18
+ super(attrs.reject {|k,v| k == "tasks" }, opts)
19
+ @api_page = attrs
20
+ @entries = attrs["tasks"].map do |entry|
21
+ Task.new(entry, client: opts[:client])
22
+ end
23
+ end
24
+
25
+ # def each
26
+ # @entries.each_with_index do |time_entry, index|
27
+ # yield(time_entry)
28
+ # end
29
+ # end
30
+ def page
31
+ @attributes['page']
32
+ end
33
+
34
+ def size
35
+ total_entries
36
+ end
37
+
38
+ def fetch_next_page
39
+ new_page = page + 1
40
+ @entries += client.tasks(page: new_page).entries
41
+ @attributes['page'] = new_page
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,45 @@
1
+ module Harvesting
2
+ module Models
3
+ class TimeEntries < Base
4
+ include Harvesting::Enumerable
5
+ extend Forwardable
6
+
7
+ attributed :per_page,
8
+ :total_pages,
9
+ :total_entries,
10
+ :next_page,
11
+ :previous_page,
12
+ :page,
13
+ :links
14
+
15
+ attr_reader :entries
16
+
17
+ def initialize(attrs, opts = {})
18
+ super(attrs.reject {|k,v| k == "time_entries" }, opts)
19
+ @api_page = attrs
20
+ @entries = attrs["time_entries"].map do |entry|
21
+ TimeEntry.new(entry, client: opts[:client])
22
+ end
23
+ end
24
+
25
+ # def each
26
+ # @entries.each_with_index do |time_entry, index|
27
+ # yield(time_entry)
28
+ # end
29
+ # end
30
+ def page
31
+ @attributes['page']
32
+ end
33
+
34
+ def size
35
+ total_entries
36
+ end
37
+
38
+ def fetch_next_page
39
+ new_page = page + 1
40
+ @entries += @client.time_entries(page: new_page).entries
41
+ @attributes['page'] = new_page
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,32 @@
1
+ module Harvesting
2
+ module Models
3
+ class TimeEntry < Base
4
+ attributed :id,
5
+ :spent_date,
6
+ :hours,
7
+ :notes,
8
+ :created_at,
9
+ :updated_at,
10
+ :is_locked,
11
+ :locked_reason,
12
+ :is_closed,
13
+ :is_billed,
14
+ :timer_started_at,
15
+ :started_time,
16
+ :ended_time,
17
+ :is_running,
18
+ :invoice,
19
+ :external_reference,
20
+ :billable,
21
+ :budgeted,
22
+ :billable_rate,
23
+ :cost_rate,
24
+ :project_id,
25
+ :task_id
26
+
27
+ def path
28
+ id.nil? ? "time_entries" : "time_entries/#{id}"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,25 @@
1
+ module Harvesting
2
+ module Models
3
+ class User < Base
4
+ attributed :id,
5
+ :first_name,
6
+ :last_name,
7
+ :email,
8
+ :telephone,
9
+ :timezone,
10
+ :weekly_capacity,
11
+ :has_access_to_all_future_projects,
12
+ :is_contractor,
13
+ :is_admin,
14
+ :is_project_manager,
15
+ :can_see_rates,
16
+ :can_create_projects,
17
+ :can_create_invoices,
18
+ :is_active,
19
+ :created_at,
20
+ :updated_at,
21
+ :roles,
22
+ :avatar_url
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module Harvesting
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,214 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: harvesting
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ernesto Tagwerker
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.3'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: '3.3'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.3'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '3.3'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.16'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.16'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '10.0'
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.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: guard-rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '4.7'
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '4.7'
85
+ type: :development
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '4.7'
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '4.7'
95
+ - !ruby/object:Gem::Dependency
96
+ name: byebug
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '10.0'
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '10.0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '10.0'
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '10.0'
115
+ - !ruby/object:Gem::Dependency
116
+ name: vcr
117
+ requirement: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - "~>"
120
+ - !ruby/object:Gem::Version
121
+ version: '4.0'
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '4.0'
125
+ type: :development
126
+ prerelease: false
127
+ version_requirements: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '4.0'
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '4.0'
135
+ - !ruby/object:Gem::Dependency
136
+ name: webmock
137
+ requirement: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - "~>"
140
+ - !ruby/object:Gem::Version
141
+ version: '3.4'
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '3.4'
145
+ type: :development
146
+ prerelease: false
147
+ version_requirements: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - "~>"
150
+ - !ruby/object:Gem::Version
151
+ version: '3.4'
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '3.4'
155
+ description: Interact with the Harvest API v2.0 from your Ruby application
156
+ email:
157
+ - ernesto+github@ombulabs.com
158
+ executables: []
159
+ extensions: []
160
+ extra_rdoc_files: []
161
+ files:
162
+ - ".gitignore"
163
+ - ".travis.yml"
164
+ - CODE_OF_CONDUCT.md
165
+ - Gemfile
166
+ - Gemfile.lock
167
+ - Guardfile
168
+ - LICENSE.txt
169
+ - README.md
170
+ - Rakefile
171
+ - TODO.md
172
+ - bin/console
173
+ - bin/setup
174
+ - harvesting.gemspec
175
+ - lib/harvesting.rb
176
+ - lib/harvesting/client.rb
177
+ - lib/harvesting/enumerable.rb
178
+ - lib/harvesting/errors.rb
179
+ - lib/harvesting/models/base.rb
180
+ - lib/harvesting/models/client.rb
181
+ - lib/harvesting/models/contact.rb
182
+ - lib/harvesting/models/project.rb
183
+ - lib/harvesting/models/projects.rb
184
+ - lib/harvesting/models/task.rb
185
+ - lib/harvesting/models/tasks.rb
186
+ - lib/harvesting/models/time_entries.rb
187
+ - lib/harvesting/models/time_entry.rb
188
+ - lib/harvesting/models/user.rb
189
+ - lib/harvesting/version.rb
190
+ homepage: https://github.com/ombulabs/harvesting
191
+ licenses:
192
+ - MIT
193
+ metadata: {}
194
+ post_install_message:
195
+ rdoc_options: []
196
+ require_paths:
197
+ - lib
198
+ required_ruby_version: !ruby/object:Gem::Requirement
199
+ requirements:
200
+ - - ">="
201
+ - !ruby/object:Gem::Version
202
+ version: '0'
203
+ required_rubygems_version: !ruby/object:Gem::Requirement
204
+ requirements:
205
+ - - ">="
206
+ - !ruby/object:Gem::Version
207
+ version: '0'
208
+ requirements: []
209
+ rubyforge_project:
210
+ rubygems_version: 2.7.6
211
+ signing_key:
212
+ specification_version: 4
213
+ summary: Ruby wrapper for the Harvest API v2.0
214
+ test_files: []