Class: Buttercms::BlogController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/buttercms/blog_controller.rb

Instance Method Summary collapse

Instance Method Details

#butter_authorObject



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/buttercms/blog_controller.rb', line 116

def butter_author
  response = make_butter_request("#{API_URL}authors/#{params[:author_slug]}")
  response_json = JSON.parse(response)
  @first_name = response_json['first_name']
  @last_name = response_json['last_name']
  @recent_posts = response_json['recent_posts']

  if defined? Buttercms.configuration
    if !Buttercms.configuration.author_template.nil?
      render template: Buttercms.configuration.author_template
    end
  end
end

#butter_categoryObject



130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/controllers/buttercms/blog_controller.rb', line 130

def butter_category
  response = make_butter_request("#{API_URL}categories/#{params[:category_slug]}")
  response_json = JSON.parse(response)
  @name = response_json['name']
  @recent_posts = response_json['recent_posts']

  if defined? Buttercms.configuration
    if !Buttercms.configuration.category_template.nil?
      render template: Buttercms.configuration.category_template
    end
  end
end

#butter_homeObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/buttercms/blog_controller.rb', line 84

def butter_home
  # Check for pagination
  if params[:page]
    page_param = "?page=#{params[:page]}"
  else
    page_param = ''
  end

  response = make_butter_request("#{API_URL}posts/#{page_param}")
  response_json = JSON.parse(response)
  @next_page = response_json['next_page']
  @previous_page = response_json['previous_page']
  @recent_posts = response_json['results']

  if defined? Buttercms.configuration
    if !Buttercms.configuration.home_template.nil?
      render template: Buttercms.configuration.home_template
    end
  end
end

#butter_not_foundObject

Raises:

  • (ActionController::RoutingError)


46
47
48
# File 'app/controllers/buttercms/blog_controller.rb', line 46

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

#butter_postObject



105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/buttercms/blog_controller.rb', line 105

def butter_post
  response = make_butter_request("#{API_URL}posts/#{params[:slug]}")
  @post = JSON.parse(response)

  if defined? Buttercms.configuration
    if !Buttercms.configuration.post_template.nil?
      render template: Buttercms.configuration.post_template
    end
  end
end

#get_butter_layoutObject



35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/buttercms/blog_controller.rb', line 35

def get_butter_layout
  if defined? Buttercms.configuration
    if !Buttercms.configuration.layout.nil?
      return Buttercms.configuration.layout
    end
  end

  # Default to the included layout.
  return "buttercms/application"
end

#get_butter_tokenObject

Raises:

  • (Exception)


50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/buttercms/blog_controller.rb', line 50

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/buttercms/blog_controller.rb', line 61

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 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