Class: Ayadn::View

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

Instance Method Summary collapse

Constructor Details

#initializeView



5
6
7
8
9
# File 'lib/ayadn/view.rb', line 5

def initialize
  @workers = Workers.new
  @thor = Thor::Shell::Basic.new
  @status = Status.new
end

Instance Method Details



431
432
433
434
435
436
437
438
# File 'lib/ayadn/view.rb', line 431

def all_hashtag_links(stream, hashtag)
  clear_screen()
  @status.info("info", "links from posts containing hashtag '##{hashtag}':", "cyan")
  links = @workers.links_from_posts(stream)
  links.uniq!
  show_links(links)
  @workers.save_links(links, "hashtag", hashtag)
end


440
441
442
443
444
445
446
447
# File 'lib/ayadn/view.rb', line 440

def all_search_links(stream, words)
  clear_screen()
  @status.info("info", "links from posts containing word(s) '#{words}':", "cyan")
  links = @workers.links_from_posts(stream)
  links.uniq!
  show_links(links)
  @workers.save_links(links, "search", words)
end


449
450
451
452
453
454
455
456
# File 'lib/ayadn/view.rb', line 449

def all_stars_links(stream)
  clear_screen()
  @status.info("info", "links from your starred posts:", "cyan")
  links = @workers.links_from_posts(stream)
  links.uniq!
  show_links(links)
  @workers.save_links(links, 'starred')
end

#big_separatorObject



487
488
489
# File 'lib/ayadn/view.rb', line 487

def big_separator
  "----------\n\n\n"
end

#clear_screenObject



412
413
414
# File 'lib/ayadn/view.rb', line 412

def clear_screen
  puts "\e[H\e[2J"
end

#downloading(options = {}) ⇒ Object



462
463
464
465
466
467
# File 'lib/ayadn/view.rb', line 462

def downloading(options = {})
  unless options[:raw]
    clear_screen()
    @status.downloading
  end
end

#get(stream, options = {}, niceranks = {}) ⇒ Object



403
404
405
406
407
408
409
410
# File 'lib/ayadn/view.rb', line 403

def get(stream, options = {}, niceranks = {})
  clear_screen()
  if options[:index]
    show_posts_with_index(stream, options, niceranks)
  else
    show_posts(stream, options, niceranks)
  end
end

#hide_cursorObject



15
16
17
# File 'lib/ayadn/view.rb', line 15

def hide_cursor
  puts "\e[?25l"
end

#if_raw(what, options) ⇒ Object



30
31
32
33
34
35
# File 'lib/ayadn/view.rb', line 30

def if_raw what, options
  if options[:raw]
    show_raw(what, options)
    exit
  end
end

#infos(stream, token) ⇒ Object



458
459
460
# File 'lib/ayadn/view.rb', line 458

def infos(stream, token)
  show_userinfos(stream, token, true)
end

#list(what, list, target, options = {}) ⇒ Object



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/ayadn/view.rb', line 469

def list(what, list, target, options = {})
  clear_screen()
  case what
  when :whoreposted
    show_list_reposted(list, target)
  when :whostarred
    show_list_starred(list, target)
  when :followings
    show_list_followings(list, target, options)
  when :followers
    show_list_followers(list, target, options)
  when :muted
    show_list_muted(list, options)
  when :blocked
    show_list_blocked(list, options)
  end
end

#page(msg) ⇒ Object



416
417
418
419
420
421
# File 'lib/ayadn/view.rb', line 416

def page msg
  # clear_screen
  puts "\n"
  puts msg
  puts "\n"
end

#render(stream, options = {}, niceranks = {}) ⇒ Object



395
396
397
398
399
400
401
# File 'lib/ayadn/view.rb', line 395

def render(stream, options = {}, niceranks = {})
  unless options[:raw]
    get(stream['data'], options, niceranks)
  else
    show_raw(stream)
  end
end

#show_channels(resp, options = {}) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/ayadn/view.rb', line 289

