pastee 2.0.3 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 9e81f18c945bad3c976b40b33c5684fbafedbe73
4
- data.tar.gz: 2274cb27b32854de4d397257b65d05598744d7f5
2
+ SHA256:
3
+ metadata.gz: 066d69e5b280295c48bbd53cf7660c61b80a6036465e6c2f38a7a4fa5805e1d3
4
+ data.tar.gz: 120891e5742804d8add5d9d938ec61f9105e10e78b17e1fa04d3d3a28e6e36f4
5
5
  SHA512:
6
- metadata.gz: 75ea743d1b7ebc346d80ffd352537726d70c74f14e64a1f89ccaf8444a9051fad4d6cb2bb0cfba0fa48217721244e29dc8c84acce992a45d1b46c64976c07cac
7
- data.tar.gz: 00ceb24ed9a5878676ddfbd1a573cbaab9af738b405d842ce681a93a3b22a38db66c8338c46a29aaf9a44bbfa3e64c76044b1d541c3b7c356226340767ba0059
6
+ metadata.gz: d16b22f8a8b135ac6be68c8a9a5fcca6f10328661dd43d47a5b0dd47c2a744414693c8e8f51b475d9cb105832fe6e612e3f4b97c8ce955c9ba605a89cc720edd
7
+ data.tar.gz: 9173147b1cf006c63ecf2963b40b152f635ea3ea4f5a18efad12c76264022378b50c4d1ea759457868db22d6f7a7e6244f56af15ad600158beef9eb2f5603fc3
@@ -1,4 +1,8 @@
1
1
  # Changelog
2
+ ## Version 3
3
+ ### Version 3.0.0
4
+ * Update for new Pastee API. Pretty much a complete rewrite.
5
+
2
6
  ## Version 2
3
7
  ### Version 2.0.3
4
8
  * Use pessimistic version requirements.
@@ -1,55 +1,158 @@
1
1
  require 'httpclient'
2
2
  require 'json'
3
- require_relative 'errors'
3
+ require_relative 'pastee/errors'
4
+ require_relative 'pastee/paste'
5
+ require_relative 'pastee/syntax'
4
6
 
5
- # This class contains interfaces to the standard Pastee API. For the beta API,
6
- # use the PasteeBeta class.
7
7
  class Pastee
8
+ BASE_URL = 'https://api.paste.ee/v1'.freeze
8
9
  # Creates a new instance of Pastee.
9
- # @param api_key
10
- # @param use_ssl [Boolean] Whether to use a secure SSL connection.
11
- def initialize(api_key, use_ssl = true)
12
- @url = use_ssl ? 'https://paste.ee/' : 'http://paste.ee/'
13
- @client = HTTPClient.new
14
- @key = api_key
15
- end
16
-
17
- # Submits a POST request to the URL.
18
- # @param paste [String] The raw paste content.
19
- # @param description [String] The description of the paste.
20
- # @param encrypted [Boolean] The encryption
21
- # @param expire [Fixnum] The number of minutes until it expires.
22
- # @param language [String] The language to use for syntax highlighting.
10
+ # @param api_key [String] The API key for this application.
11
+ def initialize(api_key)
12
+ @client = HTTPClient.new(
13
+ default_header: {
14
+ 'X-Auth-Token' => api_key,
15
+ 'Content-Type' => 'application/json'
16
+ }
17
+ )
18
+ end
19
+
20
+ # Obtains information for a Pastee syntax from its integer ID.
21
+ # @param id [Integer] The ID for this syntax.
22
+ # @return [Pastee::Syntax] The syntax object representative of this integer ID.
23
+ # @raise (see #throw_error)
24
+ def get_syntax(id)
25
+ uri = URI.parse("#{BASE_URL}/syntaxes/#{id}")
26
+ response = JSON.parse(@client.get(uri).body)
27
+ return Pastee::Syntax.new(response['syntax']) if response['success']
28
+
29
+ throw_error(response)
30
+ end
31
+
32
+ # Obtains a list of valid Pastee syntaxes.
33
+ # @return [Array<Pastee::Syntax>] A list of syntax objects.
34
+ # @raise (see #throw_error)
35
+ def list_syntaxes
36
+ uri = URI.parse("#{BASE_URL}/syntaxes")
37
+ response = JSON.parse(@client.get(uri).body)
38
+ return response['syntaxes'].map { |obj| Pastee::Syntax.new(obj) } if response['success']
39
+
40
+ throw_error(response)
41
+ end
42
+
43
+ # Gets paste information from its string ID.
44
+ # @param id [String] The paste ID to obtain information for.
45
+ # @return [Pastee::Paste] The paste that is tied to the provided ID.
46
+ # @raise (see #throw_error)
47
+ def get_paste(id)
48
+ uri = URI.parse("#{BASE_URL}/pastes/#{id}")
49
+ response = JSON.parse(@client.get(uri).body)
50
+ return Pastee::Paste.new(response['paste']) if response['success']
51
+
52
+ throw_error(response)
53
+ end
54
+
55
+ # Submits a new paste to Pastee. Build a paste using Pastee::Paste and Pastee::Section and submit it. This new way of
56
+ # creating and submitting pastes is a little more convoluted than with the legacy (non-sectional) API, so use the
57
+ # following example as a guideline. {#submit_simple} is simpler and should be used for simple single-section pastes.
58
+ # @example
59
+ # section1 = Pastee::Paste::Section.new(
60
+ # name: 'section 1', # syntax defaults to autodetect
61
+ # contents: 'Some text!'
62
+ # )
63
+ # section2 = Pastee::Paste::Section.new(
64
+ # name: 'section 2',
65
+ # syntax: 'ruby',
66
+ # contents: File.read('lib/pastee.rb')
67
+ # )
68
+ # section3 = Pastee::Paste::Section.new(
69
+ # name: 'section 3',
70
+ # syntax: 'markdown',
71
+ # contents: File.read('README.md')
72
+ # )
73
+ # paste = Pastee::Paste.new(
74
+ # encrypted: true,
75
+ # description: 'super secret paste',
76
+ # sections: [
77
+ # section1,
78
+ # section2,
79
+ # section3
80
+ # ]
81
+ # )
82
+ # pastee.submit(paste)
83
+ # @param paste [Pastee::Paste] The paste (see example)
23
84
  # @return [String] The paste ID.
