Class: Ayadn::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/ayadn/post.rb

Instance Method Summary collapse

Instance Method Details

#auto_readlineObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ayadn/post.rb', line 80

def auto_readline
  loop do
    begin
      #while buffer = Readline.readline("#{Settings.config[:identity][:handle]} >> ".color(:red))
      while buffer = Readline.readline(">> ".color(:red))
        post({text: buffer})
        puts Status.done
      end
    rescue Interrupt
      abort(Status.canceled)
    end
  end
end

#check_length(lines_array, max_size) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ayadn/post.rb', line 115

def check_length(lines_array, max_size)
  words_array = []
  lines_array.each { |word| words_array << get_markdown_text(word) }
  size = words_array.join.length
  if size < 1
    error_text_empty
    exit
  elsif size > max_size
    Errors.warn "Canceled: too long (#{size - max_size}chars)"
    abort(Status.too_long(size, max_size))
  end
end

#check_message_length(lines_array) ⇒ Object



111
112
113
# File 'lib/ayadn/post.rb', line 111

def check_message_length(lines_array)
  check_length(lines_array, Settings.config[:message_max_length])
end

#check_post_length(lines_array) ⇒ Object



107
108
109
# File 'lib/ayadn/post.rb', line 107

def check_post_length(lines_array)
  check_length(lines_array, Settings.config[:post_max_length])
end

#composeObject




76
77
78
# File 'lib/ayadn/post.rb', line 76

def compose
  readline()
end

#entitiesObject



60
61
62
63
64
65
# File 'lib/ayadn/post.rb', line 60

def entities
  {
    "parse_markdown_links" => true,
    "parse_links" => true
  }
end

#error_text_emptyObject



141
142
143
144
# File 'lib/ayadn/post.rb', line 141

def error_text_empty
  puts Status.no_text
  Errors.warn "-Post without text-"
end

#get_markdown_text(str) ⇒ Object



128
129
130
# File 'lib/ayadn/post.rb', line 128

def get_markdown_text(str)
  str.gsub(/\[([^\]]+)\]\(([^)]+)\)/, '\1')
end

#markdown_extract(str) ⇒ Object



132
133
134
135
# File 'lib/ayadn/post.rb', line 132

def markdown_extract(str)
    result = str.gsub(/\[([^\]]+)\]\(([^)]+)\)/, '\1|||\2')
    result.split('|||') #=> [text, link]
end

#message(dic) ⇒ Object



28
29
30
# File 'lib/ayadn/post.rb', line 28

def message(dic)
  send_content(Endpoints.new.messages(dic[:id]), payload_basic(dic))
end

#payload_basic(dic) ⇒ Object




34
35
36
37
38
39
40
# File 'lib/ayadn/post.rb', line 34

def payload_basic(dic)
  {
    "text" => dic[:text],
    "entities" => entities(),
    "annotations" => Annotations.new(dic).content
  }
end

#payload_pm(dic) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/ayadn/post.rb', line 42

def payload_pm(dic)
  {
    "text" => dic[:text],
    "entities" => entities(),
    "destinations" => dic[:username],
    "annotations" => Annotations.new(dic).content
  }
end

#payload_reply(dic) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/ayadn/post.rb', line 51

def payload_reply(dic)
  {
    "text" => dic[:text],
    "reply_to" => dic[:reply_to],
    "entities" => entities(),
    "annotations" => Annotations.new(dic).content
  }
end

#pm(dic) ⇒ Object



24
25
26
# File 'lib/ayadn/post.rb', line 24

def pm(dic)
  send_content(Endpoints.new.pm_url, payload_pm(dic))
end

#post(dic) ⇒ Object



5
6
7
# File 'lib/ayadn/post.rb', line 5

def post(dic)
  send_content(Endpoints.new.posts_url, payload_basic(dic))
end

#readlineObject



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ayadn/post.rb', line 94

def readline
  puts Status.readline
  post = []
  begin
    while buffer = Readline.readline("> ")
      post << buffer
    end
  rescue Interrupt
    abort(Status.canceled)
  end
  post
end

#reply(dic) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ayadn/post.rb', line 9

def reply(dic)
  replied_to = dic[:reply_to].values[0]
  reply = replied_to[:handle].dup
  reply << " #{dic[:text]}"
  replied_to[:mentions].uniq!
  replied_to[:mentions].each do |m|
    next if m == replied_to[:username]
    next if m == Settings.config[:identity][:username]
    reply << " @#{m}"
  end
  dic[:text] = reply
  dic[:reply_to] = dic[:id]
  send_content(Endpoints.new.posts_url, payload_reply(dic))
end

#send_content(url, payload) ⇒ Object




69
70
71
72
# File 'lib/ayadn/post.rb', line 69

def send_content(url, payload)
  url << "?include_annotations=1&access_token=#{Ayadn::Settings.user_token}"
  JSON.parse(CNX.post(url, payload))
end

#text_is_empty?(args) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/ayadn/post.rb', line 137

def text_is_empty?(args)
  args.empty? || args[0] == ""
end