Class: Pastee
- Inherits:
-
Object
- Object
- Pastee
- Defined in:
- lib/pastee.rb,
lib/pastee/paste.rb,
lib/pastee/errors.rb,
lib/pastee/syntax.rb
Defined Under Namespace
Classes: Errors, Paste, Syntax
Constant Summary collapse
- BASE_URL =
'https://api.paste.ee/v1'.freeze
Instance Method Summary collapse
-
#delete(id) ⇒ Boolean
Delete a paste.
-
#get_paste(id) ⇒ Pastee::Paste
Gets paste information from its string ID.
-
#get_syntax(id) ⇒ Pastee::Syntax
Obtains information for a Pastee syntax from its integer ID.
-
#get_user_type ⇒ String
Get the user type for the currently authenticated user.
-
#initialize(api_key) ⇒ Pastee
constructor
Creates a new instance of Pastee.
-
#list_syntaxes ⇒ Array<Pastee::Syntax>
Obtains a list of valid Pastee syntaxes.
-
#submit(paste) ⇒ String
Submits a new paste to Pastee.
-
#submit_simple(name, text, encrypted = false) ⇒ String
Simple submission method.
Constructor Details
#initialize(api_key) ⇒ Pastee
Creates a new instance of Pastee.
11 12 13 14 15 16 17 18 |
# File 'lib/pastee.rb', line 11 def initialize(api_key) @client = HTTPClient.new( default_header: { 'X-Auth-Token' => api_key, 'Content-Type' => 'application/json' } ) end |
Instance Method Details
#delete(id) ⇒ Boolean
Delete a paste.
111 112 113 114 115 116 117 |
# File 'lib/pastee.rb', line 111 def delete(id) uri = URI.parse("#{BASE_URL}/pastes/#{id}") response = JSON.parse(@client.delete(uri).body) return true if response['success'] throw_error(response) end |
#get_paste(id) ⇒ Pastee::Paste
Gets paste information from its string ID.
47 48 49 50 51 52 53 |
# File 'lib/pastee.rb', line 47 def get_paste(id) uri = URI.parse("#{BASE_URL}/pastes/#{id}") response = JSON.parse(@client.get(uri).body) return Pastee::Paste.new(response['paste']) if response['success'] throw_error(response) end |
#get_syntax(id) ⇒ Pastee::Syntax
Obtains information for a Pastee syntax from its integer ID.
24 25 26 27 28 29 30 |
# File 'lib/pastee.rb', line 24 def get_syntax(id) uri = URI.parse("#{BASE_URL}/syntaxes/#{id}") response = JSON.parse(@client.get(uri).body) return Pastee::Syntax.new(response['syntax']) if response['success'] throw_error(response) end |
#get_user_type ⇒ String
Get the user type for the currently authenticated user.
122 123 124 125 126 127 128 |
# File 'lib/pastee.rb', line 122 def get_user_type uri = URI.parse("#{BASE_URL}/users/info") response = JSON.parse(@client.get(uri).body) return response['type'] if response['success'] throw_error(response) end |
#list_syntaxes ⇒ Array<Pastee::Syntax>
Obtains a list of valid Pastee syntaxes.
35 36 37 38 39 40 41 |
# File 'lib/pastee.rb', line 35 def list_syntaxes uri = URI.parse("#{BASE_URL}/syntaxes") response = JSON.parse(@client.get(uri).body) return response['syntaxes'].map { |obj| Pastee::Syntax.new(obj) } if response['success'] throw_error(response) end |
#submit(paste) ⇒ String
Submits a new paste to Pastee. Build a paste using Pastee::Paste and Pastee::Section and submit it. This new way of creating and submitting pastes is a little more convoluted than with the legacy (non-sectional) API, so use the following example as a guideline. #submit_simple is simpler and should be used for simple single-section pastes.
87 88 89 90 91 92 93 |
# File 'lib/pastee.rb', line 87 def submit(paste) uri = URI.parse("#{BASE_URL}/pastes") response = JSON.parse(@client.request(:post, uri, body: JSON.dump(paste.to_h)).body) return response['id'] if response['success'] throw_error(response) end |
#submit_simple(name, text, encrypted = false) ⇒ String
Simple submission method. Transforms a name and text into a proper single-Section Paste object and submits it.
101 102 103 104 105 |
# File 'lib/pastee.rb', line 101 def submit_simple(name, text, encrypted = false) section = Pastee::Paste::Section.new(name: name, contents: text) paste = Pastee::Paste.new(description: name, sections: [section], encrypted: encrypted) submit(paste) end |