24
- def submit(paste, description, encrypted = false, expire = 0,
25
- language = nil)
26
- uri = URI.parse(URI.encode("#{@url}/api"))
27
- params = {
28
- key: @key,
29
- descripton: description,
30
- paste: paste,
31
- expire: expire,
32
- format: 'json'
33
- }
34
- params[:encrypted] = encrypted ? 1 : 0
35
- params[:language] = language unless language.nil?
36
- response = @client.post(uri, params)
37
- json = JSON.parse(response.body)
38
- if json['status'] == 'error'
39
- throw_error(json['error'])
40
- else
41
- json['paste']['id']
42
- end
85
+ # @raise (see #throw_error)
86
+ # @see #submit_simple
87
+ def submit(paste)
88
+ uri = URI.parse("#{BASE_URL}/pastes")
89
+ response = JSON.parse(@client.request(:post, uri, body: JSON.dump(paste.to_h)).body)
90
+ return response['id'] if response['success']
91
+
92
+ throw_error(response)
93
+ end
94
+
95
+ # Simple submission method. Transforms a name and text into a proper single-Section Paste object and submits it.
96
+ # @param name [String] The paste's name.
97
+ # @param text [String] The paste text.
98
+ # @param encrypted [Boolean] Whether this paste should be treated as encrypted by pastee.
99
+ # @return (see #submit)
100
+ # @raise (see #throw_error)
101
+ def submit_simple(name, text, encrypted = false)
102
+ section = Pastee::Paste::Section.new(name: name, contents: text)
103
+ paste = Pastee::Paste.new(description: name, sections: [section], encrypted: encrypted)
104
+ submit(paste)
105
+ end
106
+
107
+ # Delete a paste.
108
+ # @param id [String] The paste ID to delete.
109
+ # @return [Boolean] True if it was successfully deleted.
110
+ # @raise (see #throw_error)
111
+ def delete(id)
112
+ uri = URI.parse("#{BASE_URL}/pastes/#{id}")
113
+ response = JSON.parse(@client.delete(uri).body)
114
+ return true if response['success']
115
+
116
+ throw_error(response)
117
+ end
118
+
119
+ # Get the user type for the currently authenticated user.
120
+ # @return [String] The user type.
121
+ # @raise (see #throw_error)
122
+ def get_user_type
123
+ uri = URI.parse("#{BASE_URL}/users/info")
124
+ response = JSON.parse(@client.get(uri).body)
125
+ return response['type'] if response['success']
126
+
127
+ throw_error(response)
43
128
  end
44
129
 
45
130
  private
46
131
 
47
- def throw_error(error)
48
- case error
49
- when 'error_no_key' then fail Pastee::Errors::NoKeyError
50
- when 'error_no_paste' then fail Pastee::Errors::NoPasteError
51
- when 'error_invalid_key' then fail Pastee::Errors::InvalidKeyError
52
- when 'error_invalid_language' then fail Pastee::Errors::InvalidLanguageError
132
+ # Determines and raises the right error according to the error code provided by the pastee API.
133
+ # @param response [Hash] The response object returned by the pastee API and parsed.
134
+ # @raise [Pastee::Errors::BadRequestError] on 400, 404, 405, and 406.
135
+ # @raise [Pastee::Errors::InvalidKeyError] on 401.
136
+ # @raise [Pastee::Errors::RequiresUserApplicationError] on 403.
137
+ # @raise [Pastee::Errors::TooManyRequestsError] on 429.
138
+ # @raise [Pastee::Errors::InternalServerError] on 500.
139
+ # @raise [Pastee::Errors::ServiceUnavailableError] on 503.
140
+ # @raise [Pastee::Errors::UnknownError] if the error code is not recognized.
141
+ def throw_error(response)
142
+ error = response['errors'][0]
143
+ error_code = error['code']
144
+ error_msg = error['message']
145
+ case error_code
146
+ when 400 then raise Pastee::Errors::BadRequestError.new(error_code)
147
+ when 401 then raise Pastee::Errors::InvalidKeyError
148
+ when 403 then raise Pastee::Errors::RequiresUserApplicationError
149
+ when 404 then raise Pastee::Errors::BadRequestError.new(error_code)
150
+ when 405 then raise Pastee::Errors::BadRequestError.new(error_code)
151
+ when 406 then raise Pastee::Errors::BadRequestError.new(error_code)
152
+ when 429 then raise Pastee::Errors::TooManyRequestsError
153
+ when 500 then raise Pastee::Errors::InternalServerError
154
+ when 503 then raise Pastee::Errors::ServiceUnavailableError
155
+ else raise Pastee::Errors::UnknownError.new(error_code, error_msg)
53
156
  end
