module TaskReport::Gist

Public Class Methods

create(params) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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