Skip to content

Instantly share code, notes, and snippets.

@amitpatelx
Created June 11, 2017 11:08
Show Gist options
  • Save amitpatelx/f1307afdbc7ae03d1f42106e41944e83 to your computer and use it in GitHub Desktop.
Save amitpatelx/f1307afdbc7ae03d1f42106e41944e83 to your computer and use it in GitHub Desktop.
module CMS
class Base
HTTP_ERRORS = [Faraday::Error::ConnectionFailed, Faraday::Error::TimeoutError]
attr_reader :cms
def initialize(cms)
@cms = cms
end
def credentials
@credentials ||= cms.blog_connection_options
end
def handle_network_errors!(e, source)
#client.update_column(:connected, false) if client.connected?
error = e.message
Rails.logger.error "[ERROR] Unable to connect with WordPress(#{error}) due to network error in #{source}. Connection Options = #{cms.blog_connection_options.except(:password)}"
raise StandardError.new(I18n.t('errors.cms.network.connection_failed'))
end
end
end
module CMS
class GetPost < CMS::Base
def call(post_id)
begin
Magpress::Post.new(credentials).find(post_id)
rescue *HTTP_ERRORS => e
handle_network_errors!(e, self)
rescue => e
Rails.logger.error "[ERROR] Problem while get post for #{credentials} (#{e.message})"
raise
end
end
private
def response(response)
Wordpress::ResponseProcessors::GetPost.new(response).generic_response
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment