Class: Grin::Album

Inherits:
Client show all
Defined in:
lib/grin/album.rb

Constant Summary

Constants inherited from Client

Client::API_VERSION, Client::DOMAIN

Instance Method Summary collapse

Methods inherited from Client

#album, #albums, #categories, #category, #create_album, #create_category, #find_or_create_album, #find_or_create_category

Constructor Details

#initialize(data) ⇒ Album

Returns a new instance of Album.



4
5
6
7
8
9
10
11
# File 'lib/grin/album.rb', line 4

def initialize(data)
  data.each do |key, value|
    instance_variable_set("@#{key}", value)
    Album.instance_eval do
      attr_reader key.to_sym
    end
  end
end

Instance Method Details

#create_photo(image_data) ⇒ Object



28
29
30
# File 'lib/grin/album.rb', line 28

def create_photo(image_data)
  post("albums/#{id}/photos.json", { :photo => { :image => image_data } })
end

#destroyObject



40
41
42
# File 'lib/grin/album.rb', line 40

def destroy
  delete("albums/#{id}.json")
end

#find_or_create_photo(filename, image_data) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/grin/album.rb', line 32

def find_or_create_photo(filename, image_data)
  if photo = photos.select { |p| p.filename == filename }.pop
    return photo
  else
    create_photo(image_data)
  end
end

#photo(photo_id) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/grin/album.rb', line 19

def photo(photo_id)
  photo = get("albums/#{id}/photos/#{photo_id}.json")
  if photo.respond_to?(:[]) && photo['result'] == 'failure'
    return photo
  else
    Photo.new(photo)
  end
end

#photosObject



13
14
15
16
17
# File 'lib/grin/album.rb', line 13

def photos
  photos = []
  get("albums/#{id}/photos.json").each { |photo| photos << Photo.new(photo) }
  return photos
end