Class: TeamworkScrapeClient::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/teamwork_scrape_client/client.rb', line 8

def initialize(options = {})
  @email = options[:email]
  @password = options[:password]
  @base_url = options[:base_url]

  raise ArgumentError, 'email is required' unless @email
  raise ArgumentError, 'password is required' unless @password
  raise ArgumentError, 'base_url is required' unless @base_url

  (email, password)
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



6
7
8
# File 'lib/teamwork_scrape_client/client.rb', line 6

def base_url
  @base_url
end

#emailObject (readonly)

Returns the value of attribute email.



6
7
8
# File 'lib/teamwork_scrape_client/client.rb', line 6

def email
  @email
end

#passwordObject (readonly)

Returns the value of attribute password.



6
7
8
# File 'lib/teamwork_scrape_client/client.rb', line 6

def password
  @password
end

Instance Method Details

#accountObject



45
46
47
48
49
# File 'lib/teamwork_scrape_client/client.rb', line 45

def 
  return @account if @account
  response = mech.get('/account.json')
  @account = JSON.parse(response.body)['account']
end

#company_by_name(company_name) ⇒ Object



56
57
58
59
60
61
# File 'lib/teamwork_scrape_client/client.rb', line 56

def company_by_name(company_name)
  response = mech.get("/projects.json?getActivePages=true&searchCompany=true&formatMarkdown=false&status=active&getCategoryPath=1&userId=0&page=1&pageSize=500&orderBy=lastActivityDate&orderMode=DESC")
  projects = JSON.parse(response.body)['projects']
  companies = projects.map { |p| p['company'] }.uniq
  companies.find { |c| c['name'] == company_name }
end

#copy_project(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/teamwork_scrape_client/client.rb', line 68

def copy_project(options = {})
  old_project_id = options[:old_project_id]
  raise ArgumentError, "old_project_id option is required" unless old_project_id

  new_project_name = options[:new_project_name]
  raise ArgumentError, "new_project_name option is required" unless new_project_name

  new_company_name = options[:new_company_name]
  raise ArgumentError, "new_company_name option is required" unless new_company_name

  # Find an existing company or add a new one
  existing_company = company_by_name(new_company_name)
  existing_company_id = existing_company ? existing_company['id'] : nil

  old_project = project(old_project_id)

  response = mech.post(
    '/index.cfm',
    action: 'CloneProject_CreateProjectClone',
    id: old_project_id,
    installationId: ['id'],
    'cloneproject-action' => 'copy',
    cloneProjectName: new_project_name,
    copyTasks: 'YES',
    copyMilestones: 'YES',
    copyMessages: 'YES',
    copyFiles: 'YES',
    copyLinks: 'YES',
    copyNotebooks: 'YES',
    copyTimelogs: 'YES',
    copyInvoices: 'YES',
    copyExpenses: 'YES',
    copyRisks: 'YES',
    copyPeople: 'YES',
    daysOffset: old_project.days_offset,
    keepOffWeekends: '1',
    createActivityLog: 'YES',
    createItemsUsingCurrentUser: 'NO',
    uncomplete: 'YES',
    copyComments: 'YES',
    copyProjectRoles: 'YES',
    copyLogo: 'YES',
    companyId: existing_company_id || 0,
    newCompanyName: existing_company_id ? '' : new_company_name
  )

  JSON.parse(response.body)
end

#login(email, password) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/teamwork_scrape_client/client.rb', line 26

def (email, password)
  mech.post(
    "#{base_url}/v/1/login.json?getInitialPage=true",
    { username: email, password: password, rememberMe: false }.to_json,
    { 'Content-Type' => 'application/json' }
  )
end

#mechObject



20
21
22
23
24
# File 'lib/teamwork_scrape_client/client.rb', line 20

def mech
  @mech ||= Mechanize.new do |m|
    m.log = Logger.new(STDOUT)
  end
end

#profileObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/teamwork_scrape_client/client.rb', line 34

def profile
  response = mech.get('/me.json',
                      fullprofile: 1,
                      getPreferences: 1,
                      cleanPreferences: true,
                      getAccounts: 1,
                      includeAuth: 1,
                      includeClockIn: 1)
  JSON.parse(response.body)['profile']
end

#project(id) ⇒ Object



51
52
53
54
# File 'lib/teamwork_scrape_client/client.rb', line 51

def project(id)
  response = mech.get("/projects/#{id}.json?getPermissions=true&getNotificationSettings=true&getActivePages=true&getDateInfo=true&getEmailAddress=true&formatMarkdown=false")
  Project.new(JSON.parse(response.body)['project'])
end

#project_by_name(project_name) ⇒ Object



63
64
65
66
# File 'lib/teamwork_scrape_client/client.rb', line 63

def project_by_name(project_name)
  response = mech.get("/projects.json?getActivePages=true&searchCompany=true&formatMarkdown=false&status=active&getCategoryPath=1&userId=0&page=1&pageSize=500&orderBy=lastActivityDate&orderMode=DESC")
  JSON.parse(response.body)['projects']
end