# File lib/rabbit/slideshare.rb, line 40 def initialize(logger) @logger = logger @user = nil @pdf_path = nil @id = nil @title = nil @description = nil @tags = [] @connection = Faraday.new(:url => BASE_URL) do |builder| builder.request :multipart builder.request :url_encoded builder.response :logger, @logger builder.adapter :net_http end end
# File lib/rabbit/slideshare.rb, line 56 def upload slideshow_id = nil begin slideshow_id = upload_slide rescue Error @logger.error(_("Feailed to upload: %s") % $!.message) return nil end begin edit_title(slideshow_id) rescue Error @logger.error(_("Feailed to edit title: %s") % $!.message) return nil end url = nil begin url = slide_url(slideshow_id) rescue Error @logger.error(_("Feailed to get slide URL: %s") % $!.message) return nil end url end
# File lib/rabbit/slideshare.rb, line 132 def api_url(command) "#{API_PATH_PREFIX}/#{command}" end
# File lib/rabbit/slideshare.rb, line 155 def common_payload timestamp = Time.now.to_i.to_s { :api_key => API_KEY, :ts => timestamp, :hash => Digest::SHA1.hexdigest("#{SHARED_SECRET}#{timestamp}"), } end
# File lib/rabbit/slideshare.rb, line 97 def edit_title(slideshow_id) payload = { :username => @user, :password => password, :slideshow_id => slideshow_id, :slideshow_title => @title, } response = get("edit_slideshow", payload) parse_edit_slideshow_response(response) end
# File lib/rabbit/slideshare.rb, line 124 def get(command, payload) @connection.get(api_url(command), prepare_payload(payload)) end
# File lib/rabbit/slideshare.rb, line 185 def parse_edit_slideshow_response(http_response) response = parse_response(http_response) response.xpath("/SlideShowEdited/SlideShowID").text.to_i end
# File lib/rabbit/slideshare.rb, line 190 def parse_get_slideshow_response(http_response) response = parse_response(http_response) response.xpath("/Slideshow/URL").text end
# File lib/rabbit/slideshare.rb, line 164 def parse_response(http_response) @logger.debug(http_response.body) unless http_response.success? raise Error, "#{http_response.status}\n#{http_response.body}" end response = Nokogiri::XML(http_response.body) if response.root.name == "SlideShareServiceError" message = response.root.elements[0] raise Error, message end response end
# File lib/rabbit/slideshare.rb, line 180 def parse_upload_slideshow_response(http_response) response = parse_response(http_response) response.xpath("/SlideShowUploaded/SlideShowID").text.to_i end
# File lib/rabbit/slideshare.rb, line 140 def password @password ||= read_password(_("Enter password on SlideShare")) end
# File lib/rabbit/slideshare.rb, line 128 def post(command, payload) @connection.post(api_url(command), prepare_payload(payload)) end
# File lib/rabbit/slideshare.rb, line 116 def prepare_payload(payload) payload = common_payload.merge(payload) payload.keys.each do |key| payload.delete(key) if payload[key].nil? end payload end
# File lib/rabbit/slideshare.rb, line 144 def read_password(prompt) print("%s [%s]: " % [prompt, @user]) system("/bin/stty -echo") if $stdin.tty? $stdin.gets.chomp ensure if $stdin.tty? system("/bin/stty echo") puts end end
# File lib/rabbit/slideshare.rb, line 108 def slide_url(slideshow_id) payload = { :slideshow_id => slideshow_id, } response = get("get_slideshow", payload) parse_get_slideshow_response(response) end
# File lib/rabbit/slideshare.rb, line 83 def upload_slide payload = { :username => @user, :password => password, :slideshow_title => upload_title, :slideshow_srcfile => Faraday::UploadIO.new(@pdf_path, "application/pdf"), :slideshow_description => @description, :tags => @tags.join(","), } response = post("upload_slideshow", payload) parse_upload_slideshow_response(response) end
# File lib/rabbit/slideshare.rb, line 136 def upload_title @id.gsub(/-/, " ") end