Class: Tumblr::API

Inherits:
Object
  • Object
show all
Defined in:
lib/tumblr.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



73
74
75
# File 'lib/tumblr.rb', line 73

def initialize options = {}
  options.each{|k,v| send k, v}
end

Class Method Details

.successful?(res) ⇒ Boolean

Returns:

  • (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 options = {}
  post uri_write, 
       options.merge(.options).merge('action' => 'authenticate')
end

#boundaryObject



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 options = {}
  post uri_write, 
       options.merge(.options).merge('action' => 'check-audio')
end

#check_vimeo(options = {}) ⇒ Object



100
101
102
103
# File 'lib/tumblr.rb', line 100

def check_vimeo options = {}
  post uri_write, 
       options.merge(.options).merge('action' => 'check-vimeo')
end

#delete(type, options = {}) ⇒ Object



89
90
91
92
93
# File 'lib/tumblr.rb', line 89

def delete type, options = {}
  json = options.delete('json') || options.delete(:json)
  uri = uri_delete
  post uri, options.merge(.options)
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, options = {} 
  42.times do
    res = yield uri, options
    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, options = {}
  following_redirects(uri, options) do |uri, options|
    .httpclient.get uri, options
  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, options = {}
  following_redirects(uri, options) do |uri, options|
    .httpclient.post uri, options, "content-type" => boundary
  end
end

#read(type, options = {}) ⇒ Object



83
84
85
86
87
# File 'lib/tumblr.rb', line 83

def read type, options = {}
  json = options.delete('json') || options.delete(:json)
  uri = json ? uri_read_json : uri_read
  get uri, options.merge(.options)
end

#write(type, options = {}) ⇒ Object



77
78
79
80
81
# File 'lib/tumblr.rb', line 77

def write type, options = {}
  json = options.delete('json') || options.delete(:json)
  uri = uri_write
  post uri, options.merge(.options).merge('type' => type)
end