Module: TaskReport::Gist

Defined in:
lib/task_report/gist.rb

Class Method Summary collapse

Class Method Details

.create(params) ⇒ Object



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

def create(params)
  uri = URI "https://api.github.com/gists"
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Post.new(uri.request_uri)
  request['Accept'] = 'application/json'
  request['Content-Type'] = 'application/json'
  request['Authorization'] = "token #{User.api_token}"
  request.body = JSON.dump(params)
  response = http.request(request)
  JSON.parse(response.body)
end

.create_or_update(params, from) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/task_report/gist.rb', line 90

def create_or_update(params, from)
  description = params[:description]
  found = find_gists_by_description(from)

  if found
    edit(found['id'], params)
    return found
  end

  create(params)
end

.delete(gist_id) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/task_report/gist.rb', line 70

def delete(gist_id)
  uri = URI "https://api.github.com/gists/#{gist_id}"
  request = Net::HTTP::Delete.new(uri.request_uri)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request['Authorization'] = "token #{User.api_token}"
  http.request(request)
end

.edit(gist_id, params) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/task_report/gist.rb', line 57

def edit(gist_id, params)
  uri = URI "https://api.github.com/gists/#{gist_id}"
  request = Net::HTTP::Patch.new(uri.request_uri)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request['Accept'] = 'application/json'
  request['Content-Type'] = 'application/json'
  request['Authorization'] = "token #{User.api_token}"
  request.body = JSON.dump(params)
  response = http.request(request)
  JSON.parse(response.body)
end

.file_content(raw_url) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/task_report/gist.rb', line 79

def file_content(raw_url)
  uri = URI raw_url
  request = Net::HTTP::Get.new(uri.request_uri)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request['Accept'] = 'application/json'
  request['Authorization'] = "token #{User.api_token}"
  response = http.request(request)
  JSON.parse(response.body)
end

.find_gist_from_today_by_description(description) ⇒ Object



8
9
10
11
12
# File 'lib/task_report/gist.rb', line 8

def find_gist_from_today_by_description(description)
  get_gists_for_user.find do |gist|
    gist['description'] == description
  end
end

.find_gists_by_description(description, from = Time.now) ⇒ Object



20
21
22
23
24
# File 'lib/task_report/gist.rb', line 20

def find_gists_by_description(description, from = Time.now)
  get_gists_for_user(from).find do |gist|
    description == gist['description']
  end
end

.find_gists_by_descriptions(descriptions, from = Time.now) ⇒ Object



14
15
16
17
18
# File 'lib/task_report/gist.rb', line 14

def find_gists_by_descriptions(descriptions, from = Time.now)
  get_gists_for_user(from).select do |gist|
    descriptions.include? gist['description']
  end
end

.get_gists_for_user(from = Time.now) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/task_report/gist.rb', line 26

def get_gists_for_user(from = Time.now)
  # kind of like Time.now.midnight in UTC
  time_string = Time.new(from.year, from.month, from.day).getgm.strftime('%Y-%m-%dT%H:%M:%SZ')

  params = {
    access_token: User.api_token,
    since: time_string
  }

  uri = URI "https://api.github.com/users/#{User.name}/gists"
  uri.query = URI.encode_www_form params
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)
  JSON.parse(response.body)
end