Class: Pastee

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(api_key) ⇒ Pastee

Creates a new instance of Pastee.

Parameters:

  • api_key (String)

    The API key for this application.



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.

Parameters:

  • id (String)

    The paste ID to delete.

Returns:

  • (Boolean)

    True if it was successfully deleted.

Raises:



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.

Parameters:

  • id (String)

    The paste ID to obtain information for.

Returns:

Raises:



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.

Parameters:

  • id (Integer)

    The ID for this syntax.

Returns:

  • (Pastee::Syntax)

    The syntax object representative of this integer ID.

Raises:



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_typeString

Get the user type for the currently authenticated user.

Returns:

  • (String)

    The user type.

Raises:



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_syntaxesArray<Pastee::Syntax>

Obtains a list of valid Pastee syntaxes.

Returns:

Raises:



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.

Examples:

section1 = Pastee::Paste::Section.new(
  name: 'section 1', # syntax defaults to autodetect
  contents: 'Some text!'
)
section2 = Pastee::Paste::Section.new(
  name: 'section 2',
  syntax: 'ruby',
  contents: File.read('lib/pastee.rb')
)
section3 = Pastee::Paste::Section.new(
  name: 'section 3',
  syntax: 'markdown',
  contents: File.read('README.md')
)
paste = Pastee::Paste.new(
  encrypted: true,
  description: 'super secret paste',
  sections: [
    section1,
    section2,
    section3
  ]
)
pastee.submit(paste)

Parameters:

Returns:

  • (String)

    The paste ID.

Raises:

See Also:



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.

Parameters:

  • name (String)

    The paste’s name.

  • text (String)

    The paste text.

  • encrypted (Boolean) (defaults to: false)

    Whether this paste should be treated as encrypted by pastee.

Returns:

  • (String)

    The paste ID.

Raises:



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