Class: DeviantClient
- Inherits:
-
Object
- Object
- DeviantClient
- Defined in:
- lib/deviantart.rb
Constant Summary collapse
- OAUTH_PATH =
'https://www.deviantart.com/oauth2/draft15/'
- API_PATH =
'https://www.deviantart.com/api/draft15/'
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#key ⇒ Object
Returns the value of attribute key.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
#secret ⇒ Object
Returns the value of attribute secret.
Instance Method Summary collapse
- #authorization_url(params = {}) ⇒ Object
- #damntoken ⇒ Object
- #fetch(resource) ⇒ Object
- #get_token(params) ⇒ Object
-
#initialize(key = nil, secret = nil) ⇒ DeviantClient
constructor
A new instance of DeviantClient.
- #placebo ⇒ Object
- #post(resource, options = {}, filepath = nil) ⇒ Object
- #refresh ⇒ Object
- #stash_space ⇒ Object
- #submit(params) ⇒ Object
- #whoami ⇒ Object
Constructor Details
#initialize(key = nil, secret = nil) ⇒ DeviantClient
Returns a new instance of DeviantClient.
17 18 19 20 |
# File 'lib/deviantart.rb', line 17 def initialize key=nil, secret=nil @key = key @secret = secret end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
12 13 14 |
# File 'lib/deviantart.rb', line 12 def access_token @access_token end |
#key ⇒ Object
Returns the value of attribute key.
10 11 12 |
# File 'lib/deviantart.rb', line 10 def key @key end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
13 14 15 |
# File 'lib/deviantart.rb', line 13 def refresh_token @refresh_token end |
#secret ⇒ Object
Returns the value of attribute secret.
11 12 13 |
# File 'lib/deviantart.rb', line 11 def secret @secret end |
Instance Method Details
#authorization_url(params = {}) ⇒ Object
22 23 24 25 |
# File 'lib/deviantart.rb', line 22 def params = {} redirect_uri = (params.has_key? :redirect_uri) ? params[:redirect_uri] : '' url = OAUTH_PATH + 'authorize?client_id=' + @key + '&redirect_uri=' + redirect_uri + '&display=page&response_type=code' end |
#damntoken ⇒ Object
89 90 91 92 |
# File 'lib/deviantart.rb', line 89 def damntoken data = fetch 'user/damntoken' return data['damntoken'] end |
#fetch(resource) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/deviantart.rb', line 52 def fetch resource url = API_PATH + resource + '?access_token=' + @access_token #require 'pp'; pp url; begin page = open(url) data = JSON.parse(page.read) return data rescue raise 'Trying fetch resource "'+resource+'" but fail.' end end |
#get_token(params) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/deviantart.rb', line 27 def get_token params code = params[:code] ? params[:code] : '' url = OAUTH_PATH + 'token?client_id=' + @key + '&client_secret=' + @secret + '&code=' + code + '&grant_type=authorization_code' begin page = open(url) data = JSON.parse(page.read) @access_token = data['access_token'] @refresh_token = data['refresh_token'] rescue Exception=>e refresh end end |
#placebo ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/deviantart.rb', line 77 def placebo begin fetch 'placebo' rescue refresh end end |
#post(resource, options = {}, filepath = nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/deviantart.rb', line 64 def post resource, ={}, filepath=nil url = API_PATH + resource + '?access_token=' + @access_token url = url + '&' + .map{ |k,v| "#{k}="+URI::encode(v) }.join('&') if begin response = RestClient.post url, filepath ? { :file => File.new(filepath, 'rb') } : nil return JSON.parse(response.to_str) rescue Exception=>e pp e raise 'Trying post data but fail' end end |
#refresh ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/deviantart.rb', line 40 def refresh url = OAUTH_PATH + 'token?client_id=' + @key + '&client_secret=' + @secret + '&refresh_token=' + @refresh_token + '&grant_type=refresh_token' begin page = open(url) data = JSON.parse(page.read) @access_token = data['access_token'] @refresh_token = data['refresh_token'] rescue Exception=>e raise 'Trying refresh token but fail. Is refresh_token expired?' end end |
#stash_space ⇒ Object
94 95 96 97 |
# File 'lib/deviantart.rb', line 94 def stash_space data = post 'stash/space' return data['available_space'] end |
#submit(params) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/deviantart.rb', line 99 def submit params option_keys = [:title, :artist_comments, :keywords, :stashid, :folder, :folderid] = { } option_keys.each do |key| [key] = params[key] if params[key] end data = post 'stash/submit', , params[:file] data['link'] = 'http://sta.sh/0'+data['stashid'].to_i.to_s(36) return data end |
#whoami ⇒ Object
85 86 87 |
# File 'lib/deviantart.rb', line 85 def whoami fetch 'user/whoami' end |