Module: Horoshop::Connection

Included in:
Base
Defined in:
lib/horoshop/connection.rb

Overview

Module for check connection

Constant Summary collapse

ERROR =
{ 'status' => 'HTTP_ERROR', 'response' => { 'message' => 'UNKNOWN SERVER ERROR' } }.freeze

Instance Method Summary collapse

Instance Method Details

#connection(horoshop) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/horoshop/connection.rb', line 16

def connection(horoshop)
  @connection ||= Faraday.new(url: horoshop.url) do |faraday|
    faraday.request :json
    faraday.response :raise_error
    faraday.response :json
    faraday.adapter Faraday.default_adapter
  end
end

#mixin_token!(horoshop, body) ⇒ Object



25
26
27
28
# File 'lib/horoshop/connection.rb', line 25

def mixin_token!(horoshop, body)
  horoshop.refresh_token! unless horoshop.token_valid?
  body[:token] = horoshop.token
end

#post(horoshop:, url:, body:, add_token: false) ⇒ Object



9
10
11
12
13
14
# File 'lib/horoshop/connection.rb', line 9

def post(horoshop:, url:, body:, add_token: false)
  mixin_token!(horoshop, body) if add_token
  connection(horoshop).post(url, body).body
rescue Faraday::Error
  ERROR
end