Class: Pastee::Paste

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

Overview

Wrapper class for pastes.

Defined Under Namespace

Classes: Section

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Paste

Create a new Paste object. Used when creating new pastes and when getting existing pastes. For creating new pastes, you only need to provide the description and sections, and optionally encrypted (defaults to false). The rest will be created automatically by the pastee API. ID, views, created_at, and expires_at are created by pastee, they are only here for receiving paste objects. object (returned by the pastee API) to be turned into a Section object.

Parameters:

  • opts (Hash) (defaults to: {})

    Options hash. Keys can either be strings or symbols — they will be converted to symbols.

Options Hash (opts):

  • :id (String)

    Paste ID.

  • :encrypted (Boolean)

    Whether this paste is treated as encrypted. Pastee does not actually encrypt pastes, this only affects how it appears on the website.

  • :description (String)

    The paste’s description or name.

  • :views (Integer)

    How many times this paste has been viewed.

  • :created_at (String)

    When this paste was created, in string form. Is parsed into a DateTime.

  • :expires_at (String)

    When this paste expires, in string form. Is parsed into a DateTime.

  • :sections (Array)

    An array either of Section objects (if you are creating a new paste) or a hash



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pastee/paste.rb', line 36

def initialize(opts = {})
  # Standardize keys so that both the pastee API (which uses strings) and pastee-rb consumers (who use symbols) can
  # both use this method.
  opts = Hash[opts.map { |k, v| [k.to_sym, v] }]

  @id = opts[:id]
  @encrypted = opts[:encrypted] || false
  @description = opts[:description]
  @views = opts[:views]
  @created_at = DateTime.parse(opts[:created_at]) if opts[:created_at]
  @expires_at = DateTime.parse(opts[:expires_at]) if opts[:expires_at]
  # For creating our own pastes
  @sections = opts[:sections][0].is_a?(Section) ? opts[:sections] : opts[:sections].map { |o| Section.new(o) }
end

Instance Attribute Details

#created_atDateTime (readonly)

Returns When this paste was created.

Returns:

  • (DateTime)

    When this paste was created.



14
15
16
# File 'lib/pastee/paste.rb', line 14

def created_at
  @created_at
end

#descriptionString (readonly)

Returns The paste’s description.

Returns:

  • (String)

    The paste’s description.



8
9
10
# File 'lib/pastee/paste.rb', line 8

def description
  @description
end

#expires_atDateTime, NilClass (readonly)

Returns When this paste will expire, or nil if it never expires.

Returns:

  • (DateTime, NilClass)

    When this paste will expire, or nil if it never expires.



17
18
19
# File 'lib/pastee/paste.rb', line 17

def expires_at
  @expires_at
end

#idInteger (readonly)

Returns The paste’s ID.

Returns:

  • (Integer)

    The paste’s ID.



5
6
7
# File 'lib/pastee/paste.rb', line 5

def id
  @id
end

#sectionsArray<Pastee::Section> (readonly)

Returns An array of Sections for this paste.

Returns:

  • (Array<Pastee::Section>)

    An array of Sections for this paste.



20
21
22
# File 'lib/pastee/paste.rb', line 20

def sections
  @sections
end

#viewsInteger (readonly)

Returns How many views the paste has.

Returns:

  • (Integer)

    How many views the paste has.



11
12
13
# File 'lib/pastee/paste.rb', line 11

def views
  @views
end

Instance Method Details

#encrypted?Boolean

Returns Whether this paste is encrypted.

Returns:

  • (Boolean)

    Whether this paste is encrypted.



52
53
54
# File 'lib/pastee/paste.rb', line 52

def encrypted?
  @encrypted
end

#to_hHash

Converts this to a hash object. Used in submitting pastes.

Returns:

  • (Hash)


58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pastee/paste.rb', line 58

def to_h
  hash = {
    description: description,
    sections: sections.map(&:to_h)
  }
  hash[:id] = id if id
  hash[:encrypted] = encrypted?
  hash[:views] = views if views
  hash[:created_at] = created_at if created_at
  hash[:expires_at] = expires_at if expires_at

  hash
end