Module: Buttercms::API

Included in:
BlogController
Defined in:
app/controllers/buttercms/blog_controller.rb

Overview

Thin wrapper around RestClient for talking to Butter API

Constant Summary collapse

API_URL =
'https://buttercms.com/api/'

Instance Method Summary collapse

Instance Method Details

#butter_not_foundObject

Raises:

  • (ActionController::RoutingError)


35
36
37
# File 'app/controllers/buttercms/blog_controller.rb', line 35

def butter_not_found
  raise ActionController::RoutingError.new('Not Found')
end

#get_butter_tokenObject

Raises:

  • (Exception)


39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/buttercms/blog_controller.rb', line 39

def get_butter_token
  # Make sure the Buttercms has been initialized.
  if defined? Buttercms.configuration
    if defined? Buttercms.configuration.token
      return Buttercms.configuration.token
    end
  end

  raise Exception.new("/config/intitializer/butter.rb is missing. Please run $ rails g buttercms:install")
end

#make_butter_request(path) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/buttercms/blog_controller.rb', line 50

def make_butter_request(path)
  begin
    # Get the content. If it takes longer than 10 seconds timeout.
    # TODO(jlumetta): Make :timeout configurable.
    response = RestClient.get "#{API_URL}#{path}", {:Authorization => "Token #{get_butter_token}", :timeout => 10}
  rescue SocketError => e
    raise Buttercms::ConnectionError
  rescue => e
    puts e.message

    case e.response.code
      when 404
        butter_not_found
      when 401
        raise Buttercms::TokenError
      else
        raise Buttercms::InvalidResponseError
      end
  end
  return response
end