54
157
  end
55
158
  end
@@ -0,0 +1,61 @@
1
+ class Pastee
2
+ class Errors
3
+ # Error for an unrecognized error code from the API.
4
+ class UnknownError < StandardError
5
+ def initialize(error_code, error_msg)
6
+ @error_code = error_code
7
+ @error_msg = error_msg
8
+ end
9
+
10
+ def message
11
+ "Error code #{@error_code} (unknown): #{@error_msg}. Please report this unknown error to the developers of the pastee gem."
12
+ end
13
+ end
14
+
15
+ # Error for error codes 400, 404, 405, and 406.
16
+ class BadRequestError < StandardError
17
+ def initialize(code)
18
+ @code = code
19
+ end
20
+
21
+ def message
22
+ "Error code #{@code}: Bad request."
23
+ end
24
+ end
25
+
26
+ # Error for error code 401.
27
+ class InvalidKeyError < StandardError
28
+ def message
29
+ 'Error code 401: The key provided was invalid.'
30
+ end
31
+ end
32
+
33
+ # Error for error code 403.
34
+ class RequiresUserApplicationError < StandardError
35
+ def message
36
+ 'Error code 403: This resource requires a UserApplication.'
37
+ end
38
+ end
39
+
40
+ # Error for error code 429.
41
+ class TooManyRequestsError < StandardError
42
+ def message
43
+ 'Error code 429: Too many pastes have been submitted with the given API key.'
44
+ end
45
+ end
46
+
47
+ # Error for error code 500.
48
+ class InternalServerError < StandardError
49
+ def message
50
+ 'Error code 500: There was a problem with the Pastee server.'
51
+ end
52
+ end
53
+
54
+ # Error for error code 503.
55
+ class ServiceUnavailableError < StandardError
56
+ def message
57
+ 'Error code 503: The service is temporarily offline for maintenance.'
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,127 @@
1
+ class Pastee
2
+ # Wrapper class for pastes.
3
+ class Paste
4
+ # @return [Integer] The paste's ID.
5
+ attr_reader :id
6
+
7
+ # @return [String] The paste's description.
8
+ attr_reader :description
9
+
10
+ # @return [Integer] How many views the paste has.
11
+ attr_reader :views
12
+
13
+ # @return [DateTime] When this paste was created.
14
+ attr_reader :created_at
15
+
16
+ # @return [DateTime, NilClass] When this paste will expire, or nil if it never expires.
17
+ attr_reader :expires_at
18
+
19
+ # @return [Array<Pastee::Section>] An array of Sections for this paste.
20
+ attr_reader :sections
21
+
22
+ # Create a new Paste object. Used when creating new pastes and when getting existing pastes. For creating new
23
+ # pastes, you only need to provide the description and sections, and optionally encrypted (defaults to false). The
24
+ # rest will be created automatically by the pastee API. ID, views, created_at, and expires_at are created by pastee,
25
+ # they are only here for receiving paste objects.
26
+ # @param opts [Hash] Options hash. Keys can either be strings or symbols — they will be converted to symbols.
27
+ # @option opts [String] :id Paste ID.
28
+ # @option opts [Boolean] :encrypted Whether this paste is treated as encrypted. Pastee does not actually encrypt
29
+ # pastes, this only affects how it appears on the website.
30
+ # @option opts [String] :description The paste's description or name.
31
+ # @option opts [Integer] :views How many times this paste has been viewed.
32
+ # @option opts [String] :created_at When this paste was created, in string form. Is parsed into a DateTime.
33
+ # @option opts [String] :expires_at When this paste expires, in string form. Is parsed into a DateTime.
34
+ # @option opts [Array] :sections An array either of Section objects (if you are creating a new paste) or a hash
35
+ # object (returned by the pastee API) to be turned into a Section object.
36
+ def initialize(opts = {})
37
+ # Standardize keys so that both the pastee API (which uses strings) and pastee-rb consumers (who use symbols) can
38
+ # both use this method.
39
+ opts = Hash[opts.map { |k, v| [k.to_sym, v] }]
40
+
41
+ @id = opts[:id]
42
+ @encrypted = opts[:encrypted] || false
43
+ @description = opts[:description]
44
+ @views = opts[:views]
45
+ @created_at = DateTime.parse(opts[:created_at]) if opts[:created_at]
46
+ @expires_at = DateTime.parse(opts[:expires_at]) if opts[:expires_at]
47
+ # For creating our own pastes
48
+ @sections = opts[:sections][0].is_a?(Section) ? opts[:sections] : opts[:sections].map { |o| Section.new(o) }
49
+ end
50
+
51
+ # @return [Boolean] Whether this paste is encrypted.
52
+ def encrypted?
53
+ @encrypted
54
+ end
55
+
56
+ # Converts this to a hash object. Used in submitting pastes.
57
+ # @return [Hash]
58
+ def to_h
59
+ hash = {
60
+ description: description,
61
+ sections: sections.map(&:to_h)
62
+ }
63
+ hash[:id] = id if id
64
+ hash[:encrypted] = encrypted?
65
+ hash[:views] = views if views
66
+ hash[:created_at] = created_at if created_at
67
+ hash[:expires_at] = expires_at if expires_at
68
+
69
+ hash
70
+ end
71
+
72
+ # Wrapper class for paste sections.
73
+ class Section
74
+ # @return [Integer] The ID for this section.
75
+ attr_reader :id
76
+
77
+ # @return [String] The short name of the syntax used in this section.
78
+ attr_reader :syntax
79
+
80
+ # @return [String] The name of the section.
81
+ attr_reader :name
82
+
83
+ # @return [String] Contents of the section.
84
+ attr_reader :contents
85
+
86
+ # @return [Integer] Size of the section in bytes.
87
+ attr_reader :size
88
+
89
+ # Create a new Section object. Used when creating new pastes and when getting existing pastes. For creating new
90
+ # pastes, you only need to provide the name and contents and optionally syntax. The rest will be created
91
+ # automatically by the pastee API or by our {#to_h} method (syntax defaults to "autodetect"). ID and size are
92
+ # created by pastee, they are only here for receiving paste objects.
93
+ # @param opts [Hash] Options hash. Keys can either be strings or symbols — they will be converted to symbols.
94
+ # @option opts [Integer] :id Section ID.
95
+ # @option opts [String] :syntax The syntax short name for this section.
96
+ # @option opts [String] :name The name of this section.
97
+ # @option opts [String] :contents The contents of this section.
98
+ # @option opts [Integer] :size The size of this section.
99
+ def initialize(opts = {})
100
+ # Standardize keys so that both the pastee API (which uses strings) and pastee-rb consumers (who use symbols) can
101
+ # both use this method.
102
+ opts = Hash[opts.map { |k, v| [k.to_sym, v] }]
103
+
104
+ @id = opts[:id]
105
+ @syntax = opts[:syntax]
106
+ @name = opts[:name]
107
+ @contents = opts[:contents]
108
+ @size = opts[:size]
109
+ end
110
+
111
+ # Converts this to a hash object. Used in submitting pastes.
112
+ # @return [Hash]
113
+ def to_h
114
+ hash = {
115
+ name: name,
116
+ contents: contents
117
+ }
118
+ hash[:id] = id if id
119
+ hash[:syntax] = syntax || 'autodetect'
120
+ hash[:size] = size if size
121
+
122
+ hash
123
+ end
124
+ end
125
+ end
126
+ end
127
+
@@ -0,0 +1,23 @@
1
+ class Pastee
2
+ class Syntax
3
+ # @return [Integer]
4
+ attr_reader :id
5
+
6
+ # @return [String]
7
+ attr_reader :short_name
8
+
9
+ # @return [String]
10
+ attr_reader :full_name
11
+
12
+ # Creates a new object representing a Syntax on the Pastee website.
13
+ # @param opts [Hash<String, Object>] The options hash. This is usually obtained by the Pastee API.
14
+ # @option opts [Integer] 'id' The integer ID for this syntax.
15
+ # @option opts [String] 'short' The shortened name for the syntax.
16
+ # @option opts [String] 'name' The full name for the syntax.
17
+ def initialize(opts = {})
18
+ @id = opts['id']
19
+ @short_name = opts['short']
20
+ @full_name = opts['name']
21
+ end
22
+ end
23
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pastee
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.3
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eli Foster
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-02 00:00:00.000000000 Z
11
+ date: 2020-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpclient
@@ -32,15 +32,18 @@ extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
34
  - CHANGELOG.md
