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.



70
71
72
# File 'lib/tumblr.rb', line 70

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

Class Method Details

.successful?(res) ⇒ Boolean

Returns:

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

#boundaryObject



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

#check_vimeo(options = {}) ⇒ Object



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

def check_vimeo options = {}
  post uri_write, 
       options.merge(.options).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, 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



107
108
109
110
111
# File 'lib/tumblr.rb', line 107

def get uri, options = {}
  following_redirects(uri, options) do |uri, options|
    .httpclient.get uri, options
  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, options = {}
  following_redirects(uri, options) do |uri, options|
    .httpclient.post uri, options, "content-type" => boundary
  end
end

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



80
81
82
83
84
# File 'lib/tumblr.rb', line 80

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



74
75
76
77
78
# File 'lib/tumblr.rb', line 74

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