Class: Clip
- Inherits:
-
Object
- Object
- Clip
- Defined in:
- lib/clip.rb
Instance Attribute Summary collapse
-
#created ⇒ Object
Returns the value of attribute created.
-
#data ⇒ Object
Returns the value of attribute data.
-
#pos ⇒ Object
Returns the value of attribute pos.
-
#title ⇒ Object
Returns the value of attribute title.
-
#type ⇒ Object
Returns the value of attribute type.
-
#wrap ⇒ Object
Returns the value of attribute wrap.
Class Method Summary collapse
Instance Method Summary collapse
- #==(clip) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ Clip
constructor
A new instance of Clip.
-
#text ⇒ Object
POSSIBLE CASES FOR TEXT <-> TITLE/DATA CONVERSION text title data a) “” 1) “” “” b) abc 2) abc “” c) abcn 2) abc “” d) abcn 3) abc defn def e) abcn 3) abc defn defn.
- #text=(str) ⇒ Object
-
#title! ⇒ Object
Return modified title for easier readabiliy - empty string is replaced with “…” - tabs are replaced with 4 spaces.
- #to_hash ⇒ Object
- #to_mbox ⇒ Object
Constructor Details
#initialize ⇒ Clip
Returns a new instance of Clip.
19 20 21 22 23 24 25 26 |
# File 'lib/clip.rb', line 19 def initialize @data = "" # no lines at all, "\n" would be 1 empty line @wrap = false @type = :text @title = "" @created = Time.now @pos = 0 end |
Instance Attribute Details
#created ⇒ Object
Returns the value of attribute created.
17 18 19 |
# File 'lib/clip.rb', line 17 def created @created end |
#data ⇒ Object
Returns the value of attribute data.
17 18 19 |
# File 'lib/clip.rb', line 17 def data @data end |
#pos ⇒ Object
Returns the value of attribute pos.
17 18 19 |
# File 'lib/clip.rb', line 17 def pos @pos end |
#title ⇒ Object
Returns the value of attribute title.
17 18 19 |
# File 'lib/clip.rb', line 17 def title @title end |
#type ⇒ Object
Returns the value of attribute type.
17 18 19 |
# File 'lib/clip.rb', line 17 def type @type end |
#wrap ⇒ Object
Returns the value of attribute wrap.
17 18 19 |
# File 'lib/clip.rb', line 17 def wrap @wrap end |
Class Method Details
.from_hash(hash) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/clip.rb', line 96 def Clip.from_hash(hash) cl = Clip.new case hash["type"] when :text cl.wrap = hash["wrap"] cl.data = hash["data"] cl.title = hash["title"] cl.created = hash["created"] end return cl end |
.from_mbox(str) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/clip.rb', line 28 def Clip.from_mbox(str) # "From_" line already removed cl = Clip.new in_header = true charset = "UTF-8" str.each_line do | line | line.encode!(charset) if in_header then if line == "\n" then in_header = false next end #(key, value) = line.split(/\s*:\s*/,2) # stricter pattern to preserve leading spaces (key, value) = line.split(/\s*: /,2) value.chomp! case key.downcase when "flags", "x-flags" cl.wrap = value.downcase.include?("w") when "subject" cl.title = value when "created-at", "date" cl.created = Time.parse(value) when "content-type" case value when "text/plain" cl.type = :text when "application/x-jota-pref" cl.type = :pref else cl.type = :text # safe fallback type end end else cl.data << line end end return cl end |
Instance Method Details
#==(clip) ⇒ Object
124 125 126 |
# File 'lib/clip.rb', line 124 def ==(clip) return self.to_hash == clip.to_hash end |
#empty? ⇒ Boolean
119 120 121 122 |
# File 'lib/clip.rb', line 119 def empty? # strip also removes LFs return @data.strip == "" && @title.strip == "" end |
#text ⇒ Object
POSSIBLE CASES FOR TEXT <-> TITLE/DATA CONVERSION
text title data
a) “” 1) “” “” b) abc 2) abc “” c) abcn 2) abc “” d) abcn 3) abc defn
def
e) abcn 3) abc defn
def\n
138 139 140 141 142 143 144 145 146 |
# File 'lib/clip.rb', line 138 def text if @title == "" and @data == "" then # map (1) to (a) return "" else # map (2) to (b) and (3) to (d) return "%s\n%s" % [@title,@data] end end |
#text=(str) ⇒ Object
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/clip.rb', line 148 def text=(str) # map (a),(b) to (c) and (d) to (e) str = str.chomp + "\n" # map (c) to (2) and (e) to (3) arr = str.split("\n",-1) # arr has at least 2 elems # the last elem is empty @title = arr[0] # first elem = first line @data = arr[1..-1].join("\n") # second to last elem end |
#title! ⇒ Object
Return modified title for easier readabiliy
-
empty string is replaced with “…”
-
tabs are replaced with 4 spaces
162 163 164 165 166 167 168 169 |
# File 'lib/clip.rb', line 162 def title! if @title.strip == "" then return "..." else return (@title) end end |
#to_hash ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/clip.rb', line 108 def to_hash hash = Hash.new hash["type"] = @type hash["wrap"] = @wrap hash["data"] = @data hash["title"] = @title hash["created"] = @created return hash end |
#to_mbox ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/clip.rb', line 68 def to_mbox str = "" charset = "UTF-8" str << "Created-At: %s\n" % @created.ctime str << "Subject: %s\n" % @title case @type when :text str << "Content-Type: text/plain; charset=#{charset}\n" when :pref str << "Content-Type: application/x-jota-pref\n" end str << "Flags: " str << "w" if @wrap str << "\n" str << "Originating-Client: %s %s\n" % [ Version::PROGRAM_NAME, Version::STRING] str << "\n" str << @data return str end |