Class: Grin::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/grin/client.rb

Direct Known Subclasses

Album, Category, Photo

Constant Summary collapse

API_VERSION =
"v1"
DOMAIN =
"fotogger.com"

Instance Method Summary collapse

Constructor Details

#initialize(subdomain, email, password) ⇒ Client

Returns a new instance of Client.



9
10
11
# File 'lib/grin/client.rb', line 9

def initialize(subdomain, email, password)
  @@auth_string = CGI.escape(email) + ':' + CGI.escape(password) + "@" + subdomain
end

Instance Method Details

#album(id) ⇒ Object



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

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

#albumsObject



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

def albums
  albums = []
  get('albums.json').each { |album| albums << Album.new(album) }
  return albums
end

#categoriesObject



45
46
47
48
49
# File 'lib/grin/client.rb', line 45

def categories
  categories = []
  get('categories.json').each { |category| categories << Category.new(category) }
  return categories
end

#category(id) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/grin/client.rb', line 51

def category(id)
  category = get("categories/#{id}.json")
  if category.respond_to?(:[]) && category['result'] == 'failure'
    return category
  else
    Category.new(category)
  end
end

#create_album(title, category_id = categories.first.id) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/grin/client.rb', line 28

def create_album(title, category_id = categories.first.id)
  album = post("albums.json", { :album => { :title => title, :category_id => category_id } })
  if album.respond_to?(:[]) && album['result'] == 'failure'
    return album
  else
    Album.new(album)
  end
end

#create_category(name) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/grin/client.rb', line 60

def create_category(name)
  category = post("categories.json", { :category => { :name => name } })
  if category.respond_to?(:[]) && category['result'] == 'failure'
    return category
  else
    Category.new(category)
  end
end

#find_or_create_album(title, category_id) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/grin/client.rb', line 37

def find_or_create_album(title, category_id)
  if album = albums.select { |a| a.title == title }.pop
    return album
  else
    create_album(title, category_id)
  end
end

#find_or_create_category(name) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/grin/client.rb', line 69

def find_or_create_category(name)
  category = categories.select { |c| c.name == name }.pop
  if category
    return category
  else
    create_category(name)
  end
end