Class: Githubris::Gist

Inherits:
Base
  • Object
show all
Includes:
Publicity
Defined in:
lib/githubris/gist.rb

Instance Method Summary collapse

Methods included from Publicity

#privatize!, #public?, #publicize!

Methods inherited from Base

#==, #initialize, #swap_attributes

Constructor Details

This class inherits a constructor from Githubris::Base

Instance Method Details

#delete!Object



29
30
31
# File 'lib/githubris/gist.rb', line 29

def delete!
  @api.delete_gist(@attributes[:id])
end

#historyObject



33
34
35
36
37
# File 'lib/githubris/gist.rb', line 33

def history
  @attributes[:history].map do |gist_data|
    Githubris::Gist.new(gist_data)
  end
end

#new?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
# File 'lib/githubris/gist.rb', line 67

def new?
  reload
rescue Githubris::Error
  true
else
  false
end

#reloadObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/githubris/gist.rb', line 39

def reload
  if @attributes[:id]
    gist = @api.get_gist @attributes[:id]
  elsif @attributes[:url]
    gist = Githubris::Gist.new @api._get @attributes[:url]
  else
    raise Githubris::Error, 'Missing a unique identifier, such as an id or a url'
  end
  swap_attributes gist
end

#saveObject



50
51
52
53
54
55
56
57
# File 'lib/githubris/gist.rb', line 50

def save
  if new?
    saved_attributes = @api.post_gist(saving_attributes)
  else
    saved_attributes = @api.patch_gist(@attributes[:id], saving_attributes)
  end
  swap_attributes saved_attributes
end

#saving_attributesObject



59
60
61
62
63
64
65
# File 'lib/githubris/gist.rb', line 59

def saving_attributes
  {
    :description => @attributes[:description],
    :public => @attributes[:public],
    :files => @attributes[:files]
  }
end

#star!Object



13
14
15
# File 'lib/githubris/gist.rb', line 13

def star!
  @api.put_gist_star(@attributes[:id])
end

#starred?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/githubris/gist.rb', line 21

def starred?
  @api.get_gist_starred?(@attributes[:id])
end

#unstar!Object



17
18
19
# File 'lib/githubris/gist.rb', line 17

def unstar!
  @api.delete_gist_star(@attributes[:id])
end

#unstarred?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/githubris/gist.rb', line 25

def unstarred?
  !starred?
end

#userObject



9
10
11
# File 'lib/githubris/gist.rb', line 9

def user
  @user ||= Githubris::User.new(@attributes[:user])
end