Class: Ayadn::Post

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

Instance Method Summary collapse

Constructor Details

#initializePost

Returns a new instance of Post.



7
8
9
# File 'lib/ayadn/post.rb', line 7

def initialize
  @status = Status.new
end

Instance Method Details

#auto_readlineObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ayadn/post.rb', line 87

def auto_readline
  loop do
    begin
      #while buffer = Readline.readline("#{Settings.config[:identity][:handle]} >> ".color(:red))
      while buffer = Readline.readline(">> ".color(:red))
        resp = post({text: buffer})
        FileOps.save_post(resp) if Settings.options[:backup][:posts]
        @status.done
      end
    rescue Interrupt
      @status.canceled
      exit
    end
  end
end

#bad_text_size(post, size, max_size) ⇒ Object



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

def bad_text_size(post, size, max_size)
  if size < 1
    error_text_empty()
  elsif size > max_size
    Errors.warn "Canceled: too long (#{size - max_size}chars)"
    @status.info("info", "your text:", "cyan")
    puts post
    @status.too_long(size, max_size)
    exit
  end
end

#composeObject




83
84
85
# File 'lib/ayadn/post.rb', line 83

def compose
  readline()
end

#entitiesObject



67
68
69
70
71
72
# File 'lib/ayadn/post.rb', line 67

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

#error_text_emptyObject



162
163
164
165
166
# File 'lib/ayadn/post.rb', line 162

def error_text_empty
  @status.no_text
  Errors.warn "-No text-"
  exit
end


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

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

#markdown_extract(str) ⇒ Object



157
158
159
160
# File 'lib/ayadn/post.rb', line 157

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

#message(dic) ⇒ Object



35
36
37
# File 'lib/ayadn/post.rb', line 35

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

#message_size_error(message) ⇒ Object



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

def message_size_error(message)
  text = keep_text_from_markdown_links(message)
  size, max_size = text.length, Settings.config[:message_max_length]
  bad_text_size(message, size, max_size)
end

#message_size_ok?(message) ⇒ Boolean

works on a string, returns boolean

Returns:

  • (Boolean)


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

def message_size_ok?(message) # works on a string, returns boolean
  text = keep_text_from_markdown_links(message)
  size, max_size = text.length, Settings.config[:message_max_length]
  (size >= 1 && size <= max_size)
end

#payload_basic(dic) ⇒ Object




41
42
43
44
45
46
47
# File 'lib/ayadn/post.rb', line 41

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

#payload_pm(dic) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/ayadn/post.rb', line 49

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

#payload_reply(dic) ⇒ Object



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

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

#pm(dic) ⇒ Object



31
32
33
# File 'lib/ayadn/post.rb', line 31

def pm(dic)
  send_content(Endpoints.new.pm_url, payload_pm(dic))
end

#post(dic) ⇒ Object



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

def post(dic)
  send_content(Endpoints.new.posts_url, payload_basic(dic))
end

#post_size_error(post) ⇒ Object



129
130
131
132
133
# File 'lib/ayadn/post.rb', line 129

def post_size_error(post)
  text = keep_text_from_markdown_links(post)
  size, max_size = text.length, Settings.config[:post_max_length]
  bad_text_size(post, size, max_size)
end

#post_size_ok?(post) ⇒ Boolean

works on a string, returns boolean

Returns:

  • (Boolean)


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

def post_size_ok?(post) # works on a string, returns boolean
  text = keep_text_from_markdown_links(post)
  size, max_size = text.length, Settings.config[:post_max_length]
  (size >= 1 && size <= max_size)
end

#readlineObject



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ayadn/post.rb', line 103

def readline
  @status.readline
  post = []
  begin
    while buffer = Readline.readline("> ")
      post << buffer
    end
  rescue Interrupt
    @status.canceled
    exit
  end
  post
end

#reply(dic) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ayadn/post.rb', line 15

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_error(reply) if post_size_ok?(reply) == false
  dic[:text] = reply
  dic[:reply_to] = dic[:id]
  send_content(Endpoints.new.posts_url, payload_reply(dic))
end

#send_content(url, payload) ⇒ Object




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

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