Class: Gisterday::Gist
- Inherits:
-
Object
- Object
- Gisterday::Gist
- Defined in:
- lib/gist.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #add_file(file) ⇒ Object
- #format_response(response) ⇒ Object
- #get_token ⇒ Object
-
#initialize(options) ⇒ Gist
constructor
A new instance of Gist.
- #push_gist ⇒ Object
- #read_file(file) ⇒ Object
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 = @headers = {'User-Agent' => 'gisterday'} @body = { 'description' => @options[:description] || "Gist created using Gisterday", 'public' => true, 'files' => {} } end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
69 70 71 |
# File 'lib/gist.rb', line 69 def body @body end |
#headers ⇒ Object
Returns the value of attribute headers.
69 70 71 |
# File 'lib/gist.rb', line 69 def headers @headers end |
#options ⇒ Object
Returns the value of attribute options.
69 70 71 |
# File 'lib/gist.rb', line 69 def @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_token ⇒ Object
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_gist ⇒ Object
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 |