def show_channels(resp, options = {})
  view = ""
  bucket = @workers.build_channels(resp['data'], options)
  bucket.reverse.each do |ch|
    if options[:broadcasts]
      next if ch.type != "net.app.core.broadcast"
    end
    if options[:"no-broadcasts"]
      next if ch.type == "net.app.core.broadcast"
    end
    if options[:messages]
      next if ch.type != "net.app.core.pm"
    end
    if options[:"no-messages"]
      next if ch.type == "net.app.core.pm"
    end
    if options[:patter]
      next if ch.type != "net.patter-app.room"
    end
    if options[:"no-patter"]
      next if ch.type == "net.patter-app.room"
    end
    if options[:other]
      case ch.type
      when "net.app.core.pm", "net.app.core.broadcast", "net.patter-app.room"
        next
      end
    end
    if options[:"no-other"]
      next if ch.type != "net.app.core.pm" || ch.type != "net.app.core.broadcast" || ch.type != "net.patter-app.room"
    end
    view << "\n"
    view << "ID: ".color(:cyan)
    view << "#{ch.id}".color(Settings.options[:colors][:id])
    view << "\n"
    ch_alias = Databases.get_alias_from_id(ch.id)
    unless ch_alias.nil?
      view << "Alias: ".color(:cyan)
      view << "#{ch_alias}".color(Settings.options[:colors][:username])
      view << "\n"
    end
    view << "Messages: ".color(:cyan)
    view << "#{ch.num_messages}".color(Settings.options[:colors][:symbols])
    view << "\n"
    if ch.owner
      view << "Owner: ".color(:cyan)
      view << "@#{ch.owner['username']}".color(Settings.options[:colors][:username])
      # + (#{ch.owner['name']}) if ch.owner['name']
      view << "\n"
    end
    unless options[:channels] # unless the request comes from Search
      view << "Writers: ".color(:cyan)
      view << "#{ch.writers}".color(Settings.options[:colors][:name])
      view << "\n"
    end
    view << "Type: ".color(:cyan)
    view << "#{ch.type}".color(Settings.options[:colors][:index])
    view << "\n"
    if ch.type == "net.patter-app.room"
      ann = ch.annotations.select {|a| a['type'] == "net.patter-app.settings"}
      view << "Name: ".color(:cyan)
      view << "#{ann[0]['value']['name']}".color(Settings.options[:colors][:link])
      view << "\n"
      view << "Description: ".color(:cyan)
      view << "#{ann[0]['value']['blurb']}".color(Settings.options[:colors][:username])
      view << "\n"
      ann = ch.annotations.select {|a| a['type'] == "net.app.core.fallback_url"}
      view << "URL: ".color(:cyan)
      view << "#{ann[0]['value']['url']}".color(Settings.options[:colors][:link])
      view << "\n"
    end
    if ch.type == "net.app.core.broadcast"
      ann = ch.annotations.select {|a| a['type'] == "net.app.core.broadcast.metadata"}
      view << "Title: ".color(:cyan)
      view << "#{ann[0]['value']['title']}".color(Settings.options[:colors][:link])
      view << "\n"
      view << "Description: ".color(:cyan)
      view << "#{ann[0]['value']['description']}".color(Settings.options[:colors][:username])
      view << "\n"
      ann = ch.annotations.select {|a| a['type'] == "net.app.core.fallback_url"}
      view << "URL: ".color(:cyan)
      view << "#{ann[0]['value']['url']}".color(Settings.options[:colors][:link])
      view << "\n"
    end
    unless ch.recent_message.nil?
      unless ch.recent_message['text'].nil?
        view << "Most recent message (#{@workers.parsed_time(ch.recent_message['created_at'])}): ".color(:cyan)
        view << "\n"
        view << "---\n#{ch.recent_message['text']}\n---"
      end
    end
    if ch.type == "net.paste-app.clips"
      ann = ch.recent_message['annotations'].select {|a| a['type'] == "net.paste-app.clip"}
      view << "\n\n"
      view << ann[0]['value']['content']
      view << "\n"
    end
    view << "\n\n"
  end
  puts view
  unless options[:channels] || options[:id]
    @status.info("info", "your account is currently linked to #{bucket.length} channels", "cyan")
    puts "\n"
  end
