Class: Tumblr::Api

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

Defined Under Namespace

Classes: Exception

Class Method Summary collapse

Class Method Details

.connectionObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/tumblr/api.rb', line 6

def connection
  unless @connection
    @connection = ::Faraday.new(:url => host) do |builder|
      builder.use ::Faraday::Request::UrlEncoded
      builder.use ::Faraday::Response::Logger
      builder.use Tumblr::Params
      builder.use Tumblr::Parsing
      builder.adapter ::Faraday.default_adapter
    end
  end
  @connection
end

.get(method, params = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tumblr/api.rb', line 19

def get(method, params={})
  response = nil
  5.times do |attempt|
    response = connection.get path(method, params)
    break if response.success?
  end
  if response && response.success?
    response
  else
    error = response ? "#{response["status"]}: #{response["message"]}" : "Something went wrong"
    raise Tumblr::Api::Exception.new(error)
  end
end

.hostObject



37
38
39
# File 'lib/tumblr/api.rb', line 37

def host
  "http://api.tumblr.com"
end

.path(method = "", params = {}) ⇒ Object



41
42
43
44
45
46
# File 'lib/tumblr/api.rb', line 41

def path(method="", params={})
  uri = Addressable::URI.new
  params.keys.each { |key| params[key] = params[key].to_s }
  uri.query_values = params
  "/v2/blog/#{Tumblr.config.blog}/#{method}?#{uri.query}"
end

.url(method = '', params = {}) ⇒ Object



33
34
35
# File 'lib/tumblr/api.rb', line 33

def url(method='', params={})
  "#{host}#{path(method, params)}"
end