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.

Parameters:

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

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

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)

Returns Contents of the section.

Returns:

  • (String)

    Contents of the section.



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

def contents
  @contents
end

#idInteger (readonly)

Returns The ID for this section.

Returns:

  • (Integer)

    The ID for this section.



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

def id
  @id
end

#nameString (readonly)

Returns The name of the section.

Returns:

  • (String)

    The name of the section.



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

def name
  @name
end

#sizeInteger (readonly)

Returns Size of the section in bytes.

Returns:

  • (Integer)

    Size of the section in bytes.



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

def size
  @size
end

#syntaxString (readonly)

Returns The short name of the syntax used in this section.

Returns:

  • (String)

    The short name of the syntax used in this section.



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.

Returns:

  • (Hash)


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