Class: Pastee::Paste::Section

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

Overview

Wrapper class for paste sections.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Section

Create a new Section object. Used when creating new pastes and when getting existing pastes. For creating new pastes, you only need to provide the name and contents and optionally syntax. The rest will be created automatically by the pastee API or by our #to_h method (syntax defaults to “autodetect”). ID and size are created by pastee, they are only here for receiving paste objects.

Options Hash (opts):

  • :id (Integer)

    Section ID.

  • :syntax (String)

    The syntax short name for this section.

  • :name (String)

    The name of this section.

  • :contents (String)

    The contents of this section.

  • :size (Integer)

    The size of this section.



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pastee/paste.rb', line 99

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]
  @syntax = opts[:syntax]
  @name = opts[:name]
  @contents = opts[:contents]
  @size = opts[:size]
end

Instance Attribute Details

#contentsString (readonly)



84
85
86
# File 'lib/pastee/paste.rb', line 84

def contents
  @contents
end

#idInteger (readonly)



75
76
77
# File 'lib/pastee/paste.rb', line 75

def id
  @id
end

#nameString (readonly)



81
82
83
# File 'lib/pastee/paste.rb', line 81

def name
  @name
end

#sizeInteger (readonly)



87
88
89
# File 'lib/pastee/paste.rb', line 87

def size
  @size
end

#syntaxString (readonly)



78
79
80
# File 'lib/pastee/paste.rb', line 78

def syntax
  @syntax
end

Instance Method Details

#to_hHash

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



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/pastee/paste.rb', line 113

def to_h
  hash = {
    name: name,
    contents: contents
  }
  hash[:id] = id if id
  hash[:syntax] = syntax || 'autodetect'
  hash[:size] = size if size

  hash
end