Class: Gisterday::Gist

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Gist

Returns a new instance of Gist.



71
72
73
74
75
76
77
78
79
# File 'lib/gist.rb', line 71

def initialize(options)
  @options = options
  @headers = {'User-Agent' => 'gisterday'}
  @body = {
    'description' => @options[:description] || "Gist created using Gisterday",
    'public' => true,
    'files' => {}
    }
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



69
70
71
# File 'lib/gist.rb', line 69

def body
  @body
end

#headersObject

Returns the value of attribute headers.



69
70
71
# File 'lib/gist.rb', line 69

def headers
  @headers
end

#optionsObject

Returns the value of attribute options.



69
70
71
# File 'lib/gist.rb', line 69

def options
  @options
end

Instance Method Details

#add_file(file) ⇒ Object



93
94
95
# File 'lib/gist.rb', line 93

def add_file(file)
  @body['files'][file] = {"content" => read_file(file)}
end

#format_response(response) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/gist.rb', line 106

def format_response(response)
  STDOUT.puts %Q{

    #{JSON.parse(response.body) if @options[:verbose]}


    New Gist created at:
    #{JSON.parse(response.body)['html_url']}
  }
end

#get_tokenObject



85
86
87
88
89
90
91
# File 'lib/gist.rb', line 85

def get_token
  begin
    read_file(TOKEN_LOCATION)
  rescue
    false
  end
end

#push_gistObject



97
98
99
100
101
102
103
104
# File 'lib/gist.rb', line 97

def push_gist
  if get_token && @options[:anonymous] == nil
    @headers['Authorization'] = "token #{get_token}"
  end

  response = HTTParty.post(GIST_URL,:body => @body.to_json, :headers => @headers) 
  format_response(response)
end

#read_file(file) ⇒ Object



81
82
83
# File 'lib/gist.rb', line 81

def read_file(file)
  File.read(file)
end