35
- - lib/errors.rb
36
35
  - lib/pastee.rb
37
- - lib/pastee_beta.rb
38
- - lib/syntax.rb
36
+ - lib/pastee/errors.rb
37
+ - lib/pastee/paste.rb
38
+ - lib/pastee/syntax.rb
39
39
  homepage: https://github.com/elifoster/pasteee-rb
40
40
  licenses:
41
41
  - MIT
42
42
  metadata:
43
- issue_tracker: https://github.com/elifoster/pastee-rb/issues
43
+ bug_tracker_uri: https://github.com/elifoster/pastee-rb/issues
44
+ changelog_uri: https://elifoster.github.io/pastee-rb/file.CHANGELOG.html
45
+ documentation_uri: https://elifoster.github.io/pastee-rb
46
+ source_code_uri: https://github.com/elifoster/pastee-rb
44
47
  post_install_message:
45
48
  rdoc_options: []
46
49
  require_paths:
@@ -56,8 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
59
  - !ruby/object:Gem::Version
57
60
  version: '0'
58
61
  requirements: []
59
- rubyforge_project:
60
- rubygems_version: 2.5.1
62
+ rubygems_version: 3.1.3
61
63
  signing_key:
62
64
  specification_version: 4
63
65
  summary: A simple interface to the paste.ee API
