Class: TeamworkScrapeClient::Client
- Inherits:
-
Object
- Object
- TeamworkScrapeClient::Client
- Defined in:
- lib/teamwork_scrape_client/client.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
Instance Method Summary collapse
- #account ⇒ Object
- #company_by_name(company_name) ⇒ Object
- #copy_project(options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #login(email, password) ⇒ Object
- #mech ⇒ Object
- #profile ⇒ Object
- #project(id) ⇒ Object
- #project_by_name(project_name) ⇒ Object
- #today ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/teamwork_scrape_client/client.rb', line 9 def initialize( = {}) @email = [:email] @password = [:password] @base_url = [:base_url] @debug = [:debug] raise ArgumentError, 'email is required' unless @email raise ArgumentError, 'password is required' unless @password raise ArgumentError, 'base_url is required' unless @base_url login(email, password) end |
Instance Attribute Details
#base_url ⇒ Object (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 |
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
7 8 9 |
# File 'lib/teamwork_scrape_client/client.rb', line 7 def debug @debug end |
#email ⇒ Object (readonly)
Returns the value of attribute email.
7 8 9 |
# File 'lib/teamwork_scrape_client/client.rb', line 7 def email @email end |
#password ⇒ Object (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
#account ⇒ Object
49 50 51 52 53 |
# File 'lib/teamwork_scrape_client/client.rb', line 49 def account 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
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( = {}) raise ArgumentError, "old_project_id or old_project_name option is required" unless [:old_project_name] || [:old_project_id] old_project_id = if [:old_project_name] old_project = project_by_name([:old_project_name]) raise "Project #{[:old_project_name]} not found" unless old_project old_project['id'] else [:old_project_id] end new_project_name = [:new_project_name] raise ArgumentError, "new_project_name option is required" unless new_project_name new_company_name = [: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: account['id'], sourceInstallationId: account['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 login(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 |
#mech ⇒ Object
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 |
#profile ⇒ Object
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 |
#today ⇒ Object
144 145 146 |
# File 'lib/teamwork_scrape_client/client.rb', line 144 def today Date.today.strftime('%Y%m%d') end |