Class: KHL::Webhook::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/khl/webhook/client.rb

Overview

Client for the KHL Webhook API client = KHL::Webhook::Client.new(“foo.bar/callback”, “your_challenge_token”) client.online? # => false client.challenge # => true client.online? # => true data = client.parse_message(data_from_webhook)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(challenge_url, challenge_token, options = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • challenge_url (String)

    The source url from the request

  • challenge_token (String)

    The challenge param from the request

  • options (Hash) (defaults to: {})

    Additional options

Options Hash (options):

  • :compress (Boolean) — default: true

    Enable/disable compression

  • :encrypt (encrypt) — default: false

    Enable/disable encryption

  • :key (String) — default: nil

    Encryption key

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
# File 'lib/khl/webhook/client.rb', line 21

def initialize(challenge_url, challenge_token, options = {})
  @challenge_url = challenge_url
  @challenge_token = challenge_token

  @compress = options[:compress] || true
  @encrypt = options[:encrypt] || false
  @key = options[:key]

  raise ArgumentError, "key is required" if @encrypt && @key.nil?
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



13
14
15
# File 'lib/khl/webhook/client.rb', line 13

def key
  @key
end

Instance Method Details

#challengeBoolean

Do the challenge

Returns:

  • (Boolean)

    The challenge success state



34
35
36
37
38
39
40
41
42
43
# File 'lib/khl/webhook/client.rb', line 34

def challenge
  uri = URI.parse(@challenge_url)
  res = Net::HTTP.post_form(uri, challenge: @challenge_token)
  if res.code == "200"
    @online = true
    return true
  end

  false
end

#compress?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/khl/webhook/client.rb', line 45

def compress?
  @compress
end

#encrypt?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/khl/webhook/client.rb', line 49

def encrypt?
  @encrypt
end

#online?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/khl/webhook/client.rb', line 53

def online?
  @online || false
end

#parse_message(data) ⇒ KHL::Message

Parse message from raw data

Parameters:

  • data (String)

    The raw data from the webhook

Returns:



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/khl/webhook/client.rb', line 60

def parse_message(data)
  if encrypt?
    data = decrypt(data)
  end

  if compress?
    data = decompress(data)
  end

  Message.parse(data)
end