Class: Chatbase::HttpService

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent_key: nil) ⇒ HttpService

Returns a new instance of HttpService.



5
6
7
8
# File 'lib/chatbase/http_service.rb', line 5

def initialize(agent_key: nil)
  @agent_key = agent_key || Chatbase.agent_key
  @agent_name = agent_name || Chatbase.agent_name
end

Instance Attribute Details

#agent_keyObject

Returns the value of attribute agent_key.



3
4
5
# File 'lib/chatbase/http_service.rb', line 3

def agent_key
  @agent_key
end

#agent_nameObject

Returns the value of attribute agent_name.



3
4
5
# File 'lib/chatbase/http_service.rb', line 3

def agent_name
  @agent_name
end

Instance Method Details

#connectionObject



10
11
12
13
14
15
16
17
# File 'lib/chatbase/http_service.rb', line 10

def connection
  @connection ||= begin
    Faraday.new(:url => 'https://chatbase.com/api/message') do |faraday|
      faraday.response :json, :content_type => /\bjson$/
      faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
    end
  end
end

#request_post(data) ⇒ Object



19
20
21
22
23
24
# File 'lib/chatbase/http_service.rb', line 19

def request_post(data)
  connection.post do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = {'api_key': agent_key, 'agent_name': agent_name}.merge(data).to_json
  end
end