-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmugmug_client.rb
79 lines (64 loc) · 1.96 KB
/
smugmug_client.rb
1
2
3
4
5
6
7
8
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
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require 'bundler/setup'
require 'ruby-smugmug'
Bundler.require
require_relative './configuration'
class SmugmugClient
attr_accessor :req_token, :api_key, :api_secret, :config, :consumer, :client, :default_album, :default_album_id
def initialize(config)
self.config = config
self.consumer = OAuth::Consumer.new(config.api_key, config.api_secret,
{ :site => 'https://api.smugmug.com',
:request_token_path => "/services/oauth/getRequestToken.mg",
:access_token_path => "/services/oauth/getAccessToken.mg",
:authorize_path => "/services/oauth/authorize.mg",
:proxy => "http://localhost:3128"
})
self.client = SmugMug::Client.new(:api_key => config.api_key,
:oauth_secret => config.api_secret,
:user => {:token => config.user_token, :secret => config.user_secret},
:http => {:proxy_host => 'localhost', :proxy_port => 3128}
)
#self.default_album = "SmugUp"
self.default_album_id = 28175709
end
def valid_api_token?
begin
self.req_token = consumer.get_request_token
req_token.class == OAuth::RequestToken
rescue Exception => e
false
end
end
def connectable?
begin
client.auth.checkAccessToken
rescue Exception => e
false
end
end
def album_exists?(album)
begin
!!client.albums.get.detect { | h| h["Title"] == album }
rescue Exception => e
false
end
end
def full_access?
begin
client.auth.checkAccessToken["Token"]["Access"] == "Full"
rescue Exception => e
false
end
end
def get_album_id(album)
a = client.albums.get.detect { | h| h["Title"] == album }
a["id"]
end
def upload(file)
begin
client.upload_media(:file => file, :AlbumID => default_album_id)
rescue Exception => e
false
end
end
end