class ApiNotify::ActiveRecord::Synchronizer

Public Class Methods

new(route_name, id_param) click to toggle source
# File lib/api_notify/active_record/synchronizer.rb, line 7
def initialize route_name, id_param
  @config = load_config_yaml
  @_params = {}
  @route_name = route_name
  @_success = false
  @id_param = id_param
end

Public Instance Methods

build_url(param) click to toggle source
# File lib/api_notify/active_record/synchronizer.rb, line 71
def build_url param
  _url = param ? "/#{param}" : ""
  "#{@config["base_path"]}/#{@route_name}#{_url}"
end
headers() click to toggle source
# File lib/api_notify/active_record/synchronizer.rb, line 58
def headers
  headers = {
    "Content-type" => "application/x-www-form-urlencoded",
    "Content-Length" => params_query.length.to_s,
    "Api-Key" => @config["api_key"].to_s
  }
end
params_query() click to toggle source
# File lib/api_notify/active_record/synchronizer.rb, line 54
def params_query
  @_params.empty? ? "" : "#{@_params.to_query}"
end
response() click to toggle source
# File lib/api_notify/active_record/synchronizer.rb, line 15
def response
  if @_response.body
    _response = { status: @_response.code }
    begin
      _response[:body] = JSON.parse(@_response.body)
    rescue
      _response[:body] = @_response.body
    end
  else
    _response = { status: "error" }
    _response[:body] = @_response
  end
  _response
end
send_request(type = 'GET', url_param = false) click to toggle source
# File lib/api_notify/active_record/synchronizer.rb, line 34
def send_request(type = 'GET', url_param = false)
  begin
    ApiNotify::LOGGER.info "Request Started"
    http = Net::HTTP.new(@config["domain"], @config["port"])
    if @config["port"].to_i == 443
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
    _url = url_param ? build_url(url_param) : url(type)
    ApiNotify::LOGGER.info "Request url: #{_url}"
    @_response = http.send_request(type, _url, params_query, headers)
    @_success = true
    ApiNotify::LOGGER.info "#{@_response.code}: #{ @_response.body.truncate(200, separator: "\n")}"
  rescue Exception => e
    @_response = {error: e}
    ApiNotify::LOGGER.error @_response[:error]
  end
  @_response
end
set_params(params) click to toggle source
# File lib/api_notify/active_record/synchronizer.rb, line 76
def set_params params
  @_params = params
end
success?() click to toggle source
# File lib/api_notify/active_record/synchronizer.rb, line 30
def success?
  @_success
end
url(type) click to toggle source
# File lib/api_notify/active_record/synchronizer.rb, line 66
def url type
  id = @_params[@id_param] && type!= "POST" ? "#{@_params[@id_param]}" : nil
  build_url id
end

Private Instance Methods

load_config_yaml() click to toggle source
# File lib/api_notify/active_record/synchronizer.rb, line 81
def load_config_yaml
  config_yaml = "#{Rails.root.to_s}/config/api_notify.yml"
  YAML.load_file(config_yaml)[Rails.env] if File.exists?(config_yaml)
end