Class: Ayadn::Post

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

Instance Method Summary collapse

Instance Method Details

#annotationsObject



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/ayadn/post.rb', line 281

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

#annotations_embedded(dic) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/ayadn/post.rb', line 159

def annotations_embedded dic
  base = annotations()
  dic['data'].each do |obj|
    base << {
      "type" => "net.app.core.oembed",
      "value" => {
         "+net.app.core.file" => {
            "file_id" => obj['data']['id'],
            "file_token" => obj['data']['file_token'],
            "format" => "oembed"
         }
      }
    }
  end
  return base
end

#auto_readlineObject



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ayadn/post.rb', line 176

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



250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/ayadn/post.rb', line 250

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



246
247
248
# File 'lib/ayadn/post.rb', line 246

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

#check_post_length(lines_array) ⇒ Object



242
243
244
# File 'lib/ayadn/post.rb', line 242

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

#composeObject



13
14
15
# File 'lib/ayadn/post.rb', line 13

def compose
    readline()
end

#entitiesObject



308
309
310
311
312
313
# File 'lib/ayadn/post.rb', line 308

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

#error_text_emptyObject



276
277
278
279
# File 'lib/ayadn/post.rb', line 276

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

#get_markdown_text(str) ⇒ Object



263
264
265
# File 'lib/ayadn/post.rb', line 263

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

#markdown_extract(str) ⇒ Object



267
268
269
270
# File 'lib/ayadn/post.rb', line 267

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

#payload_basic(text) ⇒ Object



317
318
319
320
321
322
323
# File 'lib/ayadn/post.rb', line 317

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

#payload_embedded(dic) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/ayadn/post.rb', line 133

def payload_embedded dic
  {
    "text" => dic['text'],
    "entities" => entities,
    "annotations" => annotations_embedded(dic)
  }
end

#payload_movie(dic) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ayadn/post.rb', line 53

def payload_movie dic
  ann = annotations_embedded(dic)
  ann << {
    "type" => "com.ayadn.movie",
      "value" => {
        "title" => dic['title'],
        "source" => dic['source']
      }
  }
  {
    "text" => dic['text'],
    "entities" => entities,
    "annotations" => ann
  }
end

#payload_nowplaying(dic) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ayadn/post.rb', line 85

def payload_nowplaying dic
  ann = annotations()
  if dic['visible'] == true
    ann << {
      "type" => "com.ayadn.nowplaying",
        "value" => {
          "title" => dic['title'],
          "artist" => dic['artist'],
          "artwork" => dic['artwork'],
          "link" => dic['link'],
          "source" => dic['source']
        }
    }
  else
    ann << {
      "type" => "com.ayadn.nowplaying",
        "value" => {
          "status" => "no-url",
          "source" => dic['source']
        }
    }
  end
  if dic['visible'] == true
    ann << {
      "type" => "net.app.core.oembed",
      "value" => {
        "version" => "1.0",
        "type" => "photo",
        "width" => dic['width'],
        "height" => dic['height'],
        "title" => dic['title'],
        "url" => dic['artwork'],
        "embeddable_url" => dic['artwork'],
        "provider_url" => "https://itunes.apple.com",
        "provider_name" => "iTunes",
        "thumbnail_url" => dic['artwork_thumb'],
        "thumbnail_width" => dic['width_thumb'],
        "thumbnail_height" => dic['height_thumb']
      }
    }
  end
  {
    "text" => dic['text'],
    "entities" => entities,
    "annotations" => ann
  }
end

#payload_pm(username, text) ⇒ Object



325
326
327
328
329
330
331
332
# File 'lib/ayadn/post.rb', line 325

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

#payload_pm_embedded(dic) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/ayadn/post.rb', line 150

def payload_pm_embedded dic
  {
    "text" => dic['text'],
    "entities" => entities,
    "destinations" => dic['username'],
    "annotations" => annotations_embedded(dic)
  }
end

#payload_reply(text, reply_to) ⇒ Object



334
335
336
337
338
339
340
341
# File 'lib/ayadn/post.rb', line 334

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

#payload_reply_embedded(dic) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/ayadn/post.rb', line 141

def payload_reply_embedded dic
  {
    "text" => dic['text'],
    "reply_to" => dic['reply_to'],
    "entities" => entities,
    "annotations" => annotations_embedded(dic)
  }
end

#payload_tvshow(dic) ⇒ Object



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

def payload_tvshow dic
  ann = annotations_embedded(dic)
  ann << {
    "type" => "com.ayadn.tvshow",
      "value" => {
        "title" => dic['title'],
        "source" => dic['source']
      }
  }
  {
    "text" => dic['text'],
    "entities" => entities,
    "annotations" => ann
  }
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



190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/ayadn/post.rb', line 190

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

#reply(new_post, replied_to) ⇒ Object



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

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



237
238
239
240
# File 'lib/ayadn/post.rb', line 237

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

#send_embedded(text, files) ⇒ Object



17
18
19
# File 'lib/ayadn/post.rb', line 17

def send_embedded text, files
  send_embedded_pictures({'text' => text, 'data' => FileOps.upload_files(files)})
end

#send_embedded_pictures(dic) ⇒ Object



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

def send_embedded_pictures dic
  send_content(Endpoints.new.posts_url, payload_embedded(dic))
end

#send_message(channel_id, text) ⇒ Object



220
221
222
# File 'lib/ayadn/post.rb', line 220

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

#send_movie(dic) ⇒ Object



45
46
47
# File 'lib/ayadn/post.rb', line 45

def send_movie dic
  send_content(Endpoints.new.posts_url, payload_movie(dic))
end

#send_nowplaying(dic) ⇒ Object



41
42
43
# File 'lib/ayadn/post.rb', line 41

def send_nowplaying dic
  send_content(Endpoints.new.posts_url, payload_nowplaying(dic))
end

#send_pm(username, text) ⇒ Object



216
217
218
# File 'lib/ayadn/post.rb', line 216

def send_pm(username, text)
  send_content(Endpoints.new.pm_url, payload_pm(username, text))
end

#send_pm_embedded(username, text, files) ⇒ Object



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

def send_pm_embedded username, text, files
  send_pm_embedded_pictures({'text' => text, 'username' => username, 'data' => FileOps.upload_files(files)})
end

#send_pm_embedded_pictures(dic) ⇒ Object



37
38
39
# File 'lib/ayadn/post.rb', line 37

def send_pm_embedded_pictures dic
  send_content(Endpoints.new.pm_url, payload_pm_embedded(dic))
end

#send_post(text) ⇒ Object

def send_log(data)

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

end



229
230
231
# File 'lib/ayadn/post.rb', line 229

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

#send_reply(text, post_id) ⇒ Object



233
234
235
# File 'lib/ayadn/post.rb', line 233

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

#send_reply_embedded(text, reply_to, files) ⇒ Object



21
22
23
# File 'lib/ayadn/post.rb', line 21

def send_reply_embedded text, reply_to, files
  send_reply_embedded_pictures({'text' => text, 'reply_to' => reply_to, 'data' => FileOps.upload_files(files)})
end

#send_reply_embedded_pictures(dic) ⇒ Object



33
34
35
# File 'lib/ayadn/post.rb', line 33

def send_reply_embedded_pictures dic
  send_content(Endpoints.new.posts_url, payload_reply_embedded(dic))
end

#send_tvshow(dic) ⇒ Object



49
50
51
# File 'lib/ayadn/post.rb', line 49

def send_tvshow dic
  send_content(Endpoints.new.posts_url, payload_tvshow(dic))
end

#text_is_empty?(args) ⇒ Boolean

Returns:

  • (Boolean)


272
273
274
# File 'lib/ayadn/post.rb', line 272

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