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://api.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
71
72
73
74
75
76
# File 'app/controllers/buttercms/blog_controller.rb', line 50

def make_butter_request(path)
  begin
    response = RestClient::Request.execute(
      method: :get,
      url: "#{API_URL}#{path}",
      verify_ssl: false
    )
  rescue SocketError => e
    raise Buttercms::ConnectionError
  rescue => e
    if e.respond_to? :response
      case e.response.code
        when 404
          butter_not_found
        when 401
          raise Buttercms::TokenError
        else
          raise Buttercms::InvalidResponseError
        end
    else
      # In the event that e doesn't have a response due to network issue (Errno::ENETUNREACH)
      raise Buttercms::ConnectionError
    end

  end
  return response
end