Class: Tumblr::API
- Inherits:
-
Object
- Object
- Tumblr::API
- Defined in:
- lib/tumblr.rb
Class Method Summary collapse
Instance Method Summary collapse
- #authenticate(options = {}) ⇒ Object
- #boundary ⇒ Object
- #check_audio(options = {}) ⇒ Object
- #check_vimeo(options = {}) ⇒ Object
- #delete(type, options = {}) ⇒ Object
- #following_redirects(uri, options = {}) ⇒ Object
- #get(uri, options = {}) ⇒ Object
- #handle_redirect(uri, res) ⇒ Object
-
#initialize(options = {}) ⇒ API
constructor
A new instance of API.
- #post(uri, options = {}) ⇒ Object
- #read(type, options = {}) ⇒ Object
- #write(type, options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ API
Returns a new instance of API.
73 74 75 |
# File 'lib/tumblr.rb', line 73 def initialize = {} .each{|k,v| send k, v} end |
Class Method Details
.successful?(res) ⇒ Boolean
147 148 149 |
# File 'lib/tumblr.rb', line 147 def self.successful? res HTTP::Status.successful? res.status end |
Instance Method Details
#authenticate(options = {}) ⇒ Object
95 96 97 98 |
# File 'lib/tumblr.rb', line 95 def authenticate = {} post uri_write, .merge(account.).merge('action' => 'authenticate') end |
#boundary ⇒ Object
122 123 124 125 |
# File 'lib/tumblr.rb', line 122 def boundary random = Array::new(8){ "%2.2d" % rand(42) }.join("__") "multipart/form-data; boundary=___#{ random }___" end |
#check_audio(options = {}) ⇒ Object
105 106 107 108 |
# File 'lib/tumblr.rb', line 105 def check_audio = {} post uri_write, .merge(account.).merge('action' => 'check-audio') end |
#check_vimeo(options = {}) ⇒ Object
100 101 102 103 |
# File 'lib/tumblr.rb', line 100 def check_vimeo = {} post uri_write, .merge(account.).merge('action' => 'check-vimeo') end |
#delete(type, options = {}) ⇒ Object
89 90 91 92 93 |
# File 'lib/tumblr.rb', line 89 def delete type, = {} json = .delete('json') || .delete(:json) uri = uri_delete post uri, .merge(account.) end |
#following_redirects(uri, options = {}) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/tumblr.rb', line 127 def following_redirects uri, = {} 42.times do res = yield uri, if HTTP::Status.successful?(res.status) return res elsif HTTP::Status.redirect?(res.status) uri = handle_redirect uri, res else return res end end raise "followed too damn many redirects!" end |
#get(uri, options = {}) ⇒ Object
116 117 118 119 120 |
# File 'lib/tumblr.rb', line 116 def get uri, = {} following_redirects(uri, ) do |uri, | account.httpclient.get uri, end end |
#handle_redirect(uri, res) ⇒ Object
141 142 143 144 145 |
# File 'lib/tumblr.rb', line 141 def handle_redirect uri, res newuri = URI.parse res.header['location'][0] newuri = uri + newuri unless newuri.is_a?(URI::HTTP) newuri end |
#post(uri, options = {}) ⇒ Object
110 111 112 113 114 |
# File 'lib/tumblr.rb', line 110 def post uri, = {} following_redirects(uri, ) do |uri, | account.httpclient.post uri, , "content-type" => boundary end end |
#read(type, options = {}) ⇒ Object
83 84 85 86 87 |
# File 'lib/tumblr.rb', line 83 def read type, = {} json = .delete('json') || .delete(:json) uri = json ? uri_read_json : uri_read get uri, .merge(account.) end |
#write(type, options = {}) ⇒ Object
77 78 79 80 81 |
# File 'lib/tumblr.rb', line 77 def write type, = {} json = .delete('json') || .delete(:json) uri = uri_write post uri, .merge(account.).merge('type' => type) end |