@@ -1,100 +0,0 @@
1
- class Pastee
2
- class Errors
3
- class NoKeyError < StandardError
4
- def message
5
- 'Error code 1: No key was provided.'
6
- end
7
- end
8
-
9
- class NoPasteError < StandardError
10
- def message
11
- 'Error code 2: No paste content was provided.'
12
- end
13
- end
14
-
15
- class InvalidKeyError < StandardError
16
- def message
17
- 'Error code 3: The key provided was invalid.'
18
- end
19
- end
20
-
21
- class InvalidLanguageError < StandardError
22
- def message
23
- 'Error code 4: The syntax highlighting language provided was invalid.'
24
- end
25
- end
26
-
27
- class Beta
28
- class MustUseBetaError < StandardError
29
- def message
30
- 'You must be using the Beta API for this action.'
31
- end
32
- end
33
-
34
- class UnauthorizedError < StandardError
35
- def message
36
- 'The provided API key is incorrect.'
37
- end
38
- end
39
-
40
- class UndefinedApplicationKeyError < StandardError
41
- def message
42
- 'You must provide an application key.'
43
- end
44
- end
45
-
46
- class InvalidAppKeyError < StandardError
47
- def message
48
- 'The application key provided is invalid.'
49
- end
50
- end
51
-
52
- class InvalidFieldError < StandardError
53
- def initialize(msg, field)
54
- super(msg)
55
- @field = field
56
- end
57
-
58
- def message
59
- msg
60
- end
61
- end
62
-
63
- class RequiresUserApplicationError < StandardError
64
- def message
65
- 'This resource requires a UserApplication.'
66
- end
67
- end
68
-
69
- class InvalidMethodError < StandardError
70
- def message
71
- 'The endpoint was requested with an invalid method.'
72
- end
73
- end
74
-
75
- class InvalidFormatError < StandardError
76
- def message
77
- 'The request was in a format other than XML or JSON.'
78
- end
79
- end
80
-
81
- class TooManyRequestsError < StandardError
82
- def message
83
- 'Too many pastes have been submitted with the given API key.'
84
- end
85
- end
86
-
87
- class InternalServerError < StandardError
88
- def message
89
- 'There was a problem with the Pastee server.'
90
- end
91
- end
92
-
93
- class ServiceUnavailableError < StandardError
94
- def message
95
- 'The service is temporarily offline for maintenance.'
96
- end
97
- end
98
- end
99
- end
100
- end
@@ -1,190 +0,0 @@
1
- require 'httpclient'
2
- require 'json'
3
- require_relative 'syntax'
4
-
5
- # Base class for the Pastee Beta API. Provides interfaces to all of the beta
6
- # APIs. Will eventually be transferred to the Pastee class once the API is
7
- # out of beta.
8
- # @author Eli Clemente Gordillo Foster
9
- # @since 2.0.0
10
- class PasteeBeta
11
- URL = 'https://api.beta.paste.ee'.freeze
12
-
13
- # Initializes the PasteeBeta instance.
14
- # @author Eli Clemente Gordillo Foster
15
- # @param [String] The Application or UserApplication key. This can be changed
16
- # with #set_new_key.
17
- # @return [void]
18
- def initialize(api_key)
19
- @key = api_key
20
- @client = HTTPClient.new
21
- @client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
22
- end
23
-
24
- # Resets the API key to a new value. Useful for when, for example, the
25
- # UserApplication key is needed and not the base Application key.
26
- # @author Eli Clemente Gordillo Foster
27
- # @param [String] The new key to use.
28
- # @return [void]
29
- def set_new_key(new_key)
30
- @key = new_key
31
- end
32
-
33
- # Submits a new paste to the Pastee Beta website.
34
- # @author Eli Clemente Gordillo Foster
35
- # @param [String] The paste contents.
36
- # @param [String] The paste description.
37
- # @param [String] The paste's name.
38
- # @param [Boolean] Whether the paste is encrypted.
39
- # @param [String] The syntax highlight language. It is recommended to use the
40
- # constants in PasteeBeta::Constants::Syntax in order to avoid errors.
41
- # @return [String] The new paste's ID.
42
- def submit(paste, description, name, encrypted = false,
43
- language = PasteeBeta::Constants::Syntax::AUTODETECT)
44
- params = {
45
- description: description,
46
- sections: [
47
- {
48
- name: name,
49
- syntax: language,
50
- contents: paste
51
- }
52
- ]
53
- }
54
- params[:encrypted] = encrypted ? 1 : 0
55
- json = post('pastes'.freeze, params)
56
- json['id']
57
- end
58
-
59
- # Gets the UserApplication key for the provided user.
60
- # @author Eli Clemente Gordillo Foster
61
- # @param [String] The user's login handle.
62
- # @param [String] The password for that user.
63
- # @return [String] The user's UserApplication key.
64
- def get_user_key(username, password)
65
- params = {
66
- username: username,
67
- password: password
68
- }
69
- post('users/authenticate', params)['key']
70
- end
71
-
72
- # Gets the current API type.
73
- # @return [String] Either "Application" or "UserApplication".
74
- def get_api_key_type
75
- get('users/info')['type']
76
- end
77
-
78
- # Gets a list of paste IDs.
79
- # @author Eli Clemente Gordillo Foster
80
- # @param [Integer] The number of entries to get per page.
81
- # @param [Integer] The page number to begin at.
82
- # @return [Array<String>] All paste IDs in that page.
83
- def list_pastes(perpage = 25, page = 1)
84
- params = {
85
- perpage: perpage,
86
- page: page,
87
- key: @key
88
- }
89
- json = get('pastes', params)
90
- ret = []
91
- json['data'].each do |h|
92
- ret << h['id']
93
- end
94
- ret
95
- end
96
-
97
- # Gets information about a paste.
98
- # @author Eli Clemente Gordillo Foster
99
- # @param [String] The paste ID.
100
- # @return [Hash] The information for the paste, including id,
101
- # encryption (boolean), description, view count, creation date, expiration
102
- # date, and the sections.
103
- def get_paste(id)
104
- get("pastes/#{id}")['paste']
105
- end
106
-
107
- # Deletes a paste permanently.
108
- # @author Eli Clemente Gordillo Foster
109
- # @param [String] The paste ID.
110
- # @return [void]
111
- def delete_paste(id)
112
- uri = URI.parse(URI.encode("#{URL}/v1/pastes/#{id}"))
113
- response = @client.delete(uri, key: @key)
114
- json = JSON.parse(response.body)
115
- throw_error(json['errors'][0]['message']) unless json['success']
116
- end
117
-
118
- private
119
-
120
- # Throws the according error based on the error message provided by the API.
121
- # @author Eli Clemente Gordillo Foster
122
- # @private
123
- # @param [String] The error message provided by the API.
124
- # @param [String] The field error parameter.
125
- # @raise [UndefinedApplicationKeyError] When the application key is nil.
126
- # @return [void]
127
- def fail_with(message, field = nil)
128
- case message
129
- when 'No application key supplied.'
130
- fail Pastee::Errors::Beta::UndefinedApplicationKeyError
131
- when "The selected #{field} is invalid." && !field.nil?
132
- fail Pastee::Errors::Beta::InvalidFieldError.new("The field #{field} is" \
133
- ' invalid.', field)
134
- when 'No application key supplied.'
135
- fail Pastee::Errors::Beta::UndefinedApplicationKeyError
136
- when 'NotUserApplication'
137
- fail Pastee::Errors::Beta::RequiresUserApplicationError
138
- when 'Invalid application key.'
139
- fail Pastee::Errors::Beta::InvalidAppKeyError
140
- end
141
- end
142
-
143
- # Throws an error based on the error hash provided by the Pastee API.
144
- # @author Eli Clemente Gordillo Foster
145
- # @see #fail_with.
146
- # @private
147
- # @param [Hash] The error field provided by the API.
148
- # @return [void]
149
- def throw_error(errors)
150
- message = errors[0]['message']
151
- field = errors[0]['field'] if defined? errors[0]['field']
152
- fail_with(message, field)
153
- end
154
-
155
- # Performs a generic GET request. Authentication is handled automatically.
156
- # @param url_suffix [String] Everything after v1/ for the particular API.
157
- # @param params [Hash] The HTTP parameters.
158
- # @param header [Hash] Non-Auth header key/value pairs.
159
- # @return [JSON] The successful response as a JSON.
160
- def get(url_suffix, params = {}, header = {})
161
- uri = URI.parse(URI.encode("#{URL}/v1/#{url_suffix}"))
162
- header['X-Auth-Token'] = @key
163
- response = @client.get(uri, params, header)
164
- json = JSON.parse(response.body)
165
- if json['success']
166
- return json
167
- else
168
- throw_error(json['errors'])
169
- end
170
- end
171
-
172
- # Performs a generic POST requests. Authentication and content-types are
173
- # handled automatically.
174
- # @param url_suffix [String] See #get
175
- # @param params [Hash] See #get
176
- # @param header [Hash] See #get
177
- # @return [JSON] See #get
178
- def post(url_suffix, params = {}, header = {})
179
- uri = URI.parse(URI.encode("#{URL}/v1/#{url_suffix}"))
180
- header['X-Auth-Token'] = @key
181
- header['Content-Type'] = 'application/json'
182
- response = @client.post(uri, params.to_json, header)
183
- json = JSON.parse(response.body)
184
- if json['success']
185
- return json
186
- else
187
- throw_error(json['errors'])
188
- end
189
- end
190
- end
@@ -1,422 +0,0 @@
1
- class PasteeBeta
2
- module Constants
3
- module Syntax
4
- # 1C
5
- ONEC = '1c'.freeze
6
-
7
- # Access log
8
- ACCESS_LOG = 'accesslog'.freeze
9
-
10
- # Apache
11
- APACHE = 'apache'.freeze
12
-
13
- # AppleScript
14
- APPLESCRIPT = 'applescript'.freeze
15
-
16
- # ARM Assembly
17
- ARM_ASSEMBLY = 'armasm'.freeze
18
-
19
- # AsciiDoc
20
- ASCIIDOC = 'asciidoc'.freeze
21
-
22
- # AspectJ
23
- ASPECTJ = 'aspectj'.freeze
24
-
25
- # Auto Detect
26
- AUTODETECT = 'autodetect'.freeze
27
-
28
- # AutoHotkey
29
- AUTOHOTKEY = 'autohotkey'.freeze
30
-
31
- # AutoIt
32
- AUTOIT = 'autoit'.freeze
33
-
34
- # AVR Assembler
35
- AVRASM = 'avrasm'.freeze
36
-
37
- # Axapta
38
- AXAPTA = 'axapta'.freeze
39
-
40
- # Bash
41
- BASH = 'bash'.freeze
42
-
43
- # Brainfuck
44
- BRAINFUCK = 'brainfuck'.freeze
45
-
46
- # C#
47
- CSHARP = 'cs'.freeze
48
-
49
- # C++
50
- CPLUSPLUS = 'cpp'.freeze
51
-
52
- # C/AL
53
- CAL = 'cal'.freeze
54
-
55
- # Cap'n Proto
56
- CAPNPROTO = 'capnproto'.freeze
57
-
58
- # Ceylon
59
- CEYLON = 'ceylon'.freeze
60
-
61
- # Clojure
62
- CLOJURE = 'clojure'.freeze
63
-
64
- # Clojure REPL
65
- CLOJURE_REPL = 'clojure-repl'.freeze
66
-
67
- # CMake
68
- CMAKE = 'cmake'.freeze
69
-
70
- # CoffeeScript
71
- COFFEESCRIPT = 'coffeescript'.freeze
72
-
73
- # crmsh
74
- CRMSH = 'crmsh'.freeze
75
-
76
- # Crystal
77
- CRYSTAL = 'crystal'.freeze
78
-
79
- # CSS
80
- CSS = 'css'.freeze
81
-
82
- # D
83
- D = 'd'.freeze
84
-
85
- # Dart
86
- DART = 'dart'.freeze
87
-
88
- # Delphi
89
- DELPHI = 'delphi'.freeze
90
-
91
- # Diff
92
- DIFF = 'diff'.freeze
93
-
94
- # Django
95
- DJANGO = 'django'.freeze
96
-
97
- # DNS Zone file
98
- DNS = 'dns'.freeze
99
-
100
- # Dockerfile
101
- DOCKERFILE = 'dockerfile'.freeze
102
-
103
- # DOS .bat
104
- DOSBAT = 'dos'.freeze
105
-
106
- # Dust
107
- DUST = 'dust'.freeze
108
-
109
- # Elixir
110
- ELIXIR = 'elixir'.freeze
111
-
112
- # Elm
113
- ELM = 'elm'.freeze
114
-
115
- # ERB (Embedded Ruby)
116
- ERB = 'erb'.freeze
117
-
118
- # Erlang
119
- ERLANG = 'erlang'.freeze
120
-
121
- # Erlang REPL
122
- ERLANG_REPL = 'erlang-repl'.freeze
123
-
124
- # F#
125
- FSHARP = 'fsharp'.freeze
126
-
127
- # FIX
128
- FIX = 'fix'.freeze
129
-
130
- # Fortran
131
- FORTRAN = 'fortran'.freeze
132
-
133
- # GAMS
134
- GAMS = 'gams'.freeze
135
-
136
- # G-Code (ISO 6983)
137
- GCODE = 'gcode'.freeze
138
-
139
- # Gherkin
140
- GHERKIN = 'gherkin'.freeze
141
-
142
- # GLSL
143
- GLSL = 'glsl'.freeze
144
-
145
- # Go
146
- GO = 'go'.freeze
147
-
148
- # Golo
149
- GOLO = 'golo'.freeze
150
-
151
- # Gradle
152
- GRADLE = 'gradle'.freeze
153
-
154
- # Groovy
155
- GROOVY = 'groovy'.freeze
156
-
157
- # Haml
158
- HAML = 'haml'.freeze
159
-
160
- # Handlebars
161
- HANDLEBARS = 'handlebars'.freeze
162
-
163
- # Haskell
164
- HASKELL = 'haskell'.freeze
165
-
166
- # Haxe
167
- HAXE = 'haxe'.freeze
168
-
169
- # HTML, XML
170
- XML = 'xml'.freeze
171
-
172
- # HTTP
173
- HTTP = 'http'.freeze
174
-
175
- # Inform 7
176
- INFORM7 = 'inform7'.freeze
177
-
178
- # Ini
179
- INI = 'ini'.freeze
180
-
181
- # Intel x86 Assembly
182
- INTEL_X86_ASSEMBLY = 'x86asm'.freeze
183
-
184
- # IRPF90
185
- IRPF90 = 'irpf90'.freeze
186
-
187
- # Java
188
- JAVA = 'java'.freeze
189
-
190
- # JavaScript
191
- JAVASCRIPT = 'javascript'.freeze
192
-
193
- # JSON
194
- JSON = 'json'.freeze
195
-
196
- # Julia
197
- JULIA = 'julia'.freeze
198
-
199
- # Kotlin
200
- KOTLIN = 'kotlin'.freeze
201
-
202
- # Lasso
203
- LASSO = 'lasso'.freeze
204
-
205
- # Less
206
- LESS = 'less'.freeze
207
-
208
- # Lisp
209
- LISP = 'lisp'.freeze
210
-
211
- # LiveCode
212
- LIVECODE = 'livecodeserver'.freeze
213
-
214
- # LiveScript
215
- LIVESCRIPT = 'livescript'.freeze
216
-
217
- # Lua
218
- LUA = 'lua'.freeze
219
-
220
- # Makefile
221
- MAKEFILE = 'makefile'.freeze
222
-
223
- # Markdown
224
- MARKDOWN = 'markdown'.freeze
225
-
226
- # Mathematica
227
- MATHEMATICA = 'mathematica'.freeze
228
-
229
- # Matlab
230
- MATLAB = 'matlab'.freeze
231
-
232
- # MEL
233
- MEL = 'mel'.freeze
234
-
235
- # Mercury
236
- MERCURY = 'mercury'.freeze
237
-
238
- # Mizar
239
- MIZAR = 'mizar'.freeze
240
-
241
- # Mojolicious
242
- MOJO = 'mojolicious'.freeze
243
-
244
- # Monkey
245
- MONKEY = 'monkey'.freeze
246
-
247
- # Nginx
248
- NGINX = 'nginx'.freeze
249
-
250
- # Nimrod
251
- NIMROD = 'nimrod'.freeze
252
-
253
- # Nix
254
- NIX = 'nix'.freeze
255
-
256
- # NSIS
257
- NSIS = 'nsis'.freeze
258
-
259
- # Objective C
260
- OBJECTIVE_C = 'objectivec'.freeze
261
-
262
- # OCaml
263
- OCAML = 'ocaml'.freeze
264
-
265
- # OpenSCAD
266
- OPENSCAD = 'openscad'.freeze
267
-
268
- # Oracle Rules Language
269
- ORACLE_RULES = 'ruleslanguage'.freeze
270
-
271
- # Oxygene
272
- OXYGENE = 'oxygene'.freeze
273
-
274
- # Parser3
275
- PARSER3 = 'parser3'.freeze
276
-
277
- # Perl
278
- PERL = 'perl'.freeze
279
-
280
- # pf
281
- PF = 'pf'.freeze
282
-
283
- # PHP
284
- PHP = 'php'.freeze
285
-
286
- # PowerShell
287
- POWERSHELL = 'powershell'.freeze
288
-
289
- # Processing
290
- PROCESSING = 'processing'.freeze
291
-
292
- # Prolog
293
- PROLOG = 'prolog'.freeze
294
-
295
- # Protocol Buffers
296
- PROTOCOL_BUFFERS = 'protobuf'.freeze
297
-
298
- # Puppet
299
- PUPPET = 'puppet'.freeze
300
-
301
- # Python
302
- PYTHON = 'python'.freeze
303
-
304
- # Python profile
305
- PYTHON_PROFILE = 'profile'.freeze
306
-
307
- # Q
308
- Q = 'q'.freeze
309
-
310
- # R
311
- R = 'r'.freeze
312
-
313
- # RenderMan RIB
314
- RENDERMAN_RIB = 'rib'.freeze
315
-
316
- # RenderMan RSL
317
- RENDERMAN_RSL = 'rsl'.freeze
318
-
319
- # Roboconf
320
- ROBOCONF = 'roboconf'.freeze
321
-
322
- # Ruby
323
- RUBY = 'ruby'.freeze
324
-
325
- # Rust
326
- RUST = 'rust'.freeze
327
-
328
- # Scala
329
- SCALA = 'scala'.freeze
330
-
331
- # Scheme
332
- SCHEME = 'scheme'.freeze
333
-
334
- # Scilab
335
- SCILAB = 'scilab'.freeze
336
-
337
- # SCSS
338
- SCSS = 'scss'.freeze
339
-
340
- # Smali
341
- SMALI = 'smali'.freeze
342
-
343
- # Smalltalk
344
- SMALLTALK = 'smalltalk'.freeze
345
-
346
- # SML
347
- SML = 'sml'.freeze
348
-
349
- # SQF
350
- SQF = 'sqf'.freeze
351
-
352
- # SQL
353
- SQL = 'sql'.freeze
354
-
355
- # Stata
356
- STATA = 'stata'.freeze
357
-
358
- # STEP Part 21 (ISO 10303-21)
359
- STEP = 'step21'.freeze
360
-
361
- # Stylus
362
- STYLUS = 'stylus'.freeze
363
-
364
- # Swift
365
- SWIFT = 'swift'.freeze
366
-
367
- # Tcl
368
- TCL = 'tcl'.freeze
369
-
370
- # TeX
371
- TEX = 'tex'.freeze
372
-
373
- # Text
374
- TEXT = 'text'.freeze
375
-
376
- # Thrift
377
- THRIFT = 'thrift'.freeze
378
-
379
- # TP
380
- TP = 'tp'.freeze
381
-
382
- # Twig
383
- TWIG = 'twig'.freeze
384
-
385
- # TypeScript
386
- TYPESCRIPT = 'typescript'.freeze
387
-
388
- # Vala
389
- VALA = 'vala'.freeze
390
-
391
- # VB.NET
392
- VB = 'vbnet'.freeze
393
-
394
- # VBScript
395
- VBSCRIPT = 'vbscript'.freeze
396
-
397
- # VBScript in HTML
398
- VBSCRIPT_HTML = 'vbscript-html'.freeze
399
-
400
- # Verilog
401
- VERILOG = 'verilog'.freeze
402
-
403
- # VHDL
404
- VHDL = 'vhdl'.freeze
405
-
406
- # Vim Script
407
- VIM = 'vim'.freeze
408
-
409
- # XL
410
- XL = 'xl'.freeze
411
-
412
- # XQuery
413
- XQUERY = 'xquery'.freeze
414
-
415
- # YAML
416
- YAML = 'yaml'.freeze
417
-
418
- # Zephir
419
- ZEPHIR = 'zephir'.freeze
420
- end
421
- end
422
- end