module WordpressRailsConnection
Public Class Methods
get_first_different_post(post_id,posts_response)
click to toggle source
# File lib/wordpress-rails-connection.rb, line 49 def self.get_first_different_post(post_id,posts_response) return nil if posts_response['found'] <= 0 posts_response['posts'].each do |p| if p['ID'].to_s != post_id return p end end nil end
get_next_post(date, post_id)
click to toggle source
# File lib/wordpress-rails-connection.rb, line 42 def self.get_next_post(date, post_id) date = DateTime.parse(date).strftime('%F') path = "/posts?after=#{date}&order_by=date&order=ASC" posts_response = WordpressRailsConnection.initialize(path) WordpressRailsConnection.get_first_different_post(post_id, posts_response) end
get_post_by_id(post_id)
click to toggle source
# File lib/wordpress-rails-connection.rb, line 30 def self.get_post_by_id(post_id) path = "/posts/#{post_id}" WordpressRailsConnection.initialize(path) end
get_post_by_slug(slug)
click to toggle source
# File lib/wordpress-rails-connection.rb, line 25 def self.get_post_by_slug(slug) path = "/posts/slug:#{slug}" WordpressRailsConnection.initialize(path) end
get_previous_post(date, post_id)
click to toggle source
# File lib/wordpress-rails-connection.rb, line 35 def self.get_previous_post(date, post_id) date = DateTime.parse(date).strftime('%F') path = "/posts?before=#{date}&order_by=date&order=DESC" posts_response = WordpressRailsConnection.initialize(path) WordpressRailsConnection.get_first_different_post(post_id, posts_response) end
initialize(path = "", expires_in=6.hours)
click to toggle source
# File lib/wordpress-rails-connection.rb, line 9 def self.initialize(path = "", expires_in=6.hours) Rails.cache.fetch("#{path}", :expires_in => expires_in) do connection = Faraday.new(:url => "#{WordpressRailsConnection.site_url}#{path}") do |faraday| faraday.adapter Faraday.default_adapter faraday.headers['Content-Type'] = 'application/json' end response = connection.get JSON.parse(response.body) end end
posts(page=1, per_page=8)
click to toggle source
# File lib/wordpress-rails-connection.rb, line 20 def self.posts(page=1, per_page=8) path = "/posts?page=#{page.to_s}&number=#{per_page}" WordpressRailsConnection.initialize(path) end
site_url()
click to toggle source
# File lib/wordpress-rails-connection.rb, line 5 def self.site_url return "https://public-api.wordpress.com/rest/v1/sites/#{ENV['BLAKEWEB_WORDPRESS_SITE_ID']}" end