Class: Slideshowpro::Director

Inherits:
Object
  • Object
show all
Defined in:
lib/slideshowpro/director.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, api_key, cache = nil) ⇒ Director

Returns a new instance of Director.



4
5
6
7
8
# File 'lib/slideshowpro/director.rb', line 4

def initialize(url, api_key, cache=nil)
  self.url = url
  self.api_key = api_key
  self.cache = cache
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



3
4
5
# File 'lib/slideshowpro/director.rb', line 3

def api_key
  @api_key
end

#cacheObject

Returns the value of attribute cache.



3
4
5
# File 'lib/slideshowpro/director.rb', line 3

def cache
  @cache
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/slideshowpro/director.rb', line 3

def url
  @url
end

Instance Method Details

#format_hash(formats) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/slideshowpro/director.rb', line 44

def format_hash(formats)
  default_format_params = {:crop=>1, :quality=>'90', :sharpening=>1}
  r = {}
  formats.each do |name, format|
    f = default_format_params.merge(format)
    if name==:preview
      r["data[preview]"] = "#{f[:size].gsub('x',',')},#{f[:crop]},#{f[:quality]},#{f[:sharpening]}"
    else
      r["data[size][#{name}]"] = "#{name},#{f[:size].gsub('x',',')},#{f[:crop]},#{f[:quality]},#{f[:sharpening]}"
    end
  end
  r
end

#get_album(album_id, formats = {:thumb=>{:size=>'41x41',:quality=> 85},:large=>{:crop=>0,:size=>'750x750', :sharpening=>0}}) ⇒ Object



36
37
38
39
# File 'lib/slideshowpro/director.rb', line 36

def get_album(album_id, formats={:thumb=>{:size=>'41x41',:quality=> 85},:large=>{:crop=>0,:size=>'750x750', :sharpening=>0}})
  raw = post('get_album', format_hash(formats).merge('data[album_id]'=>album_id))
  raw["data"]["contents"]
end


40
41
42
43
# File 'lib/slideshowpro/director.rb', line 40

def get_gallery(gallery_id, formats={:preview=>{:size => '123x35',:crop => 1, :quality => 90}})
  raw = post('get_gallery', format_hash(formats).merge('data[gallery_id]'=>gallery_id))
  raw["data"]["albums"]
end

#post(method, options) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/slideshowpro/director.rb', line 9

def post(method, options)
  params = {
    'data[api_key]'=>self.api_key, 
    'data[breaker]'=>'true', 
    'data[format]'=>'json'
  }
  params.merge!(options)
  data = URI.escape(params.map {|k,v| "#{URI.escape(k)}=#{URI.escape(v.to_s)}"}.join("&"))

  if defined?(self.cache) && self.cache.respond_to?(:get)
    require 'digest/md5'
    data_key = Digest::MD5.hexdigest(method + "|" + data)
    begin
    json=self.cache.get(data_key)
    rescue Memcached::NotFound
      json = get_json(url, method, data)
      self.cache.set(data_key, json)
    rescue Memcached::ServerIsMarkedDead
      puts "Memcache Down!"
      #fall back to get data directly
      json = get_json(url, method, data)
    end
  else
    json = get_json(url, method, data)
  end
  return json
end