Class: Freeagent::API

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

Instance Method Summary collapse

Constructor Details

#initializeAPI

Returns a new instance of API.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/freeagent.rb', line 11

def initialize
  if FREEAGENT_APP_ID.nil? || FREEAGENT_APP_ID.empty?
    raise ArgumentError, 'FREEAGENT_APP_ID is unset'
  end
  if FREEAGENT_APP_SECRET.nil? || FREEAGENT_APP_SECRET.empty?
    raise ArgumentError, 'FREEAGENT_APP_SECRET is unset'
  end

  @token = reload || authorize
  File.write TOKEN_FILE, @token.to_hash.to_hash.to_yaml
end

Instance Method Details

#batch_create_timeslips(timeslips) ⇒ Object



158
159
160
# File 'lib/freeagent.rb', line 158

def batch_create_timeslips timeslips
  post('timeslips', {'timeslips' => timeslips})
end

#create_timeslip(user, project, task, dated, hours) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'lib/freeagent.rb', line 148

def create_timeslip user, project, task, dated, hours
  post('timeslips', {
    'task' => task.url,
    'project' => project.url,
    'user' => user.url,
    'dated_on' => dated.to_s,
    'hours' => hours,
  })
end

#delete_timeslip(timeslip) ⇒ Object



162
163
164
165
# File 'lib/freeagent.rb', line 162

def delete_timeslip timeslip
  id = timeslip.url.split('/').last
  delete('timeslips', id)
end

#first_accounting_year_endObject



113
114
115
# File 'lib/freeagent.rb', line 113

def first_accounting_year_end
  Date.parse get('company').company.first_accounting_year_end
end

#payslips(year, period) ⇒ Object



121
122
123
# File 'lib/freeagent.rb', line 121

def payslips year, period
  get('payroll', year, period).period.payslips
end

#periods(year) ⇒ Object



117
118
119
# File 'lib/freeagent.rb', line 117

def periods year
  get('payroll', year).periods
end

#profile(year, user) ⇒ Object



175
176
177
# File 'lib/freeagent.rb', line 175

def profile year, user
  get('payroll_profiles', year, user: user)
end

#profiles(year) ⇒ Object



125
126
127
# File 'lib/freeagent.rb', line 125

def profiles year
  get('payroll_profiles', year).profiles
end

#projectsObject



129
130
131
# File 'lib/freeagent.rb', line 129

def projects
  get_pages('projects').flat_map(&:projects)
end

#tasks(project) ⇒ Object



133
134
135
# File 'lib/freeagent.rb', line 133

def tasks project
  get_pages('tasks', project: project.url).flat_map(&:tasks)
end

#timeslips(user: nil, project: nil, task: nil, from: nil, to: nil) ⇒ Object



137
138
139
140
141
142
143
144
145
146
# File 'lib/freeagent.rb', line 137

def timeslips user: nil, project: nil, task: nil, from: nil, to: nil
  params = [
    [:user, user && user.url],
    [:project, project && project.url],
    [:task, task && task.url],
    [:from_date, from && from.to_s],
    [:to_date, to && to.to_s],
  ].reject {|(k, v)| v.nil?}.to_h
  get_pages('timeslips', **params).flat_map(&:timeslips)
end

#user(id) ⇒ Object



171
172
173
# File 'lib/freeagent.rb', line 171

def user id
  get('users', id).user
end

#usersObject



167
168
169
# File 'lib/freeagent.rb', line 167

def users
  get_pages('users').flat_map(&:users)
end