class Rabbit::Task::SlideShare

Constants

API_KEY
API_PATH_PREFIX
BASE_URL
SHARED_SECRET

Attributes

description[RW]
id[RW]
pdf_path[RW]
tags[RW]
title[RW]
user[RW]

Public Class Methods

new(logger) click to toggle source
# File lib/rabbit/slideshare.rb, line 41
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

Public Instance Methods

upload() click to toggle source
# File lib/rabbit/slideshare.rb, line 57
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

Private Instance Methods

api_url(command) click to toggle source
# File lib/rabbit/slideshare.rb, line 133
def api_url(command)
  "#{API_PATH_PREFIX}/#{command}"
end
common_payload() click to toggle source
# File lib/rabbit/slideshare.rb, line 151
def common_payload
  timestamp = Time.now.to_i.to_s
  {
    :api_key => API_KEY,
    :ts => timestamp,
    :hash => Digest::SHA1.hexdigest("#{SHARED_SECRET}#{timestamp}"),
  }
end
edit_title(slideshow_id) click to toggle source
# File lib/rabbit/slideshare.rb, line 98
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
get(command, payload) click to toggle source
# File lib/rabbit/slideshare.rb, line 125
def get(command, payload)
  @connection.get(api_url(command), prepare_payload(payload))
end
parse_edit_slideshow_response(http_response) click to toggle source
# File lib/rabbit/slideshare.rb, line 181
def parse_edit_slideshow_response(http_response)
  response = parse_response(http_response)
  response.xpath("/SlideShowEdited/SlideShowID").text.to_i
end
parse_get_slideshow_response(http_response) click to toggle source
# File lib/rabbit/slideshare.rb, line 186
def parse_get_slideshow_response(http_response)
  response = parse_response(http_response)
  response.xpath("/Slideshow/URL").text
end
parse_response(http_response) click to toggle source
# File lib/rabbit/slideshare.rb, line 160
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
parse_upload_slideshow_response(http_response) click to toggle source
# File lib/rabbit/slideshare.rb, line 176
def parse_upload_slideshow_response(http_response)
  response = parse_response(http_response)
  response.xpath("/SlideShowUploaded/SlideShowID").text.to_i
end
password() click to toggle source
# File lib/rabbit/slideshare.rb, line 141
def password
  @password ||= read_password
end
post(command, payload) click to toggle source
# File lib/rabbit/slideshare.rb, line 129
def post(command, payload)
  @connection.post(api_url(command), prepare_payload(payload))
end
prepare_payload(payload) click to toggle source
# File lib/rabbit/slideshare.rb, line 117
def prepare_payload(payload)
  payload = common_payload.merge(payload)
  payload.keys.each do |key|
    payload.delete(key) if payload[key].nil?
  end
  payload
end
read_password() click to toggle source
# File lib/rabbit/slideshare.rb, line 145
def read_password
  prompt = _("Enter password on SlideShare [%{user}]: ") % {:user => @user}
  reader = PasswordReader.new(prompt)
  reader.read
end
slide_url(slideshow_id) click to toggle source
# File lib/rabbit/slideshare.rb, line 109
def slide_url(slideshow_id)
  payload = {
    :slideshow_id => slideshow_id,
  }
  response = get("get_slideshow", payload)
  parse_get_slideshow_response(response)
end
upload_slide() click to toggle source
# File lib/rabbit/slideshare.rb, line 84
def upload_slide
  payload = {
    :username              => @user,
    :password              => password,
    :slideshow_title       => upload_title,
    :slideshow_srcfile     => Faraday::UploadIO.new(@pdf_path,
                                                    "application/pdf"),
    :slideshow_description => @description,
    :slideshow_tags        => @tags.join(","),
  }
  response = post("upload_slideshow", payload)
  parse_upload_slideshow_response(response)
end
upload_title() click to toggle source
# File lib/rabbit/slideshare.rb, line 137
def upload_title
  @id.gsub(/-/, " ")
end