class SlideshareApi::Client

Constants

SLIDESHARE_API_URL

Attributes

api_key[RW]
connection[RW]
shared_secret[RW]

Public Class Methods

new(api_key, shared_secret) click to toggle source
# File lib/slideshare_api/client.rb, line 12
def initialize(api_key, shared_secret)
  @api_key = api_key
  @shared_secret = shared_secret
  build_connection
end

Public Instance Methods

slideshow(options = {}) click to toggle source
# File lib/slideshare_api/client.rb, line 18
def slideshow(options = {})
  params = {}
  params.merge!(slideshow_url: cleaned_url(options[:slideshow_url])) if options[:slideshow_url]
  params.merge!(slideshow_id: options[:slideshow_id]) if options[:slideshow_id]
  params.merge!(detailed: options[:detailed] ? 0 : 1) if options.has_key?(:detailed)
  SlideshareApi::Model::Slideshow.new get('get_slideshow', params)
end
slideshows(options = {}) click to toggle source
# File lib/slideshare_api/client.rb, line 26
def slideshows(options = {})
  params = {}
  if options[:tag]
    params.merge!(tag: options[:tag])
    path = 'get_slideshows_by_tag'
  elsif options[:group]
    params.merge!(group_name: options[:group])
    path = 'get_slideshows_by_group'
  elsif options[:user]
    params.merge!(username_for: options[:user])
    path = 'get_slideshows_by_user'
  else
    raise SlideshareApi::Error, 'Required Parameter Missing'
  end

  params.merge!(detailed: options[:detailed] ? 1 : 0) if options.has_key?(:detailed)
  get(path, params).search('Slideshow').map { |s| SlideshareApi::Model::Slideshow.new(s) }
end

Private Instance Methods

api_validation_params() click to toggle source
# File lib/slideshare_api/client.rb, line 84
def api_validation_params
  timestamp = Time.now.to_i
  hash = Digest::SHA1.hexdigest "#{@shared_secret}#{timestamp}"
  {api_key: @api_key, ts: timestamp, hash: hash}
end
build_connection() click to toggle source
# File lib/slideshare_api/client.rb, line 77
def build_connection
  @connection = Faraday.new(url: SLIDESHARE_API_URL) do |faraday|
    faraday.request :json
    faraday.adapter Faraday.default_adapter
  end
end
check_error(xml_response) click to toggle source
# File lib/slideshare_api/client.rb, line 72
def check_error(xml_response)
  error = xml_response.search('SlideShareServiceError')
  raise SlideshareApi::Error, xml_response.search('Message').text unless error.empty?
end
cleaned_url(url) click to toggle source
# File lib/slideshare_api/client.rb, line 68
def cleaned_url(url)
  url.split('?')[0]
end
get(path, params) click to toggle source
# File lib/slideshare_api/client.rb, line 62
def get(path, params)
  xml_response = Nokogiri::XML(@connection.get(path, api_validation_params.merge(params)).body)
  check_error xml_response
  xml_response
end