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
- #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.
70 71 72 |
# File 'lib/tumblr.rb', line 70 def initialize = {} .each{|k,v| send k, v} end |
Class Method Details
.successful?(res) ⇒ Boolean
138 139 140 |
# File 'lib/tumblr.rb', line 138 def self.successful? res HTTP::Status.successful? res.status end |
Instance Method Details
#authenticate(options = {}) ⇒ Object
86 87 88 89 |
# File 'lib/tumblr.rb', line 86 def authenticate = {} post uri_write, .merge(account.).merge('action' => 'authenticate') end |
#boundary ⇒ Object
113 114 115 116 |
# File 'lib/tumblr.rb', line 113 def boundary random = Array::new(8){ "%2.2d" % rand(42) }.join("__") "multipart/form-data; boundary=___#{ random }___" end |
#check_audio(options = {}) ⇒ Object
96 97 98 99 |
# File 'lib/tumblr.rb', line 96 def check_audio = {} post uri_write, .merge(account.).merge('action' => 'check-audio') end |
#check_vimeo(options = {}) ⇒ Object
91 92 93 94 |
# File 'lib/tumblr.rb', line 91 def check_vimeo = {} post uri_write, .merge(account.).merge('action' => 'check-vimeo') end |
#following_redirects(uri, options = {}) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/tumblr.rb', line 118 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
107 108 109 110 111 |
# File 'lib/tumblr.rb', line 107 def get uri, = {} following_redirects(uri, ) do |uri, | account.httpclient.get uri, end end |
#handle_redirect(uri, res) ⇒ Object
132 133 134 135 136 |
# File 'lib/tumblr.rb', line 132 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
101 102 103 104 105 |
# File 'lib/tumblr.rb', line 101 def post uri, = {} following_redirects(uri, ) do |uri, | account.httpclient.post uri, , "content-type" => boundary end end |
#read(type, options = {}) ⇒ Object
80 81 82 83 84 |
# File 'lib/tumblr.rb', line 80 def read type, = {} json = .delete('json') || .delete(:json) uri = json ? uri_read_json : uri_read get uri, .merge(account.) end |
#write(type, options = {}) ⇒ Object
74 75 76 77 78 |
# File 'lib/tumblr.rb', line 74 def write type, = {} json = .delete('json') || .delete(:json) uri = uri_write post uri, .merge(account.).merge('type' => type) end |