Class: ApiNotify::ActiveRecord::Synchronizer

Inherits:
Object
  • Object
show all
Defined in:
lib/api_notify/active_record/synchronizer.rb

Instance Method Summary collapse

Constructor Details

#initialize(route_name, id_param) ⇒ Synchronizer

Returns a new instance of Synchronizer.



7
8
9
10
11
12
13
# 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

Instance Method Details

#build_url(param) ⇒ Object



71
72
73
74
# File 'lib/api_notify/active_record/synchronizer.rb', line 71

def build_url param
  _url = param ? "/#{param}" : ""
  "#{@config["base_path"]}/#{@route_name}#{_url}"
end

#headersObject



58
59
60
61
62
63
64
# 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_queryObject



54
55
56
# File 'lib/api_notify/active_record/synchronizer.rb', line 54

def params_query
  @_params.empty? ? "" : "#{@_params.to_query}"
end

#responseObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object



76
77
78
# File 'lib/api_notify/active_record/synchronizer.rb', line 76

def set_params params
  @_params = params
end

#success?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/api_notify/active_record/synchronizer.rb', line 30

def success?
  @_success
end

#url(type) ⇒ Object



66
67
68
69
# 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