lib/ayadn/pinboard.rb in ayadn-0.6.4 vs lib/ayadn/pinboard.rb in ayadn-1.0.0

- old
+ new

@@ -1,37 +1,48 @@ -#!/usr/bin/env ruby -require 'base64' -require 'open-uri' - # encoding: utf-8 -class AyaDN - def ayadnBookmark(*args) - post_id = args[0][1] - tags = args[0][2] - hash = @api.getSinglePost(post_id) - data = hash['data'] - post_text = data['text'] - user_name = data['user']['username'] - link = data['entities']['links'][0]['url'] - if $tools.config['pinboard']['username'] != nil - puts "\nSaving post ".green + post_id.brown + " to Pinboard...\n".green - $tools.saveToPinboard(post_id, $tools.config['pinboard']['username'], URI.unescape(Base64::decode64($tools.config['pinboard']['password'])), link, tags, post_text, user_name) - puts "Done!\n\n".green - else - puts "\nConfiguration does not include your Pinbard credentials.\n".red - begin - puts "Please enter your Pinboard username (CTRL+C to cancel): ".green - pin_username = STDIN.gets.chomp() - puts "\nPlease enter your Pinboard password (invisible, CTRL+C to cancel): ".green - pin_password = STDIN.noecho(&:gets).chomp() - rescue Exception - abort($status.stopped) - end - $tools.config['pinboard']['username'] = pin_username - $tools.config['pinboard']['password'] = URI.escape(Base64::encode64(pin_password)) - $tools.saveConfig - puts "Saving post ".green + post_id.brown + " to Pinboard...\n".green - $tools.saveToPinboard(post_id, pin_username, pin_password, link, tags, post_text, user_name) - puts "Done!\n\n".green - end - end +module Ayadn + class PinBoard + + def has_credentials_file? + File.exist?(Ayadn::Settings.config[:paths][:db] + '/ayadn_pinboard.db') + end + + def ask_credentials + begin + puts "Please enter your Pinboard username (CTRL+C to cancel): ".color(:green) + pin_username = STDIN.gets.chomp() + puts "\nPlease enter your Pinboard password (invisible, CTRL+C to cancel): ".color(:green) + pin_password = STDIN.noecho(&:gets).chomp() + rescue Interrupt + abort(Status.canceled) + rescue => e + puts Status.wtf + Errors.global_error("pinboard/ask_credentials", pin_username, e) + end + save_credentials(encode(pin_username, pin_password)) + end + + def load_credentials + decode(File.read(Ayadn::Settings.config[:paths][:db] + '/ayadn_pinboard.db')) + end + + def pin(data) + pinboard = Pinboard::Client.new(:username => data.username, :password => data.password) + pinboard.add(:url => data.url, :tags => data.tags, :extended => data.text, :description => data.description) + end + + private + + def decode(encoded_pinboard_credentials) + Base64.strict_decode64(encoded_pinboard_credentials[/AyadnPinboard (.*)/, 1]).split(":") + end + + def save_credentials(encoded_pinboard_credentials) + File.write(Ayadn::Settings.config[:paths][:db] + '/ayadn_pinboard.db', encoded_pinboard_credentials) + end + + def encode(username, password) + "AyadnPinboard #{Base64.strict_encode64([username, password].join(":"))}" + end + + end end