Class: Buttercms::BlogController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- Buttercms::BlogController
- Defined in:
- app/controllers/buttercms/blog_controller.rb
Instance Method Summary collapse
- #butter_author ⇒ Object
- #butter_category ⇒ Object
- #butter_home ⇒ Object
- #butter_not_found ⇒ Object
- #butter_post ⇒ Object
- #get_butter_layout ⇒ Object
- #get_butter_token ⇒ Object
- #make_butter_request(path) ⇒ Object
Instance Method Details
#butter_author ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/controllers/buttercms/blog_controller.rb', line 93 def 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..nil? render template: Buttercms.configuration. end end end |
#butter_category ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/controllers/buttercms/blog_controller.rb', line 107 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_home ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/controllers/buttercms/blog_controller.rb', line 61 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_found ⇒ Object
25 26 27 |
# File 'app/controllers/buttercms/blog_controller.rb', line 25 def butter_not_found raise ActionController::RoutingError.new('Not Found') end |
#butter_post ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'app/controllers/buttercms/blog_controller.rb', line 82 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_layout ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'app/controllers/buttercms/blog_controller.rb', line 14 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_token ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'app/controllers/buttercms/blog_controller.rb', line 29 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
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/controllers/buttercms/blog_controller.rb', line 40 def make_butter_request(path) begin response = RestClient.get path, {:Authorization => "Token #{get_butter_token}"} rescue SocketError => e raise Exception.new("Unable to connect to ButterCms. Please double check your internet connection.") rescue => e puts e. case e.response.code when 404 butter_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 |