Class: Github::Gists

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

Defined Under Namespace

Classes: Comments

Constant Summary collapse

REQUIRED_GIST_INPUTS =
%w[
  description
  public
  files
  content
].freeze

Constants included from Request

Request::METHODS, Request::METHODS_WITH_BODIES

Constants included from Connection

Connection::ALLOWED_OPTIONS

Constants included from Constants

Constants::ACCEPT, Constants::ACCEPTED_OAUTH_SCOPES, Constants::ACCEPT_CHARSET, Constants::CACHE_CONTROL, Constants::CONTENT_LENGTH, Constants::CONTENT_TYPE, Constants::DATE, Constants::ETAG, Constants::HEADER_LAST, Constants::HEADER_LINK, Constants::HEADER_NEXT, Constants::LOCATION, Constants::META_FIRST, Constants::META_LAST, Constants::META_NEXT, Constants::META_PREV, Constants::META_REL, Constants::OAUTH_SCOPES, Constants::PARAM_PAGE, Constants::PARAM_PER_PAGE, Constants::PARAM_START_PAGE, Constants::RATELIMIT_LIMIT, Constants::RATELIMIT_REMAINING, Constants::SERVER, Constants::USER_AGENT

Constants included from MimeType

MimeType::MEDIA_LOOKUP

Instance Attribute Summary

Attributes inherited from API

#current_options

Attributes included from Authorization

#scopes

Instance Method Summary collapse

Methods inherited from API

#api_methods_in, #append_arguments, #arguments, inherited, #initialize, #method_missing, #process_basic_auth, #set, #setup, #with, #yield_or_eval

Methods included from RateLimit

#ratelimit, #ratelimit_remaining

Methods included from Request

#delete_request, #get_request, #patch_request, #post_request, #put_request, #request

Methods included from Connection

#caching?, #clear_cache, #connection, #default_middleware, #default_options, #stack

Methods included from MimeType

#lookup_media, #parse

Methods included from Authorization

#auth_code, #authenticated?, #authentication, #authorize_url, #basic_authed?, #client, #get_token

Constructor Details

This class inherits a constructor from Github::API

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Github::API

Instance Method Details

#comments(options = {}, &block) ⇒ Object

Access to Gists::Comments API



16
17
18
# File 'lib/github_api/gists.rb', line 16

def comments(options={}, &block)
  @comments ||= ApiFactory.new('Gists::Comments', current_options.merge(options), &block)
end

#create(*args) ⇒ Object

Create a gist

Inputs

<tt>:description</tt> - Optional string
<tt>:public</tt>  - Required boolean
<tt>:files</tt> - Required hash - Files that make up this gist.
   The key of which should be a required string filename and 
   the value another required hash with parameters:
      <tt>:content</tt> - Required string - File contents.

Examples

github = Github.new
github.gists.create
  'description' => 'the description for this gist',
  'public' => true,
  'files' => {
    'file1.txt' => {
       'content' => 'String file contents'
     }
  }


100
101
102
103
104
105
106
# File 'lib/github_api/gists.rb', line 100

def create(*args)
  arguments(args) do
    assert_required REQUIRED_GIST_INPUTS
  end

  post_request("/gists", arguments.params)
end

#delete(*args) ⇒ Object

Delete a gist

Examples

github = Github.new
github.gists.delete 'gist-id'


198
199
200
201
202
# File 'lib/github_api/gists.rb', line 198

def delete(*args)
  arguments(args, :required => [:gist_id])

  delete_request("/gists/#{gist_id}", arguments.params)
end

#edit(*args) ⇒ Object

Edit a gist

Inputs

<tt>:description</tt> - Optional string
<tt>:files</tt> - Optional hash - Files that make up this gist.
   The key of which should be a optional string filename and 
   the value another optional hash with parameters:
      <tt>:content</tt> - Updated string - Update file contents.
      <tt>:filename</tt> - Optional string - New name for this file.

Examples

github = Github.new :oauth_token => '...'
github.gists.edit 'gist-id',
  'description' => 'the description for this gist',
  'files' => {
    'file1.txt' => {
       'content' => 'Updated file contents'
     },
    'old_name.txt' => {
       'filename' => 'new_name.txt',
       'content' => 'modified contents'
     },
    'new_file.txt' => {
       'content' => 'a new file contents'
     },
     'delete_the_file.txt' => nil
  }


136
137
138
139
140
# File 'lib/github_api/gists.rb', line 136

def edit(*args)
  arguments(args, :required => [:gist_id])

  patch_request("/gists/#{gist_id}", arguments.params)
end

#fork(*args) ⇒ Object

Fork a gist

Examples

github = Github.new
github.gists.fork 'gist-id'


186
187
188
189
190
# File 'lib/github_api/gists.rb', line 186

def fork(*args)
  arguments(args, :required => [:gist_id])

  post_request("/gists/#{gist_id}/fork", arguments.params)
end

#get(*args) ⇒ Object Also known as: find

Get a single gist

Examples

github = Github.new
github.gists.get 'gist-id'


72
73
74
75
76
# File 'lib/github_api/gists.rb', line 72

def get(*args)
  arguments(args, :required => [:gist_id])

  get_request("/gists/#{gist_id}", arguments.params)
end

#list(*args) ⇒ Object Also known as: all

List a user’s gists.

Examples

github = Github.new
github.gists.list user: 'user-name'

List the authenticated user’s gists or if called anonymously, this will returns all public gists

Examples

github = Github.new :oauth_token => '...'
github.gists.list

List all public gists

github = Github.new
github.gists.list :public


38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/github_api/gists.rb', line 38

def list(*args)
  params = arguments(args).params

  response = if (user = params.delete('user'))
    get_request("/users/#{user}/gists", params)
  elsif args.map(&:to_s).include?('public')
    get_request("/gists/public", params)
  else
    get_request("/gists", params)
  end
  return response unless block_given?
  response.each { |el| yield el }
end

#star(*args) ⇒ Object

Star a gist

Examples

github = Github.new
github.gists.star 'gist-id'


148
149
150
151
152
# File 'lib/github_api/gists.rb', line 148

def star(*args)
  arguments(args, :required => [:gist_id])

  put_request("/gists/#{gist_id}/star", arguments.params)
end

#starred(*args) ⇒ Object

List the authenticated user’s starred gists

Examples

github = Github.new :oauth_token => '...'
github.gists.starred


59
60
61
62
63
64
# File 'lib/github_api/gists.rb', line 59

def starred(*args)
  arguments(args)
  response = get_request("/gists/starred", arguments.params)
  return response unless block_given?
  response.each { |el| yield el }
end

#starred?(*args) ⇒ Boolean

Check if a gist is starred

Examples

github = Github.new
github.gists.starred? 'gist-id'

Returns:

  • (Boolean)


172
173
174
175
176
177
178
# File 'lib/github_api/gists.rb', line 172

def starred?(*args)
  arguments(args, :required => [:gist_id])
  get_request("/gists/#{gist_id}/star", arguments.params)
  true
rescue Github::Error::NotFound
  false
end

#unstar(*args) ⇒ Object

Unstar a gist

Examples

github = Github.new
github.gists.unstar 'gist-id'


160
161
162
163
164
# File 'lib/github_api/gists.rb', line 160

def unstar(*args)
  arguments(args, :required => [:gist_id])

  delete_request("/gists/#{gist_id}/star", arguments.params)
end