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)


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

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.



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

def base_url
  @base_url
end

#debugObject (readonly)

Returns the value of attribute debug.



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

def debug
  @debug
end

#emailObject (readonly)

Returns the value of attribute email.



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

def email
  @email
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

Instance Method Details

#accountObject



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

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

#company_by_name(company_name) ⇒ Object



60
61
62
63
64
65
# File 'lib/teamwork_scrape_client/client.rb', line 60

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)


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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/teamwork_scrape_client/client.rb', line 73

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)

  request_body = {
    id: old_project_id,
    installationId: ['id'],
    sourceInstallationId: ['id'],
    'cloneproject-action' => 'copy',
    cloneProjectName: new_project_name,
    copyBudgets: 'NO',
    copyTasks: 'YES',
    copyMilestones: 'YES',
    copyMessages: 'YES',
    copyFiles: 'YES',
    copyFollowers: "YES",
    copyForms: "NO",
    copyLinks: 'YES',
    copyNotebooks: 'YES',
    copyTimelogs: 'NO',
    copyInvoices: 'NO',
    copyExpenses: 'YES',
    copyRisks: 'YES',
    copyPeople: 'YES',
    copyProjectPrivacy: 'YES',
    copyProjectUpdates: 'NO',
    daysOffset: old_project.days_offset,
    description: '',
    keepOffWeekends: '1',
    createActivityLog: 'YES',
    createItemsUsingCurrentUser: 'NO',
    uncomplete: 'YES',
    copyComments: 'YES',
    copyProjectRoles: 'YES',
    copyLogo: 'NO',
    companyId: existing_company_id || 0,
    newCompanyName: existing_company_id ? '' : new_company_name,
    targetDate: today,
    newFromTemplate: false,
    tagIds: '',
    toTemplate: false
  }

  response = mech.post(
    "/projects/#{old_project_id}/clone.json",
    request_body.to_json,
    'Content-Type' => 'application/json'
  )

  JSON.parse(response.body)
end

#login(email, password) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/teamwork_scrape_client/client.rb', line 30

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



22
23
24
25
26
27
28
# File 'lib/teamwork_scrape_client/client.rb', line 22

def mech
  @mech ||= Mechanize.new do |m|
    m.log = Logger.new(STDOUT) if debug
    # Set the timeout to 15 minutes because copying can be SLOW
    m.read_timeout = 15 * 60
  end
end

#profileObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/teamwork_scrape_client/client.rb', line 38

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



55
56
57
58
# File 'lib/teamwork_scrape_client/client.rb', line 55

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



67
68
69
70
71
# File 'lib/teamwork_scrape_client/client.rb', line 67

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

#todayObject



144
145
146
# File 'lib/teamwork_scrape_client/client.rb', line 144

def today
  Date.today.strftime('%Y%m%d')
end