Module: Gist

Extended by:
Gist
Included in:
Gist
Defined in:
lib/gist_store/gist.rb

Overview

You can use this class from other scripts with the greatest of ease.

>> Gist.read(gist_id)
Returns the body of gist_id as a string.

>> Gist.write(content)
Creates a gist from the string `content`. Returns the URL of the
new gist.

>> Gist.copy(string)
Copies string to the clipboard.

>> Gist.browse(url)
Opens URL in your default browser.

Constant Summary collapse

GIST_URL =
'https://api.github.com/gists/%s'
CREATE_URL =
'https://api.github.com/gists'
PROXY =
nil
PROXY_HOST =
PROXY ? PROXY.host : nil
PROXY_PORT =
PROXY ? PROXY.port : nil

Instance Method Summary collapse

Instance Method Details

#delete(id) ⇒ Object

Create a gist on gist.github.com



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
# File 'lib/gist_store/gist.rb', line 109

def delete(id)
  url = URI.parse(CREATE_URL)

  if PROXY_HOST
    proxy = Net::HTTP::Proxy(PROXY_HOST, PROXY_PORT)
    http  = proxy.new(url.host, url.port)
  else
    http = Net::HTTP.new(url.host, url.port)
  end

  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  http.ca_file = ca_cert

  req = Net::HTTP::Delete.new(GIST_URL % id)

  user, password = auth()
  if user && password
    req.basic_auth(user, password)
  end

  response = http.start{|h| h.request(req) }
  case response
  when Net::HTTPNoContent
    true
  else
    raise "Creating gist failed: #{response.code} #{response.message}"
  end
end

#read(gist_id) ⇒ Object

Given a gist id, returns its content.



140
141
142
143
# File 'lib/gist_store/gist.rb', line 140

def read(gist_id)
  data = read_raw(gist_id)
  data["files"].map{|name, content| content['content'] }.join("\n\n")
end

#read_raw(gist_id) ⇒ Object



145
146
147
# File 'lib/gist_store/gist.rb', line 145

def read_raw(gist_id)
  data = JSON.parse(open(GIST_URL % gist_id).read)
end

#update(id, files) ⇒ Object

Create a gist on gist.github.com



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
# File 'lib/gist_store/gist.rb', line 75

def update(id, files)
  url = URI.parse(CREATE_URL)

  if PROXY_HOST
    proxy = Net::HTTP::Proxy(PROXY_HOST, PROXY_PORT)
    http  = proxy.new(url.host, url.port)
  else
    http = Net::HTTP.new(url.host, url.port)
  end

  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  http.ca_file = ca_cert

  req = Net::HTTP::Patch.new(GIST_URL % id)
  req.body = JSON.generate(data(files, nil, nil))

  user, password = auth()
  if user && password
    req.basic_auth(user, password)
  end

  response = http.start{|h| h.request(req) }
  case response
  when Net::HTTPOK
    JSON.parse(response.body)['html_url']
  else
    raise "Updating gist failed: #{response.code} #{response.message}"
  end
end

#write(files, private_gist = false, description = nil) ⇒ Object

Create a gist on gist.github.com



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gist_store/gist.rb', line 43

def write(files, private_gist = false, description = nil)
  url = URI.parse(CREATE_URL)

  if PROXY_HOST
    proxy = Net::HTTP::Proxy(PROXY_HOST, PROXY_PORT)
    http  = proxy.new(url.host, url.port)
  else
    http = Net::HTTP.new(url.host, url.port)
  end

  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  http.ca_file = ca_cert

  req = Net::HTTP::Post.new(url.path)
  req.body = JSON.generate(data(files, private_gist, description))

  user, password = auth()
  if user && password
    req.basic_auth(user, password)
  end

  response = http.start{|h| h.request(req) }
  case response
  when Net::HTTPCreated
    JSON.parse(response.body)['html_url']
  else
    raise "Creating gist failed: #{response.code} #{response.message}"
  end
end