Class: Buttercms::BlogController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#get_layout

Instance Method Details

#authorObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/buttercms/blog_controller.rb', line 80

def author
  response = make_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

#categoryObject



94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/buttercms/blog_controller.rb', line 94

def category
  response = make_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

#get_tokenObject

Raises:

  • (Exception)


15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/buttercms/blog_controller.rb', line 15

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

#homeObject



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

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

  response = make_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

#make_request(path) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/buttercms/blog_controller.rb', line 27

def make_request(path)
  begin
    response = RestClient.get path, {:Authorization => "Token #{get_token}"}
  rescue SocketError => e
    raise Exception.new("Unable to connect to ButterCms. Please double check your internet connection.")
  rescue => e
    puts e.message

    case e.response.code
      when 404
        not_found
      when 401
        raise Exception.new("This token has not been registered. Please register one @ https://buttercms.com/api_token/")
      else
        raise Exception.new("An error occured retrieving blog. Please try again later.")
      end
  end

  return response
end

#not_foundObject

Raises:

  • (ActionController::RoutingError)


11
12
13
# File 'app/controllers/buttercms/blog_controller.rb', line 11

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

#postObject



69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/buttercms/blog_controller.rb', line 69

def post
  response = make_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