Class: Hephaestus::YettoService

Inherits:
Object
  • Object
show all
Defined in:
app/services/hephaestus/yetto_service.rb

Constant Summary collapse

YETTO_API_VERSION_TLD =

Version is set by the consuming plug

"#{Hephaestus::PROTOCOL}#{Hephaestus::YETTO_API_URL}/#{Rails.configuration.yetto_api_version}"

Class Method Summary collapse

Class Method Details

.add_message_to_conversation(plug_installation_id, conversation_id, params) ⇒ Object



82
83
84
85
86
# File 'app/services/hephaestus/yetto_service.rb', line 82

def add_message_to_conversation(plug_installation_id, conversation_id, params)
  token = perform_token_exchange(plug_installation_id)

  yetto_client.with_headers({ "Authorization" => "Bearer #{token}" }).post("#{YETTO_API_VERSION_TLD}/conversations/#{conversation_id}/messages", json: params)
end

.create_conversation(plug_installation_id, inbox_id, params) ⇒ Object



70
71
72
73
74
# File 'app/services/hephaestus/yetto_service.rb', line 70

def create_conversation(plug_installation_id, inbox_id, params)
  token = perform_token_exchange(plug_installation_id)

  yetto_client.with_headers({ "Authorization" => "Bearer #{token}" }).post("#{YETTO_API_VERSION_TLD}/inboxes/#{inbox_id}/conversations", json: params)
end

.create_message_reply(plug_installation_id, message_id, params) ⇒ Object



76
77
78
79
80
# File 'app/services/hephaestus/yetto_service.rb', line 76

def create_message_reply(plug_installation_id, message_id, params)
  token = perform_token_exchange(plug_installation_id)

  yetto_client.with_headers("Authorization" => "Bearer #{token}").post("#{YETTO_API_VERSION_TLD}/messages/#{message_id}/replies", json: params)
end

.encoded_jwtObject



27
28
29
# File 'app/services/hephaestus/yetto_service.rb', line 27

def encoded_jwt
  Httpsensible::JWT.encode_jwt(Hephaestus::YETTO_PLUG_PEM, Hephaestus::YETTO_PLUG_ID)
end

.get_messages_in_conversation(plug_installation_id, conversation_id, filter: {}) ⇒ Object



58
59
60
61
62
# File 'app/services/hephaestus/yetto_service.rb', line 58

def get_messages_in_conversation(plug_installation_id, conversation_id, filter: {})
  token = perform_token_exchange(plug_installation_id)

  yetto_client.with_headers("Authorization" => "Bearer #{token}").get("#{YETTO_API_VERSION_TLD}/conversations/#{conversation_id}/messages#{to_filter_query(filter)}")
end

.get_messages_in_inbox(plug_installation_id, inbox_id, filter: {}) ⇒ Object



52
53
54
55
56
# File 'app/services/hephaestus/yetto_service.rb', line 52

def get_messages_in_inbox(plug_installation_id, inbox_id, filter: {})
  token = perform_token_exchange(plug_installation_id)

  yetto_client.with_headers({ "Authorization" => "Bearer #{token}" }).get("#{YETTO_API_VERSION_TLD}/inboxes/#{inbox_id}/messages#{to_filter_query(filter)}")
end

.get_plug_installation(plug_installation_id) ⇒ Object



37
38
39
40
# File 'app/services/hephaestus/yetto_service.rb', line 37

def get_plug_installation(plug_installation_id)
  token = perform_token_exchange(plug_installation_id)
  yetto_client.with_headers({ "Authorization" => "Bearer #{token}" }).get("#{YETTO_API_VERSION_TLD}/installations/#{plug_installation_id}")
end

.get_plug_installations(filter: {}) ⇒ Object



88
89
90
# File 'app/services/hephaestus/yetto_service.rb', line 88

def get_plug_installations(filter: {})
  yetto_client.with_headers({ "Authorization" => "Bearer #{encoded_jwt}" }).get("#{YETTO_API_VERSION_TLD}/plug/installations#{to_filter_query(filter)}")
end

.perform_token_exchange(plug_installation_id) ⇒ Object



31
32
33
34
35
# File 'app/services/hephaestus/yetto_service.rb', line 31

def perform_token_exchange(plug_installation_id)
  response = yetto_client.with_headers({ "Authorization" => "Bearer #{encoded_jwt}" }).post("#{YETTO_API_VERSION_TLD}/plug/installations/#{plug_installation_id}/access_tokens")
  body = response.parsed_json_body
  body["token"]
end

.update_message(plug_installation_id, message_id, params) ⇒ Object



64
65
66
67
68
# File 'app/services/hephaestus/yetto_service.rb', line 64

def update_message(plug_installation_id, message_id, params)
  token = perform_token_exchange(plug_installation_id)

  yetto_client.with_headers("Authorization" => "Bearer #{token}").patch("#{YETTO_API_VERSION_TLD}/messages/#{message_id}", json: params)
end

.update_plug_installation(plug_installation_id, params) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'app/services/hephaestus/yetto_service.rb', line 42

def update_plug_installation(plug_installation_id, params)
  plug_installation = {}

  plug_installation[:settings] = params.fetch(:settings, {})
  plug_installation[:credentials] = params.fetch(:credentials, {})

  token = perform_token_exchange(plug_installation_id)
  yetto_client.with_headers({ "Authorization" => "Bearer #{token}" }).patch("#{YETTO_API_VERSION_TLD}/installations/#{plug_installation_id}", json: plug_installation)
end

.yetto_clientObject



23
24
25
# File 'app/services/hephaestus/yetto_service.rb', line 23

def yetto_client
  @yetto_client ||= Httpsensible::Client.new(user_agent: "#{Rails.application.class.module_parent.name}/#{Hephaestus::Engine::GIT_SHA}")
end