Class: FlickrAuthentication

Inherits:
Object
  • Object
show all
Defined in:
lib/flickr_authentication.rb,
lib/flickr_authentication/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FlickrAuthentication

Returns a new instance of FlickrAuthentication.



10
11
12
13
14
# File 'lib/flickr_authentication.rb', line 10

def initialize(options = {})
  @key           = options[:key]
  @shared_secret = options[:shared_secret]
  @auth_file     = options[:auth_file]
end

Instance Attribute Details

#auth_fileObject

Returns the value of attribute auth_file.



8
9
10
# File 'lib/flickr_authentication.rb', line 8

def auth_file
  @auth_file
end

#keyObject

Returns the value of attribute key.



8
9
10
# File 'lib/flickr_authentication.rb', line 8

def key
  @key
end

#shared_secretObject

Returns the value of attribute shared_secret.



8
9
10
# File 'lib/flickr_authentication.rb', line 8

def shared_secret
  @shared_secret
end

Instance Method Details

#authenticateObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/flickr_authentication.rb', line 16

def authenticate
  FlickRaw.api_key        = key
  FlickRaw.shared_secret  = shared_secret

  if File.exists?(auth_file)
    puts "authenticating with #{auth_file}"
    data = YAML.load_file(auth_file)

    flickr.access_token   = data['access_token']
    flickr.access_secret  = data['access_secret']

    begin
      flickr.test.
    rescue Exception => e
      puts "There are bad credentials in #{@auth_file}. Delete it and try again."
    end

  else
    token    = flickr.get_request_token
    auth_url = flickr.get_authorize_url(token['oauth_token'], :perms => 'write')

    puts " "
    puts "opening your browser..."
    sleep 1
    puts "Come back and press enter the given number when you are finished"
    sleep 2
    Launchy.open(auth_url)

    verify = gets.strip

    begin
      flickr.get_access_token(token['oauth_token'], token['oauth_token_secret'], verify)
       = flickr.test.
      puts "You are now authenticated as #{.username} with token #{flickr.access_token} and secret #{flickr.access_secret}"
      # Write the YML File
      data = {}
      data["access_token"]  = flickr.access_token
      data["access_secret"] = flickr.access_secret
      File.open(auth_file, "w"){|f| YAML.dump(data, f) }
    rescue FlickRaw::FailedResponse => e
      puts "Authentication failed : #{e.msg}"
    end
  end

  flickr #return the flickr object

end