Class: Ayadn::Post

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

Instance Method Summary collapse

Instance Method Details

#annotationsObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/ayadn/post.rb', line 154

def annotations
  [
    {
    "type" => "com.ayadn.user",
    "value" => {
      "+net.app.core.user" => {
          "user_id" => "#{Settings.config[:identity][:handle]}",
          "format" => "basic"
        }
      }
    },
    {
    "type" => "com.ayadn.client",
    "value" => {
      "url" => "http://ayadn-app.net",
      "author" => {
          "name" => "Eric Dejonckheere",
          "username" => "ericd",
          "id" => "69904",
          "email" => "[email protected]"
        },
      "version" => "#{Settings.config[:version]}"
      }
    }
  ]
end

#auto_readlineObject

def auto_classic

loop do
  begin
    print "#{Settings.config[:identity][:handle]} >> ".color(:red)
    t = STDIN.gets.chomp
    send_post(t)
    puts Status.done
  rescue Interrupt
    abort(Status.canceled)
  end
end

end



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ayadn/post.rb', line 37

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

#check_length(lines_array, max_size) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ayadn/post.rb', line 123

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



119
120
121
# File 'lib/ayadn/post.rb', line 119

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

#check_post_length(lines_array) ⇒ Object



115
116
117
# File 'lib/ayadn/post.rb', line 115

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

#composeObject



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

def compose
  # case Settings.config[:platform]
  # when /mswin|mingw|cygwin/
    # post = classic
  # else
    require "readline"
    readline
  # end
  # post
end

#entitiesObject



181
182
183
184
185
186
# File 'lib/ayadn/post.rb', line 181

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

#error_text_emptyObject



149
150
151
152
# File 'lib/ayadn/post.rb', line 149

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

#get_markdown_text(str) ⇒ Object



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

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

#markdown_extract(str) ⇒ Object



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

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

#payload_basic(text) ⇒ Object



188
189
190
191
192
193
194
# File 'lib/ayadn/post.rb', line 188

def payload_basic(text)
  {
    "text" => text,
    "entities" => entities,
    "annotations" => annotations
  }
end

#payload_pm(username, text) ⇒ Object



196
197
198
199
200
201
202
203
# File 'lib/ayadn/post.rb', line 196

def payload_pm(username, text)
  {
    "text" => text,
    "entities" => entities,
    "destinations" => username,
    "annotations" => annotations
  }
end

#payload_reply(text, reply_to) ⇒ Object



205
206
207
208
209
210
211
212
# File 'lib/ayadn/post.rb', line 205

def payload_reply(text, reply_to)
  {
    "text" => text,
    "reply_to" => reply_to,
    "entities" => entities,
    "annotations" => annotations
  }
end

#post(args) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/ayadn/post.rb', line 5

def post(args)
  unless text_is_empty?(args)
    send_post(args.join(" "))
  else
    error_text_empty
  end
end

#readlineObject



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ayadn/post.rb', line 51

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

#reply(new_post, replied_to) ⇒ Object

def classic

puts Status.classic
input_text = STDIN.gets.chomp
[input_text]

end



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ayadn/post.rb', line 71

def reply(new_post, replied_to)
  replied_to = replied_to.values[0]
  reply = replied_to[:handle].dup
  reply << " #{new_post}"
  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
  reply
end

#send_content(url, payload) ⇒ Object



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

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

#send_message(channel_id, text) ⇒ Object



90
91
92
93
# File 'lib/ayadn/post.rb', line 90

def send_message(channel_id, text)
  url = Endpoints.new.messages(channel_id, {})
  send_content(url, payload_basic(text))
end

#send_pm(username, text) ⇒ Object



84
85
86
87
88
# File 'lib/ayadn/post.rb', line 84

def send_pm(username, text)
  url = Endpoints.new.pm_url
  url << "?include_annotations=1&access_token=#{Ayadn::Settings.user_token}"
  send_content(url, payload_pm(username, text))
end

#send_post(text) ⇒ Object

def send_log(data)

url = Endpoints.new.ayadnlog
send_content(url, payload_log(data))

end



100
101
102
103
# File 'lib/ayadn/post.rb', line 100

def send_post(text)
  url = Endpoints.new.posts_url
  send_content(url, payload_basic(text))
end

#send_reply(text, post_id) ⇒ Object



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

def send_reply(text, post_id)
  url = Endpoints.new.posts_url
  send_content(url, payload_reply(text, post_id))
end

#text_is_empty?(args) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/ayadn/post.rb', line 145

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