Class: SearchClickTracking::Service

Inherits:
Common::Client::Base show all
Includes:
Common::Client::Concerns::Monitoring
Defined in:
lib/search_click_tracking/service.rb

Overview

This class builds a wrapper around Search.gov web click tracking API.

Constant Summary collapse

STATSD_KEY_PREFIX =
'api.search_click_tracking'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common::Client::Concerns::Monitoring

#increment, #increment_failure, #increment_total, #with_monitoring

Methods inherited from Common::Client::Base

#config, configuration, #connection, #delete, #get, #perform, #post, #put, #raise_backend_exception, #raise_not_authenticated, #request, #sanitize_headers!, #service_name

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger, #set_sentry_metadata

Constructor Details

#initialize(url, query, position, user_agent, module_code = 'I14Y') ⇒ Service

Returns a new instance of Service.



23
24
25
26
27
28
29
# File 'lib/search_click_tracking/service.rb', line 23

def initialize(url, query, position, user_agent, module_code = 'I14Y')
  @url = url
  @query = query
  @position = position
  @user_agent = user_agent
  @module_code = module_code
end

Instance Attribute Details

#module_codeObject (readonly)

Returns the value of attribute module_code.



21
22
23
# File 'lib/search_click_tracking/service.rb', line 21

def module_code
  @module_code
end

#positionObject (readonly)

Returns the value of attribute position.



21
22
23
# File 'lib/search_click_tracking/service.rb', line 21

def position
  @position
end

#queryObject (readonly)

Returns the value of attribute query.



21
22
23
# File 'lib/search_click_tracking/service.rb', line 21

def query
  @query
end

#urlObject (readonly)

Returns the value of attribute url.



21
22
23
# File 'lib/search_click_tracking/service.rb', line 21

def url
  @url
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



21
22
23
# File 'lib/search_click_tracking/service.rb', line 21

def user_agent
  @user_agent
end

Instance Method Details

#access_keyObject (private)



73
74
75
# File 'lib/search_click_tracking/service.rb', line 73

def access_key
  Settings.search_click_tracking.access_key
end

#affiliateObject (private)



69
70
71
# File 'lib/search_click_tracking/service.rb', line 69

def affiliate
  Settings.search_click_tracking.affiliate
end

#query_paramsObject (private)

Required params [affiliate, access_key, module_code, url, query, position, user_agent]



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/search_click_tracking/service.rb', line 55

def query_params
  URI.encode_www_form(
    {
      affiliate:,
      access_key:,
      module_code:,
      url:,
      query:,
      position:,
      user_agent:
    }
  )
end

#track_clickObject

POSTs click tracking query param data to search.gov



33
34
35
36
37
38
39
# File 'lib/search_click_tracking/service.rb', line 33

def track_click
  with_monitoring do
    Faraday.post(url_with_params, '')
  end
rescue => e
  e
end

#track_click_urlObject (private)



47
48
49
# File 'lib/search_click_tracking/service.rb', line 47

def track_click_url
  config.base_path
end

#url_with_paramsObject (private)



43
44
45
# File 'lib/search_click_tracking/service.rb', line 43

def url_with_params
  "#{track_click_url}?#{query_params}"
end