Class: Forecasting::Client
- Inherits:
-
Object
- Object
- Forecasting::Client
- Defined in:
- lib/forecasting/client.rb
Constant Summary collapse
- DEFAULT_HOST =
"https://api.forecastapp.com"
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#account_id ⇒ Object
Returns the value of attribute account_id.
-
#host ⇒ Object
Returns the value of attribute host.
Instance Method Summary collapse
- #account(id:) ⇒ Forecasting::Models::Account
- #assignment(id:) ⇒ Forecasting::Models::Assignment
- #assignments(opts = {}) ⇒ Array<Forecasting::Models::Assignment>
- #client(id:) ⇒ Forecasting::Models::Client
- #clients(opts = {}) ⇒ Array<Forecasting::Models::Client>
-
#create(entity) ⇒ Harvesting::Models::Base
Creates an ‘entity` in your Harvest account.
-
#delete(entity) ⇒ Hash
It removes an ‘entity` from your Harvest account.
- #ftux_state ⇒ Forecasting::Models::FtuxState
- #future_scheduled_hours(opts = {}) ⇒ Array<Forecasting::Models::FutureScheduledHours>
-
#get(path, opts = {}) ⇒ Hash
Performs a GET request and returned the parsed JSON as a Hash.
-
#initialize(access_token: ENV['FORECAST_ACCESS_TOKEN'], account_id: ENV['FORECAST_ACCOUNT_ID'], host: DEFAULT_HOST) ⇒ Client
constructor
A new instance of Client.
- #milestone(id:) ⇒ Forecasting::Models::Milestone
- #milestones(opts = {}) ⇒ Array<Forecasting::Models::Milestone>
- #people(opts = {}) ⇒ Array<Forecasting::Models::Person>
- #person(id:) ⇒ Forecasting::Models::Person
- #placeholder(id:) ⇒ Forecasting::Models::Placeholder
- #placeholders(opts = {}) ⇒ Array<Forecasting::Models::Placeholder>
- #project(id:) ⇒ Forecasting::Models::Projects
- #projects(opts = {}) ⇒ Array<Forecasting::Models::Project>
- #remaining_budgeted_hours(opts = {}) ⇒ Array<Forecasting::Models::RemainingBudgetedHours>
- #repeated_assignment_set(id:) ⇒ Forecasting::Models::RepeatedAssignmentSet
- #repeated_assignment_sets(opts = {}) ⇒ Array<Forecasting::Models::RepeatedAssignmentSet>
- #role(id:) ⇒ Forecasting::Models::Role
- #roles(opts = {}) ⇒ Array<Forecasting::Models::Role>
- #subscription ⇒ Forecasting::Models::Subscription
-
#update(entity) ⇒ Harvesting::Models::Base
Updates an ‘entity` in your Harvest account.
- #user_connections(opts = {}) ⇒ Forecasting::Models::UserConnection
- #whoami ⇒ Forecasting::Models::User
Constructor Details
#initialize(access_token: ENV['FORECAST_ACCESS_TOKEN'], account_id: ENV['FORECAST_ACCOUNT_ID'], host: DEFAULT_HOST) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 15 16 |
# File 'lib/forecasting/client.rb', line 10 def initialize(access_token: ENV['FORECAST_ACCESS_TOKEN'], account_id: ENV['FORECAST_ACCOUNT_ID'], host: DEFAULT_HOST) @access_token = access_token @account_id = account_id @host = host end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
8 9 10 |
# File 'lib/forecasting/client.rb', line 8 def access_token @access_token end |
#account_id ⇒ Object
Returns the value of attribute account_id.
8 9 10 |
# File 'lib/forecasting/client.rb', line 8 def account_id @account_id end |
#host ⇒ Object
Returns the value of attribute host.
8 9 10 |
# File 'lib/forecasting/client.rb', line 8 def host @host end |
Instance Method Details
#account(id:) ⇒ Forecasting::Models::Account
19 20 21 |
# File 'lib/forecasting/client.rb', line 19 def account(id:) Forecasting::Models::Account.new(get("accounts/#{id}")["account"], forecast_client: self) end |
#assignment(id:) ⇒ Forecasting::Models::Assignment
24 25 26 |
# File 'lib/forecasting/client.rb', line 24 def assignment(id:) Forecasting::Models::Assignment.get(id, forecast_client: self) end |
#assignments(opts = {}) ⇒ Array<Forecasting::Models::Assignment>
29 30 31 32 33 |
# File 'lib/forecasting/client.rb', line 29 def assignments(opts = {}) get("assignments", opts)["assignments"].map do |entry| Forecasting::Models::Assignment.new(entry, forecast_client: self) end end |
#client(id:) ⇒ Forecasting::Models::Client
36 37 38 |
# File 'lib/forecasting/client.rb', line 36 def client(id:) Forecasting::Models::Client.get(id, forecast_client: self) end |
#clients(opts = {}) ⇒ Array<Forecasting::Models::Client>
41 42 43 44 45 |
# File 'lib/forecasting/client.rb', line 41 def clients(opts = {}) get("clients", opts)["clients"].map do |entry| Forecasting::Models::Client.new(entry, forecast_client: self) end end |
#create(entity) ⇒ Harvesting::Models::Base
Creates an ‘entity` in your Harvest account.
160 161 162 163 164 165 166 |
# File 'lib/forecasting/client.rb', line 160 def create(entity) url = "#{host}/#{entity.path}" uri = URI(url) response = http_response(:post, uri, body: payload(entity)) entity.attributes = JSON.parse(response.body)[entity.type] entity end |
#delete(entity) ⇒ Hash
It removes an ‘entity` from your Harvest account.
185 186 187 188 189 190 191 192 |
# File 'lib/forecasting/client.rb', line 185 def delete(entity) url = "#{host}/#{entity.path}" uri = URI(url) response = http_response(:delete, uri) raise Forecasting::Errors::UnprocessableRequest.new(response.to_s) unless response.code.to_i == 200 JSON.parse(response.body) end |
#ftux_state ⇒ Forecasting::Models::FtuxState
48 49 50 |
# File 'lib/forecasting/client.rb', line 48 def ftux_state Forecasting::Models::FtuxState.new(get("ftux_state")["ftux_state"], forecast_client: self) end |
#future_scheduled_hours(opts = {}) ⇒ Array<Forecasting::Models::FutureScheduledHours>
53 54 55 56 57 58 |
# File 'lib/forecasting/client.rb', line 53 def future_scheduled_hours(opts = {}) start_date = opts.delete(:start_date) || Time.now.strftime("%Y-%m-%d") get("aggregate/future_scheduled_hours/#{start_date}", opts)["future_scheduled_hours"].map do |entry| Forecasting::Models::FutureScheduledHours.new(entry, forecast_client: self) end end |
#get(path, opts = {}) ⇒ Hash
Performs a GET request and returned the parsed JSON as a Hash.
199 200 201 202 203 204 205 |
# File 'lib/forecasting/client.rb', line 199 def get(path, opts = {}) url = "#{host}/#{path}" url += "?#{opts.map {|k, v| "#{k}=#{v}"}.join("&")}" if opts.any? uri = URI(url) response = http_response(:get, uri) JSON.parse(response.body) end |
#milestone(id:) ⇒ Forecasting::Models::Milestone
61 62 63 |
# File 'lib/forecasting/client.rb', line 61 def milestone(id:) Forecasting::Models::Milestone.get(id, forecast_client: self) end |
#milestones(opts = {}) ⇒ Array<Forecasting::Models::Milestone>
66 67 68 69 70 |
# File 'lib/forecasting/client.rb', line 66 def milestones(opts = {}) get("milestones", opts)["milestones"].map do |entry| Forecasting::Models::Milestone.new(entry, forecast_client: self) end end |
#people(opts = {}) ⇒ Array<Forecasting::Models::Person>
78 79 80 81 82 |
# File 'lib/forecasting/client.rb', line 78 def people(opts = {}) get("people", opts)["people"].map do |entry| Forecasting::Models::Person.new(entry, forecast_client: self) end end |
#person(id:) ⇒ Forecasting::Models::Person
73 74 75 |
# File 'lib/forecasting/client.rb', line 73 def person(id:) Forecasting::Models::Person.get(id, forecast_client: self) end |
#placeholder(id:) ⇒ Forecasting::Models::Placeholder
85 86 87 |
# File 'lib/forecasting/client.rb', line 85 def placeholder(id:) Forecasting::Models::Placeholder.get(id, forecast_client: self) end |
#placeholders(opts = {}) ⇒ Array<Forecasting::Models::Placeholder>
90 91 92 93 94 |
# File 'lib/forecasting/client.rb', line 90 def placeholders(opts = {}) get("placeholders", opts)["placeholders"].map do |entry| Forecasting::Models::Placeholder.new(entry, forecast_client: self) end end |
#project(id:) ⇒ Forecasting::Models::Projects
97 98 99 |
# File 'lib/forecasting/client.rb', line 97 def project(id:) Forecasting::Models::Project.get(id, forecast_client: self) end |
#projects(opts = {}) ⇒ Array<Forecasting::Models::Project>
102 103 104 105 106 |
# File 'lib/forecasting/client.rb', line 102 def projects(opts = {}) get("projects", opts)["projects"].map do |entry| Forecasting::Models::Project.new(entry, forecast_client: self) end end |
#remaining_budgeted_hours(opts = {}) ⇒ Array<Forecasting::Models::RemainingBudgetedHours>
109 110 111 112 113 |
# File 'lib/forecasting/client.rb', line 109 def remaining_budgeted_hours(opts = {}) get("aggregate/remaining_budgeted_hours", opts)["remaining_budgeted_hours"].map do |entry| Forecasting::Models::RemainingBudgetedHours.new(entry, forecast_client: self) end end |
#repeated_assignment_set(id:) ⇒ Forecasting::Models::RepeatedAssignmentSet
116 117 118 |
# File 'lib/forecasting/client.rb', line 116 def repeated_assignment_set(id:) Forecasting::Models::RepeatedAssignmentSet.get(id, forecast_client: self) end |
#repeated_assignment_sets(opts = {}) ⇒ Array<Forecasting::Models::RepeatedAssignmentSet>
121 122 123 124 125 |
# File 'lib/forecasting/client.rb', line 121 def repeated_assignment_sets(opts = {}) get("repeated_assignment_sets", opts)["repeated_assignment_sets"].map do |entry| Forecasting::Models::RepeatedAssignmentSet.new(entry, forecast_client: self) end end |
#role(id:) ⇒ Forecasting::Models::Role
128 129 130 |
# File 'lib/forecasting/client.rb', line 128 def role(id:) Forecasting::Models::Role.get(id, forecast_client: self) end |
#roles(opts = {}) ⇒ Array<Forecasting::Models::Role>
133 134 135 136 137 |
# File 'lib/forecasting/client.rb', line 133 def roles(opts = {}) get("roles", opts)["roles"].map do |entry| Forecasting::Models::Role.new(entry, forecast_client: self) end end |
#subscription ⇒ Forecasting::Models::Subscription
140 141 142 |
# File 'lib/forecasting/client.rb', line 140 def subscription Forecasting::Models::Subscription.new(get("billing/subscription")["subscription"], forecast_client: self) end |
#update(entity) ⇒ Harvesting::Models::Base
Updates an ‘entity` in your Harvest account.
172 173 174 175 176 177 178 |
# File 'lib/forecasting/client.rb', line 172 def update(entity) url = "#{host}/#{entity.path}" uri = URI(url) response = http_response(:patch, uri, body: payload(entity)) entity.attributes = JSON.parse(response.body)[entity.type] entity end |
#user_connections(opts = {}) ⇒ Forecasting::Models::UserConnection
145 146 147 148 149 |
# File 'lib/forecasting/client.rb', line 145 def user_connections(opts = {}) get("user_connections", opts)["user_connections"].map do |entry| Forecasting::Models::UserConnection.new(entry, forecast_client: self) end end |
#whoami ⇒ Forecasting::Models::User
152 153 154 |
# File 'lib/forecasting/client.rb', line 152 def whoami Forecasting::Models::User.new(get("whoami")["current_user"], forecast_client: self) end |