Module: Sredder

Defined in:
lib/sredder.rb,
lib/sredder/util.rb,
lib/sredder/version.rb,
lib/sredder/sredderc.rb,
lib/sredder/arg_parser.rb,
lib/sredder/wrike_auth.rb,
lib/sredder/github_auth.rb,
lib/sredder/wrike_request.rb,
lib/sredder/github_request.rb

Defined Under Namespace

Modules: Util Classes: ArgParser, GithubAuth, GithubRequest, Sredderc, WrikeAuth, WrikeRequest

Constant Summary collapse

VERSION =
"0.0.8"
WRIKE_OAUTH_OPTIONS =
{
  :site               => 'https://www.wrike.com',
  :authorize_path     => '/rest/auth/authorize',
  :access_token_path  => '/rest/auth/access_token',
  :request_token_path => '/rest/auth/request_token'
}

Class Method Summary collapse

Class Method Details

.create_task(access, title, description, parent_folder_ids) ⇒ Object

Create a Wrike task and return its ID



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sredder.rb', line 58

def self.create_task(access, title, description, parent_folder_ids)
  request = WrikeRequest.new(access)
  request.post('/api/json/v2/wrike.task.add', {
    title: title,
    description: description,
    status: 1,
    parents: parent_folder_ids
  })

  if request.json["error"]
    puts "#{request.json["error"]["code"]}: #{request.json["error"]["message"]}"
  else
    puts "Task Entry #: #{request.json["task"]["id"]}"
    puts "Title: #{request.json["task"]["title"]}"
    puts "Message: #{request.json["task"]["description"]}"
    request.json["task"]["id"]
  end
end

.create_timelog(access, date, task_id, hours, comment = '') ⇒ Object

Attaches a timelog entry to a Wrike task



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/sredder.rb', line 78

def self.create_timelog(access, date, task_id, hours, comment='')
  request = WrikeRequest.new(access)

  request.post('/api/json/v2/wrike.timelog.add', {
    date: date,
    taskId: task_id,
    hours: hours,
    comment: comment
  })

  if request.json["error"]
    puts "#{request.json["error"]["code"]}: #{request.json["error"]["message"]}"
  else
    puts "Timelog Entry #: #{request.json["timelog"]["id"]}"
    puts "On #{request.json["timelog"]["date"]} for #{request.json["timelog"]["hours"]} hours with comment: '#{request.json["timelog"]["comment"]}'"
  end
end

.get_access_tokenObject

gets an authenticated OAuth access token by loading the users .sredderc or prompting them to authenticate in their browser



35
36
37
38
39
# File 'lib/sredder.rb', line 35

def self.get_access_token
  auth = WrikeAuth.new
  auth.run_oauth_procedure unless auth.authorized?
  auth.oauth_access_token
end

.get_folder(access, folder) ⇒ Object

Fetches the ID of a wrike folder by name. Currently this is limited to subfolders of “SRED Time Tracking/”



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sredder.rb', line 43

def self.get_folder(access, folder)
  request = WrikeRequest.new(access)
  request.post('/api/json/v2/wrike.folder.tree', {})
  regex = Regexp.new(Regexp.escape("SRED Time Tracking/#{folder}"))

  if request.json["error"]
    puts request.json["error"]
  else
    request.json["foldersTree"]["folders"].each_with_object([]) do |folder, arr|
      arr << folder['id'].to_i if folder['fullPath'] =~ regex
    end
  end
end

.get_pull_request(options) ⇒ Object

Github



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sredder.rb', line 97

def self.get_pull_request(options)
  auth = GithubAuth.new
  auth.run_oauth_procedure unless auth.authorized?

  request = GithubRequest.new(auth.token)
  request.get("https://api.github.com/repos/medeo/medeo-rails/pulls/#{options[:github_id]}")

  options[:title] = request.json["title"]
  options[:message] = request.json["body"].gsub("\n", "<br>")
  options[:date] = Util.time_stamp(Time.parse(request.json["created_at"]))

  options
end

.run_command(argv) ⇒ Object

When debugging, comment out the validate! call and use something like… > Sredder.run_command([‘-t Hello’, ‘-m Message’, ‘-h3’])



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sredder.rb', line 14

def self.run_command(argv)
  args_parser = ArgParser.new
  args_parser.parse(argv)
  args_parser.validate!
  options = args_parser.options

  # Github
  if options[:github_id]
    get_pull_request(options)
  end

  # Wrike
  access = get_access_token
  folder_ids = get_folder(access, options[:folder])
  task_id = create_task(access, options[:title], options[:message], folder_ids)
  create_timelog(access, options[:date], task_id, options[:hours])
end