Class: Forecasting::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/forecasting/client.rb

Constant Summary collapse

DEFAULT_HOST =
"https://api.forecastapp.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = 
  @host = host
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



8
9
10
# File 'lib/forecasting/client.rb', line 8

def access_token
  @access_token
end

#account_idObject

Returns the value of attribute account_id.



8
9
10
# File 'lib/forecasting/client.rb', line 8

def 
  @account_id
end

#hostObject

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 (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>

Returns:



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.

Parameters:

  • entity (Harvesting::Models::Base)

    A new record in your Harvest account

Returns:

  • (Harvesting::Models::Base)

    A subclass of ‘Harvesting::Models::Base` updated with the response from Harvest



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.

Parameters:

  • entity (Harvesting::Models::Base)

    A record to be removed from your Harvest account

Returns:

  • (Hash)

Raises:



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_stateForecasting::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.

Parameters:

  • path (String)

    path to be combined with ‘host`

  • opts (Hash) (defaults to: {})

    key/values will get passed as HTTP (GET) parameters

Returns:

  • (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>

Returns:



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>

Returns:



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

Returns:

  • (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>

Returns:



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>

Returns:



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

#subscriptionForecasting::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.

Parameters:

  • entity (Harvesting::Models::Base)

    An existing record in your Harvest account

Returns:

  • (Harvesting::Models::Base)

    A subclass of ‘Harvesting::Models::Base` updated with the response from Harvest



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

#whoamiForecasting::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