Class: Engagespot

Inherits:
Object
  • Object
show all
Defined in:
lib/engagespot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url: 'https://api.engagespot.co/v3', api_key: nil, api_secret: nil) ⇒ Engagespot

Returns a new instance of Engagespot.



6
7
8
9
10
# File 'lib/engagespot.rb', line 6

def initialize(base_url: 'https://api.engagespot.co/v3', api_key: nil, api_secret: nil)
  @base_url = base_url
  @api_key = api_key || raise('api_key is required')
  @api_secret = api_secret || raise('api_secret is required')
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



4
5
6
# File 'lib/engagespot.rb', line 4

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



4
5
6
# File 'lib/engagespot.rb', line 4

def api_secret
  @api_secret
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



4
5
6
# File 'lib/engagespot.rb', line 4

def base_url
  @base_url
end

Instance Method Details

#create_or_update_user(identifier, profile: nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/engagespot.rb', line 43

def create_or_update_user(identifier, profile: nil)
  raise 'identifier is required' if identifier.nil?

  headers = {
    'Content-Type' => 'application/json',
    'X-ENGAGESPOT-API-KEY' => @api_key,
    'X-ENGAGESPOT-API-SECRET' => @api_secret
  }

  begin
    url = "#{@base_url}/users/#{identifier}"
    response = HTTParty.put(url, headers: headers, body: profile.to_json)
  rescue Exception => ex
    error = ex.to_s
    return {
      success: false,
      message: error
    }
  else
    response_code = response.code
    if response_code == 200
      return {
        success: true,
        message: response.parsed_response
      }
    else
      return {
        success: false,
        message: response.parsed_response
      }
    end
  end
end

#send(send_request) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/engagespot.rb', line 12

def send(send_request)
  headers = {
    'Content-Type' => 'application/json',
    'X-ENGAGESPOT-API-KEY' => @api_key,
    'X-ENGAGESPOT-API-SECRET' => @api_secret
  }

  begin
    response = HTTParty.post("#{@base_url}/notifications", headers: headers, body: send_request.to_json)
  rescue Exception => ex
    error = ex.to_s
    return {
      success: false,
      message: error
    }
  else
    response_code = response.code
    if response_code == 202
      return {
        success: true,
        message: response.parsed_response
      }
    else
      return {
        success: false,
        message: response.parsed_response
      }
    end
  end
end