Class: Pastee::Paste
- Inherits:
-
Object
- Object
- Pastee::Paste
- Defined in:
- lib/pastee/paste.rb
Overview
Wrapper class for pastes.
Defined Under Namespace
Classes: Section
Instance Attribute Summary collapse
-
#created_at ⇒ DateTime
readonly
When this paste was created.
-
#description ⇒ String
readonly
The paste’s description.
-
#expires_at ⇒ DateTime, NilClass
readonly
When this paste will expire, or nil if it never expires.
-
#id ⇒ Integer
readonly
The paste’s ID.
-
#sections ⇒ Array<Pastee::Section>
readonly
An array of Sections for this paste.
-
#views ⇒ Integer
readonly
How many views the paste has.
Instance Method Summary collapse
-
#encrypted? ⇒ Boolean
Whether this paste is encrypted.
-
#initialize(opts = {}) ⇒ Paste
constructor
Create a new Paste object.
-
#to_h ⇒ Hash
Converts this to a hash object.
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.
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_at ⇒ DateTime (readonly)
Returns When this paste was created.
14 15 16 |
# File 'lib/pastee/paste.rb', line 14 def created_at @created_at end |
#description ⇒ String (readonly)
Returns The paste’s description.
8 9 10 |
# File 'lib/pastee/paste.rb', line 8 def description @description end |
#expires_at ⇒ DateTime, NilClass (readonly)
Returns 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 |
#id ⇒ Integer (readonly)
Returns The paste’s ID.
5 6 7 |
# File 'lib/pastee/paste.rb', line 5 def id @id end |
#sections ⇒ Array<Pastee::Section> (readonly)
Returns An array of Sections for this paste.
20 21 22 |
# File 'lib/pastee/paste.rb', line 20 def sections @sections end |
#views ⇒ Integer (readonly)
Returns 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.
52 53 54 |
# File 'lib/pastee/paste.rb', line 52 def encrypted? @encrypted end |
#to_h ⇒ Hash
Converts this to a hash object. Used in submitting pastes.
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 |