end

#show_cursorObject



11
12
13
# File 'lib/ayadn/view.rb', line 11

def show_cursor
  puts "\e[?25h"
end

#show_files_list(list) ⇒ Object



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

def show_files_list(list)
  puts build_files_list(list)
end

#show_interactions(stream) ⇒ Object



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

def show_interactions(stream)
  #puts "\n"
  puts build_interactions_stream(stream)
end


427
428
429
# File 'lib/ayadn/view.rb', line 427

def show_links(links)
  links.each {|l| puts "#{l}".color(Settings.options[:colors][:link])}
end

#show_list_blocked(list, options = {}) ⇒ Object



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

def show_list_blocked(list, options = {})
  puts @workers.build_blocked_list(list, options)
  puts "\n"
end

#show_list_followers(list, target, options = {}) ⇒ Object



122
123
124
125
# File 'lib/ayadn/view.rb', line 122

def show_list_followers(list, target, options = {})
  puts @workers.build_followers_list(list, target, options)
  puts "\n"
end

#show_list_followings(list, target, options = {}) ⇒ Object



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ayadn/view.rb', line 63

def show_list_followings(list, target, options = {})
  if options["lastpost"]
    clear_screen()
    bucket = []
    list.each do |k,v|
      bucket << [k, v[0], v[1], v[2], v[3], v[4], v[5]]
    end
    count = bucket.size
    if options[:username]
      bucket.sort_by! { |obj| obj[1] }
    elsif options[:name]
      bucket.sort_by! { |obj| obj[2].downcase }
    elsif options[:posts]
      bucket.sort_by! { |obj| [obj[5], obj[1]] }.reverse!
    elsif options[:date]
      bucket.keep_if { |obj| !obj[6].nil? }
      bucket.sort_by! { |obj| obj[6]["created_at"] }.reverse!
    end
    title = if target == "me"
        "Last post of users you're following".color(:cyan)
      else
        "Last post of users ".color(:cyan) + "#{target}".color(:red) + " is following ".color(:cyan)
      end
    puts "\t#{title}\n\n"
    bucket.each.with_index(1) do |obj,index|
      username = "@#{obj[1]}"
      colored_username = username.color(Settings.options[:colors][:username])
      if obj[6].nil?
        @workers.thor.say_status :warning, "user #{colored_username} has no posts, ignored", :red
        newline() 
        next
      end
      date = @workers.parsed_time(obj[6]["created_at"])
      mentions = @workers.extract_mentions(obj[6])
      hashtags = @workers.extract_hashtags(obj[6])
      text = @workers.colorize_text(obj[6]["text"], mentions, hashtags)
      total = "(#{obj[5]} posts)"
      name = obj[2].nil? ? "(no name)" : obj[2]
      colored_total = total.color(Settings.options[:colors][:link])
      colored_name = name.color(Settings.options[:colors][:name])
      colored_date = date.color(Settings.options[:colors][:date])
      puts "#{colored_username} #{colored_name} #{colored_date} #{colored_total}\n"
      newline()
      puts text
      unless index == count
        newline()
        puts "----------\n"
        newline()
      else
        puts "\n"
        newline()
      end
    end
  else
    puts @workers.build_followings_list(list, target, options)
    puts "\n"
  end
end

#show_list_muted(list, options = {}) ⇒ Object



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

def show_list_muted(list, options = {})
  puts @workers.build_muted_list(list, options)
  puts "\n"
end

#show_list_reposted(list, target) ⇒ Object



51
52
53
54
55
# File 'lib/ayadn/view.rb', line 51

def show_list_reposted(list, target)
  users_list, table = @workers.build_reposted_list(list, target)
  puts @workers.build_users_list(users_list, table)
  puts "\n"
end

#show_list_starred(list, target) ⇒ Object



57
58
59
60
61
# File 'lib/ayadn/view.rb', line 57

