Class: Netcore::Event

Inherits:
Object
  • Object
show all
Includes:
Payload
Defined in:
app/my_lib/netcore/event.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Payload

#fav_payload, #reservation_data_payload, #reward_data_payload, #user_loyalty_payload, #user_properties, #user_properties_for_client

Methods included from CountryIdMemoization

#malaysia_id, #singapore_id, #thailand_id

Constructor Details

#initializeEvent

Returns a new instance of Event.



9
10
11
# File 'app/my_lib/netcore/event.rb', line 9

def initialize
  @api_key = Figaro.env.NETCORE_API_KEY
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



7
8
9
# File 'app/my_lib/netcore/event.rb', line 7

def api_key
  @api_key
end

Class Method Details

.count_restaurant_viewed(*_args) ⇒ Object



14
15
16
17
# File 'app/my_lib/netcore/event.rb', line 14

def count_restaurant_viewed(*_args)
  # TODO: implement this method
  0
end

Instance Method Details

#api_clientObject



109
110
111
112
113
114
115
116
117
118
# File 'app/my_lib/netcore/event.rb', line 109

def api_client
  @api_client ||= DefaultFaradayClient.create_faraday_connection(
    'https://api2.netcoresmartech.com/v1/activity/', ssl: { min_version: :TLS1_2 }
  ) do |conn|
    conn.headers['Content-Type'] = 'application/json; charset=utf-8'
    conn.headers['Authorization'] = "Bearer #{api_key}"
    conn.headers['User-Agent'] = 'HungryHub-PRIVATE'
    conn.adapter Faraday.default_adapter
  end
end

#api_client_v2Object



120
121
122
123
124
125
126
127
128
# File 'app/my_lib/netcore/event.rb', line 120

def api_client_v2
  @api_client_v2 ||= DefaultFaradayClient.create_faraday_connection(
    'https://api.netcoresmartech.com', ssl: { min_version: :TLS1_2 }
  ) do |conn|
    conn.headers['Content-Type'] = 'application/json; charset=utf-8'
    conn.headers['User-Agent'] = 'HungryHub-PRIVATE'
    conn.adapter Faraday.default_adapter
  end
end

#api_client_v3Object



130
131
132
133
134
135
136
137
138
139
# File 'app/my_lib/netcore/event.rb', line 130

def api_client_v3
  @api_client_v3 ||= DefaultFaradayClient.create_faraday_connection(
    'http://api.netcoresmartech.com/v3/', ssl: { min_version: :TLS1_2 }
  ) do |conn|
    conn.headers['Content-Type'] = 'application/json; charset=utf-8'
    conn.headers['api-key'] = api_key
    conn.headers['User-Agent'] = 'HungryHub-PRIVATE'
    conn.adapter Faraday.default_adapter
  end
end

#create_report(report_attrs) ⇒ Object



100
101
102
103
104
105
106
107
# File 'app/my_lib/netcore/event.rb', line 100

def create_report(report_attrs)
  count = report_attrs.delete(:data)
  return if report_attrs.blank?

  report = Report.find_or_initialize_by(report_attrs)
  report.data = count
  report.save!
end

#report_if_error(response) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/my_lib/netcore/event.rb', line 141

def report_if_error(response)
  json_body = begin
    JSON.parse(response.body)
  rescue JSON::ParserError
    # might be a html response, Gateway Timeout, etc
    APMErrorHandler.report('failed parsing response from netcore', response: response.body)
    # raise error to retry the job
    raise 'failed parsing response from netcore'
  end
  status = json_body['status'].to_s.downcase
  result = json_body['result'].to_s.downcase

  if ['success', '200'].include?(status) || result == 'success'
    nil
  else
    APMErrorHandler.report('failed sending event to netcore', json_body: json_body)
    # raise error to retry the job
    raise 'failed sending event to netcore'
  end
end

#sync_user_data(user) ⇒ void

This method returns an undefined value.

Synchronizes user data with Netcore by sending a contact add event.

This method prepares a payload containing user properties and sends it to Netcore using the v2 API. The payload is structured as a contact add activity. Ref: developer.netcorecloud.com/docs/update-contact-1

If the user is blank, the method returns early and does nothing.

Payload structure:

{
  type: 'contact',
  activity: 'add',
  data: { ...user properties... }
}

The actual user properties are extracted using the `user_properties(user)` helper. The payload is sent via `upload_v2(:post, payload)`.

Any errors encountered during the upload process are handled and reported by the `report_if_error` method, which uses APMErrorHandler for error reporting.

Parameters:

  • user (User)

    The user object whose data should be synchronized.



43
44
45
46
47
48
49
50
51
52
# File 'app/my_lib/netcore/event.rb', line 43

def sync_user_data(user)
  return if user.blank?

  payload = {
    type: 'contact',
    activity: 'add',
    data: user_properties(user),
  }
  upload_v2(:post, payload)
end

#upload(payload) ⇒ Object

Add Activity API This API allows the user to send event to the Netcore CE panel from external sources i.e. offline activity data

ref: developer.netcorecloud.com/docs/add-activity-api



58
59
60
61
62
63
# File 'app/my_lib/netcore/event.rb', line 58

def upload(payload)
  Array.wrap(payload).in_groups_of(1000, false) do |group|
    response = api_client.post('upload', group.to_json)
    report_if_error(response)
  end
end

#upload_v2(method, payload) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/my_lib/netcore/event.rb', line 83

def upload_v2(method, payload)
  api_key_payload = { apikey: api_key }

  case method.downcase.to_sym
  when :post
    data = payload.delete(:data)
    response = api_client_v2.post("apiv2?#{api_key_payload.merge(payload).to_param}") do |req|
      req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
      req.body = URI.encode_www_form(data: data.to_json)
    end
    report_if_error(response)
  when :get
    response = api_client_v2.get("apiv2?#{api_key_payload.merge(payload).to_param}")
    report_if_error(response)
  end
end

#upload_v3(payload) ⇒ Object

Batch API - Add Multiple Contacts through API

General Rules for API Use

  1. The maximum number of attributes per contact is limited to 20.

  2. All keys in JSON should be in lowercase (to split longer names, use “_”)

  3. Both notify email and callback URLs are mandatory to be specified in request api call. Ensure that your callback endpoint is in POST method.

  4. All attribute fields should be in upper case.

  5. You can send a maximum size of 1000 contacts in one API call

  6. Attribute values should be either in string or integer format. If you wish to pass blank or null, then it should be enclosed within double quotes.

ref: developer.netcorecloud.com/docs/add-bulk-contact-through-api#general-rules-for-api-use



78
79
80
81
# File 'app/my_lib/netcore/event.rb', line 78

def upload_v3(payload)
  response = api_client_v3.post('contact/batchadd', payload.to_json)
  report_if_error(response)
end