lib/ayadn/post.rb in ayadn-1.6.0 vs lib/ayadn/post.rb in ayadn-1.7.0

- old
+ new

@@ -1,186 +1,90 @@ # encoding: utf-8 module Ayadn class Post - def post(args) - unless text_is_empty?(args) - send_post(args.join(" ")) - else - error_text_empty - end + def post(dic) + send_content(Endpoints.new.posts_url, payload_basic(dic)) end - def compose - readline() + 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 + dic[:text] = reply + dic[:reply_to] = dic[:id] + send_content(Endpoints.new.posts_url, payload_reply(dic)) end - def send_embedded text, files - send_embedded_pictures({'text' => text, 'data' => FileOps.upload_files(files)}) + def pm(dic) + send_content(Endpoints.new.pm_url, payload_pm(dic)) end - def send_reply_embedded text, reply_to, files - send_reply_embedded_pictures({'text' => text, 'reply_to' => reply_to, 'data' => FileOps.upload_files(files)}) + def message(dic) + send_content(Endpoints.new.messages(dic[:id]), payload_basic(dic)) end - def send_pm_embedded username, text, files - send_pm_embedded_pictures({'text' => text, 'username' => username, 'data' => FileOps.upload_files(files)}) - end + # ----- - def send_embedded_pictures dic - send_content(Endpoints.new.posts_url, payload_embedded(dic)) - end - - def send_reply_embedded_pictures dic - send_content(Endpoints.new.posts_url, payload_reply_embedded(dic)) - end - - def send_pm_embedded_pictures dic - send_content(Endpoints.new.pm_url, payload_pm_embedded(dic)) - end - - def send_nowplaying dic - send_content(Endpoints.new.posts_url, payload_nowplaying(dic)) - end - - def send_movie dic - send_content(Endpoints.new.posts_url, payload_movie(dic)) - end - - def send_tvshow dic - send_content(Endpoints.new.posts_url, payload_tvshow(dic)) - end - - def payload_movie dic - ann = annotations_embedded(dic) - ann << { - "type" => "com.ayadn.movie", - "value" => { - "title" => dic['title'], - "source" => dic['source'] - } - } + def payload_basic(dic) { - "text" => dic['text'], - "entities" => entities, - "annotations" => ann + "text" => dic[:text], + "entities" => entities(), + "annotations" => Annotations.new(dic).content } end - def payload_tvshow dic - ann = annotations_embedded(dic) - ann << { - "type" => "com.ayadn.tvshow", - "value" => { - "title" => dic['title'], - "source" => dic['source'] - } - } + def payload_pm(dic) { - "text" => dic['text'], - "entities" => entities, - "annotations" => ann + "text" => dic[:text], + "entities" => entities(), + "destinations" => dic[:username], + "annotations" => Annotations.new(dic).content } end - 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 + def payload_reply(dic) { - "text" => dic['text'], - "entities" => entities, - "annotations" => ann + "text" => dic[:text], + "reply_to" => dic[:reply_to], + "entities" => entities(), + "annotations" => Annotations.new(dic).content } end - def payload_embedded dic + def entities { - "text" => dic['text'], - "entities" => entities, - "annotations" => annotations_embedded(dic) + "parse_markdown_links" => true, + "parse_links" => true } end - def payload_reply_embedded dic - { - "text" => dic['text'], - "reply_to" => dic['reply_to'], - "entities" => entities, - "annotations" => annotations_embedded(dic) - } - end + # ----- - def payload_pm_embedded dic - { - "text" => dic['text'], - "entities" => entities, - "destinations" => dic['username'], - "annotations" => annotations_embedded(dic) - } + def send_content(url, payload) + url << "?include_annotations=1&access_token=#{Ayadn::Settings.user_token}" + JSON.parse(CNX.post(url, payload)) end - 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 + # ----- + + def compose + readline() end 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) + post({text: buffer}) puts Status.done end rescue Interrupt abort(Status.canceled) end @@ -198,49 +102,10 @@ abort(Status.canceled) end post end - 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 - - def send_pm(username, text) - send_content(Endpoints.new.pm_url, payload_pm(username, text)) - end - - def send_message(channel_id, text) - send_content(Endpoints.new.messages(channel_id, {}), payload_basic(text)) - end - - # def send_log(data) - # url = Endpoints.new.ayadnlog - # send_content(url, payload_log(data)) - # end - - def send_post(text) - send_content(Endpoints.new.posts_url, payload_basic(text)) - end - - def send_reply(text, post_id) - send_content(Endpoints.new.posts_url, payload_reply(text, post_id)) - end - - def send_content(url, payload) - url << "?include_annotations=1&access_token=#{Ayadn::Settings.user_token}" - JSON.parse(CNX.post(url, payload)) - end - def check_post_length(lines_array) check_length(lines_array, Settings.config[:post_max_length]) end def check_message_length(lines_array) @@ -275,83 +140,8 @@ def error_text_empty puts Status.no_text Errors.warn "-Post without text-" end - - 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" => "eric@aya.io" - }, - "version" => "#{Settings.config[:version]}" - } - } - ] - end - - def entities - { - "parse_markdown_links" => true, - "parse_links" => true - } - end - - - - def payload_basic(text) - { - "text" => text, - "entities" => entities, - "annotations" => annotations - } - end - - def payload_pm(username, text) - { - "text" => text, - "entities" => entities, - "destinations" => username, - "annotations" => annotations - } - end - - def payload_reply(text, reply_to) - { - "text" => text, - "reply_to" => reply_to, - "entities" => entities, - "annotations" => annotations - } - end - - # def payload_log(data) - # extended = annotations - # extended << { - # "type" => "com.ayadn.log", - # "value" => data - # } - # return { - # "text" => "#ayadnlog", - # "entities" => entities, - # "annotations" => extended - # } - # end end end