Class: Redhead::String

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/redhead/redhead_string.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ String

Takes string, splits the headers from the content using HEADERS_SEPARATOR_PATTERN, then creates the headers by calling HeaderSet.parse.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/redhead/redhead_string.rb', line 38

def initialize(string)
  if self.class.has_headers?(string)
    # if there is a separator between header content and body content
    if string =~ HEADERS_SEPARATOR_PATTERN
      @string = $'
      header_content = $`
      super(@string)

      @headers = Redhead::HeaderSet.parse(header_content)
    else
      @string = ""
      super(@string)

      # we're dealing with only headers, so pass in the entire original string.
      # this lets us deal with inputs like new("foo: bar")
      @headers = Redhead::HeaderSet.parse(string)
    end
  else
    @string = string
    super(@string)
    @headers = Redhead::HeaderSet.new([])
  end
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/redhead/redhead_string.rb', line 5

def headers
  @headers
end

#stringObject (readonly)

Returns the value of attribute string.



5
6
7
# File 'lib/redhead/redhead_string.rb', line 5

def string
  @string
end

Class Method Details

.has_headers?(string) ⇒ Boolean

Checks if the input string has header lines at the start of its content, and returns true or false depending on the value.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/redhead/redhead_string.rb', line 13

def self.has_headers?(string)
  return false if string.strip.empty?

  # check if the string itself is entirely headers
  has_headers_no_content = string.strip.lines.all? do |l|
    l =~ HEADER_NAME_VALUE_SEPARATOR_PATTERN
  end

  return true if has_headers_no_content

  # split based on the headers separator and see if
  # all lines before the separator look like headers.

  string =~ HEADERS_SEPARATOR_PATTERN
  head_content = $`

  return false unless $`

  head_content.lines.all? do |l|
    l =~ HEADER_NAME_VALUE_SEPARATOR_PATTERN
  end
end

Instance Method Details

#==(other) ⇒ Object

Returns true if self.headers == other.headers and self.string == other.string.



72
73
74
# File 'lib/redhead/redhead_string.rb', line 72

def ==(other)
  headers == other.headers && string == other.string
end

#headers!(hash) ⇒ Object

Modifies the headers in the set, using the given hash, which has the form

{ :some_header => { :raw => a, :key => b }, :another_header => ..., ... }

Change the header with key :some_header such that its new raw name is a and its new key name is b. Returns a HeaderSet object containing the changed Header objects.



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/redhead/redhead_string.rb', line 82

def headers!(hash)
  changing = headers.select { |header| hash.has_key?(header.key) }

  # modifies its elements!
  changing.each do |header|
    new_values = hash[header.key]
    header.raw = new_values[:raw] if new_values[:raw]
    header.key = new_values[:key] if new_values[:key]
  end

  Redhead::HeaderSet.new(changing)
end

#inspectObject



67
68
69
# File 'lib/redhead/redhead_string.rb', line 67

def inspect
  "+#{string.inspect}"
end

#to_sObject

Returns the main body content wrapped in the Redhead String object.



63
64
65
# File 'lib/redhead/redhead_string.rb', line 63

def to_s
  @string
end