Skip to content

Instantly share code, notes, and snippets.

@moro
Last active December 17, 2015 18:19
Show Gist options
  • Save moro/5652724 to your computer and use it in GitHub Desktop.
Save moro/5652724 to your computer and use it in GitHub Desktop.
module Censorable
extend ActiveSupport::Concern
class MonitorRequestFailed < RuntimeError
...
end
included do
has_many :monitor_requests, as: :entity, dependent: :destroy
validates :censor_status, includsion: Censorable::STATUSES
afeter_save :request_monitoring
end
module ClassMethods
def censor_passed
where(status: Censorable::PASSED_STATUSES)
end
end
def censor_passed?
censor_status.passed?
end
def feedback_monitor_judge(status)
if PASSED_STATUSES.include?(status)
....
else
....
end
end
private
def request_monitoring
req = monitor_requests.create!(content: monitoring_content)
res = post_monitor_request(req)
unless res.code == '200'
raise Censorable::MonitorRequestFailed.new(res)
end
end
def post_censoring_request(req)
res = Net::HTTP.post_form(config.content_monitor_endpoint, req.attributes)
end
def monitoring_content
case entity
when Question then ...
when Anwer then ...
end
end
end
module Censorable
extend ActiveSupport::Concern
class MonitorRequestFailed < RuntimeError
...
end
included do
has_many :monitor_requests, as: :entity, dependent: :destroy
validates :censor_status, includsion: Censorable::STATUSES
afeter_save { MonitorRequest.submit(self) }
end
module ClassMethods
def censor_passed
where(status: Censorable::PASSED_STATUSES)
end
end
def censor_passed?
censor_status.passed?
end
end
# ---------------
class MonitorRequest < ActiveRecord::Base
class << self
def submit(entity)
req = create!(entity: entity, content: monitoring_content(entity))
res = post_monitor_request(req)
unless res.code == '200'
raise Censorable::MonitorRequestFailed.new(res)
end
end
def import_result(data)
...
end
private
def monitoring_content(entity)
case entity
when Question then ...
when Anwer then ...
end
end
def post_monitor_request(req)
Net::HTTP.post_form(config.content_monitor_endpoint, req.attributes)
end
end
belongs_to :entity, polymorphic: true
def feedback_monitor_judge(result)
status = censor_status_for(reult)
if Censorable::PASSED_STATUSES.include?(status)
else
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment