Class: KHL::Webhook::Client
- Inherits:
-
Object
- Object
- KHL::Webhook::Client
- 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
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
-
#challenge ⇒ Boolean
Do the challenge.
- #compress? ⇒ Boolean
- #encrypt? ⇒ Boolean
-
#initialize(challenge_url, challenge_token, options = {}) ⇒ Client
constructor
A new instance of Client.
- #online? ⇒ Boolean
-
#parse_message(data) ⇒ KHL::Message
Parse message from raw data.
Constructor Details
#initialize(challenge_url, challenge_token, options = {}) ⇒ Client
Returns a new instance of Client.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/khl/webhook/client.rb', line 21 def initialize(challenge_url, challenge_token, = {}) @challenge_url = challenge_url @challenge_token = challenge_token @compress = [:compress] || true @encrypt = [:encrypt] || false @key = [:key] raise ArgumentError, "key is required" if @encrypt && @key.nil? end |
Instance Attribute Details
#key ⇒ Object (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
#challenge ⇒ Boolean
Do the challenge
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
45 46 47 |
# File 'lib/khl/webhook/client.rb', line 45 def compress? @compress end |
#encrypt? ⇒ Boolean
49 50 51 |
# File 'lib/khl/webhook/client.rb', line 49 def encrypt? @encrypt end |
#online? ⇒ 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
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/khl/webhook/client.rb', line 60 def (data) if encrypt? data = decrypt(data) end if compress? data = decompress(data) end Message.parse(data) end |