This repository has been archived by the owner on Dec 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
external_service.rb
46 lines (35 loc) · 1.63 KB
/
external_service.rb
1
2
3
4
5
6
7
8
9
10
11
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
42
43
44
45
46
module ExternalService
module Api
EXTERNAL_SERVICE_API_URL = "YOUR_EXTERNAL_SERVICE_API_URL"
EXTERNAL_SERVICE_API_TOKEN_URL = "#{EXTERNAL_SERVICE_API_URL}/oauth/token"
def self.perform_action_hook(node_id, session)
# query external service provide
response = query_external_service_api("#{EXTERNAL_SERVICE_API_URL}/v1/your/resources/of/interest")
# send response to user
Facebook::Api::send_message(session, response)
session.destroy # delete user session
end
private
def self.query_external_service_api(url, params={})
token = get_oauth2_token
r = token.get(url)
return JSON.parse(r.body) if r.status == 200
end
def self.post_to_external_service_api(url, params={})
token = get_oauth2_token
r = token.post(url, params: params)
return JSON.parse(r.body) if r.status == 201
end
def self.patch_to_external_service_api(url, params={})
token = get_oauth2_token
r = token.patch(url, params: params)
return true if r.status == 204
end
# OAuth2: Resource Owner Password Credentials Grant
def self.get_oauth2_token
client = OAuth2::Client.new(Rails.application.secrets.external_service_api_client_id, Rails.application.secrets.external_service_api_client_secret, token_url: EXTERNAL_SERVICE_API_TOKEN_URL)
encrypted_credentials = "Basic " + Base64.encode64("#{Rails.application.secrets.external_service_api_client_id}:#{Rails.application.secrets.external_service_api_client_secret}")
return client.client_credentials.get_token(headers: {'Authorization': encrypted_credentials })
end
end
end