Class: Giddyup::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/giddyup/util.rb

Class Method Summary collapse

Class Method Details

.post_files(files) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/giddyup/util.rb', line 8

def self.post_files(files)
  manifest_files = files.inject({}) do |acc, file|
    acc.merge!({File.basename(file) => {"content" => File.open(file).readlines.inject(""){ |acc, s| acc << s }}})
  end

  params = {
    "description" => "This gist was ★automagically★ generated by giddyup™",
    "public" => true,
    "files" => manifest_files
  }

  uri = URI("https://api.github.com/gists")
  req = Net::HTTP::Post.new(uri.to_s)

  if File.exists?("~/.g-up/credentials")
    auth = YAML::load("~/.g-up/credentials")
    req.basic_auth(auth[:username], auth[:password])
  end

  req.body = params.to_json
  req["content-type"] = "application/json"
  Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
    request = http.request(req)
    puts JSON.parse(request.body)["html_url"]
  end
end