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:



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

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

  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

#debugObject (readonly)

Returns the value of attribute debug.



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

def debug
  @debug
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



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

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

#company_by_name(company_name) ⇒ Object



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

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:



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
116
117
118
119
120
121
122
123
124
# File 'lib/teamwork_scrape_client/client.rb', line 70

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

  old_project_id = if options[:old_project_name]
    old_project = project_by_name(options[:old_project_name])
    raise "Project #{options[:old_project_name]} not found" unless old_project
    old_project['id']
  else
    options[:old_project_id]
  end

  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



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

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



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

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

#profileObject



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

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



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

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



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

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")
  projects = JSON.parse(response.body)['projects']
  projects.find { |p| p['name'] == project_name }
end