def show_list_starred(list, target)
  users_list, table = @workers.build_starred_list(list, target)
  puts @workers.build_users_list(users_list, table)
  puts "\n"
end

#show_posted(resp) ⇒ Object



46
47
48
49
# File 'lib/ayadn/view.rb', line 46

def show_posted(resp)
  show_simple_post([resp['data']], {})
  puts "\n" if Settings.options[:timeline][:compact]
end

#show_posts(data, options = {}, niceranks = {}) ⇒ Object



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

def show_posts(data, options = {}, niceranks = {})
  resp = build_stream_without_index(data, options, niceranks)
  puts resp unless resp == ""
end

#show_posts_with_index(data, options = {}, niceranks = {}) ⇒ Object



19
20
21
22
23
# File 'lib/ayadn/view.rb', line 19

def show_posts_with_index(data, options = {}, niceranks = {})
  posts, view = build_stream_with_index(data, options, niceranks)
  puts view unless view == ""
  Databases.save_indexed_posts(posts)
end

#show_raw(stream, options = {}) ⇒ Object



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

def show_raw(stream, options = {})
  #puts stream.to_json
  jj stream
end

#show_settingsObject



146
147
148
149
150
151
152
153
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
180
181
# File 'lib/ayadn/view.rb', line 146

def show_settings
  table = Terminal::Table.new do |t|
    if Settings.options[:formats][:table][:borders] == true
      t.style = { :width => Settings.options[:formats][:table][:width], border_x: '—', border_i: '+', border_y: '|' }
    else
      t.style = { :width => Settings.options[:formats][:table][:width], border_x: ' ', border_i: ' ', border_y: ' ' }
    end
    t.title = "Current Ayadn settings".color(:cyan)
    t.headings = [ "Category".color(:red), "Parameter".color(:red), "Value(s)".color(:red) ]
    @iter = 0
    opts = Settings.options.dup
    opts.each do |k,v|
      v.each do |x,y|
        t << :separator if @iter >= 1 && Settings.options[:timeline][:compact] == false
        unless y.is_a?(Hash)
          t << [ k.to_s.color(:cyan), x.to_s.color(:yellow), y.to_s.color(:green) ]
        else
          y.each do |c|
            yk = c[0]
            tempv = c[1].to_s
            if tempv.size > 10
              yv = "#{tempv[0..7]}..."
            else
              yv = tempv
            end
            t << [ k.to_s.color(:cyan), x.to_s.color(:yellow), "#{yk} = #{yv}".color(:green) ]
          end
        end
        @iter += 1
      end
    end
  end
  clear_screen()
  puts table
  puts "\n"
end

#show_simple_post(post, options = {}) ⇒ Object



42
43
44
# File 'lib/ayadn/view.rb', line 42

def show_simple_post(post, options = {})
  puts build_stream_without_index(post, options, {})
end

#show_userinfos(content, token, show_ranks = false) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/ayadn/view.rb', line 183

