Class: FlickrClient

Inherits:
Object
  • Object
show all
Defined in:
lib/flickrup/flickr_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key, shared_secret) ⇒ FlickrClient

Returns a new instance of FlickrClient.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/flickrup/flickr_client.rb', line 6

def initialize(api_key, shared_secret)
  @set_name_to_id = {}
  @set_id_to_date = {}
  FlickRaw.api_key= api_key
  FlickRaw.shared_secret=shared_secret

  @token_file = File.join(ENV['HOME'], ".flickrup")
  set_client(FlickRaw::Flickr.new)

  if File.exist?(@token_file)
    token = YAML.load_file(@token_file)
    client.access_token = token["access"]
    client.access_secret = token["secret"]
  end

  begin
     = client.test.
  rescue FlickRaw::OAuthClient::FailedResponse => e
    get_token
  end
end

Instance Method Details

#add_to_set(photo_id, set_name, photo_date_taken = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/flickrup/flickr_client.rb', line 48

def add_to_set(photo_id, set_name, photo_date_taken = nil)
  id = id_from_cache(set_name)

  if id
    client.photosets.addPhoto(:photoset_id => @set_name_to_id[set_name], :photo_id => photo_id)
  else #if we still don't, create it
    created = client.photosets.create(:title => set_name, :primary_photo_id => photo_id)
    @set_name_to_id = @set_name_to_id.merge({set_name => created.id})
    if photo_date_taken
      @set_id_to_date[created.id] = photo_date_taken
    end
  end
end

#clientObject



28
29
30
# File 'lib/flickrup/flickr_client.rb', line 28

def client
  @client
end

#date_of_set(set_name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/flickrup/flickr_client.rb', line 36

def date_of_set(set_name)
  id = id_from_cache(set_name)

  unless @set_id_to_date.include? id
    info = client.photosets.getInfo(:photoset_id => id)
    primary = client.photos.getInfo(:photo_id => info.primary)
    @set_id_to_date[id] = DateTime.strptime(primary.dates.taken, '%Y-%m-%d %H:%M:%S')
  end

  return @set_id_to_date[id]
end

#get_tokenObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/flickrup/flickr_client.rb', line 62

def get_token
  token = client.get_request_token
  auth_url = client.get_authorize_url(token['oauth_token'], :perms => 'write')

  puts "Open this url in your process to complete the authentication process : #{auth_url}"
  puts "Copy here the number given when you complete the process."
  verify = gets.strip

  begin
    client.get_access_token(token['oauth_token'], token['oauth_token_secret'], verify)
    token = {"access" => client.access_token, "secret" => client.access_secret}
     = client.test.
    puts "You are now authenticated as #{.username}"
    File.open(@token_file, 'w') { |f| f.write(token.to_yaml) }
  rescue FlickRaw::OAuthClient::FailedResponse => e
    puts "Authentication failed : #{e.msg}"
  end

end

#upload_photo(image, config) ⇒ Object



32
33
34
# File 'lib/flickrup/flickr_client.rb', line 32

def upload_photo(image, config)
  client.upload_photo(image, config)
end