Class: Ayadn::Post

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

Instance Method Summary collapse

Instance Method Details

#auto_readlineObject



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

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



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ayadn/post.rb', line 127

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

works on an array



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

def check_message_length(lines_array) # works on an array
  check_length(lines_array, Settings.config[:message_max_length])
end

#check_post_length(lines_array) ⇒ Object

works on an array



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

def check_post_length(lines_array) # works on an array
  check_length(lines_array, Settings.config[:post_max_length])
end

#composeObject




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

def compose
  readline()
end

#entitiesObject



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

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

#error_text_emptyObject



153
154
155
156
# File 'lib/ayadn/post.rb', line 153

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

#get_markdown_text(str) ⇒ Object



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

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

#markdown_extract(str) ⇒ Object



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

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

#message(dic) ⇒ Object



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

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

#payload_basic(dic) ⇒ Object




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

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

#payload_pm(dic) ⇒ Object



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

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

#payload_reply(dic) ⇒ Object



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

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

#pm(dic) ⇒ Object



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

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

#post_size(post) ⇒ Object

works on a string



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

def post_size(post) # works on a string
  size, max_size = post.length, Settings.config[:post_max_length]
  if size < 1
    abort(error_text_empty)
  elsif size > max_size
    Errors.warn "Canceled: too long (#{size - max_size}chars)"
    puts "\nYour text was: \n\n#{post}\n\n".color(:yellow)
    abort(Status.too_long(size, max_size))
  end
end

#readlineObject



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

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
23
# 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
  post_size(reply)
  dic[:text] = reply
  dic[:reply_to] = dic[:id]
  send_content(Endpoints.new.posts_url, payload_reply(dic))
end

#send_content(url, payload) ⇒ Object




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

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)


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

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