def show_userinfos(content, token, show_ranks = false)
  if Settings.options[:timeline][:compact] == true
    padding = "\n"
    view = "\n"
  else
    padding = "\n\n"
    view = ""
  end

  if content['name']
    view << "Name\t\t\t".color(:cyan) + content['name'].color(Settings.options[:colors][:name])
  else
    view << "Name\t\t\t".color(:cyan) + "(no name)".color(:red)
  end

  view << "#{padding}Username\t\t".color(:cyan) + "@#{content['username']}".color(Settings.options[:colors][:username])

  view << "#{padding}ID\t\t\t".color(:cyan) + content['id'].color(Settings.options[:colors][:id])

  view << "#{padding}URL\t\t\t".color(:cyan) + content['canonical_url'].color(Settings.options[:colors][:link])

  unless content['verified_domain'].nil?
    if content['verified_domain'] =~ (/http/ || /https/)
       domain = content['verified_domain']
    else
      domain = "http://#{content['verified_domain']}"
    end
    view << "\nVerified domain\t\t".color(:cyan) + domain.color(Settings.options[:colors][:link])
  end


  view << "#{padding}Account creation\t".color(:cyan) + @workers.parsed_time(content['created_at']).color(Settings.options[:colors][:excerpt])
  view << "#{padding}TimeZone\t\t".color(:cyan) + content['timezone'].color(Settings.options[:colors][:excerpt])
  view << "\nLocale\t\t\t".color(:cyan) + content['locale'].color(Settings.options[:colors][:excerpt])

  view << "#{padding}Posts\t\t\t".color(:cyan) + content['counts']['posts'].to_s.color(Settings.options[:colors][:excerpt])


  unless show_ranks == false
    # this is ok for one user, but do not call this in a loop
    # do call them all at once instead if many
    ranks = NiceRank.new.get_posts_day([content['id'].to_i])
    unless ranks.empty?
      view << "#{padding}Posts/day\t\t".color(:cyan) + ranks[0][:posts_day].to_s.color(Settings.options[:colors][:excerpt])
    end
  end

  view << "#{padding}Following\t\t".color(:cyan) + content['counts']['following'].to_s.color(Settings.options[:colors][:excerpt])
  view << "\nFollowers\t\t".color(:cyan) + content['counts']['followers'].to_s.color(Settings.options[:colors][:excerpt])

  if content['username'] == Settings.config[:identity][:username] && !token.nil?
    view << "#{padding}Storage used\t\t".color(:cyan) + "#{token['storage']['used'].to_filesize}".color(Settings.options[:colors][:excerpt])
    view << "\nStorage available\t".color(:cyan) + "#{token['storage']['available'].to_filesize}".color(Settings.options[:colors][:excerpt])
  end

  #view << "\nStars\t\t\t".color(:cyan) + content['counts']['stars'].to_s.color(:yellow)

  unless content['username'] == Settings.config[:identity][:username]
    if content['you_follow']
      view << "#{padding}You follow ".color(:cyan) + "@#{content['username']}".color(Settings.options[:colors][:username])
    else
      view << "#{padding}You don't follow ".color(:cyan) + "@#{content['username']}".color(Settings.options[:colors][:username])
    end
    if content['follows_you']
      view << "\n" + "@#{content['username']}".color(Settings.options[:colors][:username]) + " follows you".color(:cyan)
    else
      view << "\n" + "@#{content['username']}".color(Settings.options[:colors][:username]) + " doesn't follow you".color(:cyan)
    end
    if content['you_muted']
      view << "\nYou muted " + "@#{content['username']}".color(Settings.options[:colors][:username])
    end
    if content['you_blocked']
      view << "\nYou blocked " + "@#{content['username']}".color(Settings.options[:colors][:username])
    end
  end

  unless content['annotations'].empty?
    view << "\n" unless Settings.options[:timeline][:compact] == true
  end
  content['annotations'].each do |anno|
    case anno['type']
    when "net.app.core.directory.blog"
      view << "\nBlog\t\t\t".color(:cyan) + "#{anno['value']['url']}".color(Settings.options[:colors][:link])
    when "net.app.core.directory.twitter"
      view << "\nTwitter\t\t\t".color(:cyan) + "#{anno['value']['username']}".color(:green)
    when "com.appnetizens.userinput.birthday"
      view << "\nBirthday\t\t".color(:cyan) + "#{anno['value']['birthday']}".color(:green)
    end
  end


  #view << "#{padding}Avatar URL\t\t".color(:cyan) + content['avatar_image']['url']

  if content['description']
    mentions = content['description']['entities']['mentions'].map {|m| "@#{m['name']}"}
    hashtags = content['description']['entities']['hashtags'].map {|m| m['name']}
    view << "#{padding}#{@workers.colorize_text(content['description']['text'], mentions, hashtags)}\n"
    view << "\n" unless Settings.options[:timeline][:compact] == true
  end

  view << "\n" if Settings.options[:timeline][:compact] == true

  puts view

end

#winsizeObject



423
424
425
# File 'lib/ayadn/view.rb', line 423

def winsize
  IO.console.winsize
end