Class: Tweet

Inherits:
Object
  • Object
show all
Defined in:
lib/tidtools/tweet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time_stamp, content) ⇒ Tweet

Returns a new instance of Tweet.



8
9
10
11
# File 'lib/tidtools/tweet.rb', line 8

def initialize(time_stamp, content)
  @time_stamp = time_stamp
  @content = content
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



6
7
8
# File 'lib/tidtools/tweet.rb', line 6

def content
  @content
end

#time_stampObject (readonly)

Returns the value of attribute time_stamp.



6
7
8
# File 'lib/tidtools/tweet.rb', line 6

def time_stamp
  @time_stamp
end

Class Method Details

.decorate(text) ⇒ Object

テキストをつぶやき形式に整形する



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tidtools/tweet.rb', line 14

def self.decorate(text)
  array = text.split("\n")

  array.each_with_index do |line, index|
    # 5つ以上の水平線を4つにそろえる
    array[index] = array[index].sub(/^----+$/, "----")

    # 水平線手前の日付表示に装飾を付ける
    if (line =~ /^----+$/ and index > 0)
      ary = ParseDate::parsedate(array[index - 1])
      if (ary[0])           # 日付表示の場合のみ
        array[index - 1] = "~~@@color(gray):" + array[index - 1] + "@@~~"
      end
    end
  end

  array.join("\n")
end

.find_insert_pos(tweets, tweet) ⇒ Object

ソートされたつぶやきの配列に対して、あるつぶやきの挿入位置を返す



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tidtools/tweet.rb', line 46

def self.find_insert_pos(tweets, tweet)
  return 0 if tweet.time_stamp.nil?
  
  tweets.each_index do |index|
    if (tweets[index].time_stamp.nil?)
      next
    elsif (tweet.time_stamp >= tweets[index].time_stamp)
      return index
    end
  end

  return tweets.size
end

.merge(origin, add) ⇒ Object

つぶやき形式のテキストをマージする



34
35
36
37
38
39
40
41
42
43
# File 'lib/tidtools/tweet.rb', line 34

def self.merge(origin, add)
  origin_t = Tweet.parse_from_text(origin)
  add_t = Tweet.parse_from_text(add)

  add_t.each do |tweet|
    origin_t.insert(find_insert_pos(origin_t, tweet), tweet)
  end

  origin_t
end

.parse_from_text(text) ⇒ Object

つぶやき形式の文字列を渡すと、Tweet型の配列を返す



61
62
63
64
65
66
67
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
95
96
97
# File 'lib/tidtools/tweet.rb', line 61

def self.parse_from_text(text)
  tweets = []
  array = text.split("\n")

  index = 0
  start_index = 0
  is_pre = false
  
  while true
    if (array[index] =~ /^\{\{\{/)
      is_pre = true
    end
    
    if (!is_pre and array[index] =~ /^----+/)
      text = array[start_index...index].join("\n")
      tweets.push Tweet.new(parse_time_stamp(text), text)
      start_index = index + 1
    end
    
    if (array[index] =~ /^\}\}\}/)
      is_pre = false
    end

    index += 1

    if (index >= array.size)
      if (index > start_index)
        text = array[start_index..index].join("\n")
        tweets.push Tweet.new(parse_time_stamp(text), text)
      end

      break
    end
  end
    
  tweets
end

.parse_time_stamp(text) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/tidtools/tweet.rb', line 99

def self.parse_time_stamp(text)
  str = text.split(/\n/)[-1]

  if (str)
    ary = ParseDate::parsedate(str)
    if (ary[0])
      Time::local(*ary[0..4])
    else
      nil
    end
  else
    nil
  end
end