Class: Gist::Post

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

Overview

Class that controls a posted Gist

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filenames, pub = false, description = nil, user = nil) ⇒ Post

Returns a new instance of Post.



7
8
9
10
11
12
13
14
15
# File 'lib/gist/post.rb', line 7

def initialize(filenames, pub = false, description = nil, user = nil)
  @user ||= user
  @public ||= pub
  @description ||= description ? description : ''
  @source_files ||= {}
  populate_source_files(filenames)
  @http = Net::HTTP.new(Gist::API_URL, Gist::API_PORT)
  @http.use_ssl = true
end

Instance Attribute Details

#source_filesObject

Returns the value of attribute source_files.



4
5
6
# File 'lib/gist/post.rb', line 4

def source_files
  @source_files
end

#userObject

Returns the value of attribute user.



5
6
7
# File 'lib/gist/post.rb', line 5

def user
  @user
end

Instance Method Details

#submit(stdin) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gist/post.rb', line 17

def submit(stdin)
  unless stdin.nil? then
    @source_files['STDIN'] = {
      'content' => stdin
    }
  end
  body = {
    'files' => @source_files,
    'public' => @public,
    'description' => @description
  }.to_json
  headers ||= {}
  unless @user.nil?
    headers['Authorization'] = "token #{@user.access_token}"
  end
  response = @http.post('/gists', body, headers)
  JSON.parse(response.body)['html_url']
end