Class: Ayadn::Mark

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

Instance Method Summary collapse

Instance Method Details

#add(*args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ayadn/mark.rb', line 8

def add(*args)
  begin
    init
    unless args.empty?
      double = args.dup
      post_id, convo_title = double.shift, double.join(' ')
    else
      abort Status.wrong_arguments
    end
    abort Status.error_missing_post_id unless post_id.is_integer?
    convo_title = post_id if convo_title == ''
    api, workers, view = API.new, Workers.new, View.new
    users, bucket = [], []
    view.clear_screen
    puts "\nAnalyzing conversation...\n".inverse
    resp = api.get_convo(post_id, options)
    posts = workers.build_posts(resp['data'].reverse)
    posts.each do |id, post|
      users << "#{post[:original_poster]}"
      post[:mentions].each {|mention| users << "#{mention}"}
      bucket << post
    end
    users.uniq!
    now = Time.now.to_s
    bookmark = {
      id: post_id,
      root_id: bucket[0][:id],
      last_id: (bucket.last)[:id],
      title: convo_title,
      first_date: bucket[0][:date],
      last_date: (bucket.last)[:date],
      mark_date: now[0..18],
      first_poster: bucket[0][:original_poster],
      last_poster: (bucket.last)[:username],
      users: users,
      size: bucket.length,
      url: bucket[0][:canonical_url],
      root_text: bucket[0][:raw_text],
      root_colorized_text: bucket[0][:text]
    }
    view.clear_screen
    puts "Bookmarked conversation:\n".color(:green)
    puts make_entry bookmark
    Databases.add_bookmark bookmark
    Logs.rec.info "Added conversation bookmark for post #{bookmark[:id]}."
    puts Status.done
  rescue => e
    Errors.global_error({error: e, caller: caller, data: [args]})
  ensure
    Databases.close_all
  end
end

#clearObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ayadn/mark.rb', line 83

def clear
  begin
    init
    puts "\n\nAre you sure you want to erase all the content of your bookmarks database?\n\n[y/N]\n".color(:red)
    input = STDIN.getch
    if input == 'y' || input == 'Y'
      Databases.clear_bookmarks
      Logs.rec.info "Cleared the bookmarks database."
      puts Status.done
    else
      abort Status.canceled
    end
  rescue => e
    Errors.global_error({error: e, caller: caller, data: []})
  ensure
    Databases.close_all
  end
end

#delete(*args) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ayadn/mark.rb', line 105

def delete *args
  begin
    init
    args.empty? ? abort(Status.wrong_arguments) : post_id = args[0]
    abort Status.error_missing_post_id unless post_id.is_integer?
    Databases.delete_bookmark post_id
    puts Status.done
  rescue => e
    Errors.global_error({error: e, caller: caller, data: [args]})
  ensure
    Databases.close_all
  end
end

#listObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ayadn/mark.rb', line 64

def list
  begin
    init
    list = []
    Databases.bookmarks.each {|i,v| list << v}
    if options[:raw]
      jj JSON.parse(list.to_json)
      exit
    end
    puts "\n"
    list.each {|marked| puts make_entry marked; puts "\n"}
  rescue => e
    Errors.global_error({error: e, caller: caller, data: [args, options]})
  ensure
    Databases.close_all
  end
end

#rename(*args) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ayadn/mark.rb', line 121

def rename *args
  begin
    init
    unless args.empty? || args[1].nil?
      post_id, new_title = args[0], args[1]
    else
      abort Status.wrong_arguments
    end
    abort Status.error_missing_post_id unless post_id.is_integer?
    Databases.rename_bookmark post_id, new_title
    puts Status.done
  rescue => e
    Errors.global_error({error: e, caller: caller, data: [args]})
  ensure
    Databases.close_all
  end
end