Class: Gists

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

Constant Summary collapse

API_ROOT =
"api.github.com"

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Gists

Returns a new instance of Gists.



8
9
10
11
# File 'lib/gists.rb', line 8

def initialize(username, password)
  @username = username
  @password = password
end

Instance Method Details

#get(resource) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/gists.rb', line 13

def get(resource)
  Net::HTTP.start(API_ROOT, :use_ssl => true) do |http|
    req = Net::HTTP::Get.new(resource)
    req.basic_auth @username, @password
    response = http.request(req)
    
    JSON.parse(response.body, :symbolize_names => true)
  end
end

#get_allObject



34
35
36
# File 'lib/gists.rb', line 34

def get_all
  get("/gists")
end

#get_gist(id) ⇒ Object



38
39
40
# File 'lib/gists.rb', line 38

def get_gist(id)
  Gist.json_create(get("/gists/#{id}"))
end

#post(resource, body) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/gists.rb', line 23

def post(resource, body)
  Net::HTTP.start(API_ROOT, :use_ssl => true) do |http|
    req = Net::HTTP::Post.new(resource)
    req.basic_auth @username, @password
    req.body = body
    response = http.request(req)

    JSON.parse(response.body, :symbolize_names => true)
  end
end

#save(gist) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/gists.rb', line 42

def save(gist)
  if not gist.id.nil? then
    post "/gists/#{gist.id}", gist.to_json
  else
    res = post "/gists", gist.to_json
    gist.id = res["id"]
    res
  end
end