Class: BaseCRM::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/basecrm/http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ HttpClient

Returns a new instance of HttpClient.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/basecrm/http_client.rb', line 9

def initialize(config)
  @config = config

  @headers = {
    'User-Agent' => config.user_agent,
    'Accept' => 'application/json'
  }

  @api_version = "/v2"

  options = {
    # timeout: config.timeout
  }
  options[:ssl] = { verify: false } unless config.verify_ssl

  @client = Faraday.new(config.base_url, options) do |faraday|
    faraday.request :retry, retry_options
    faraday.use BaseCRM::Middlewares::OAuthBearerToken, config.access_token
    faraday.use BaseCRM::Middlewares::RaiseError
    faraday.response :logger, config.logger if config.debug?

    faraday.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/basecrm/http_client.rb', line 7

def headers
  @headers
end

Instance Method Details

#delete(path, params = {}, headers = {}) ⇒ Object



46
47
48
# File 'lib/basecrm/http_client.rb', line 46

def delete(path, params={}, headers={})
  request(:delete, path, params)
end

#get(path, params = {}, headers = {}) ⇒ Object



34
35
36
# File 'lib/basecrm/http_client.rb', line 34

def get(path, params={}, headers={})
  request(:get, path, params, headers)
end

#post(path, body = {}, headers = {}) ⇒ Object



38
39
40
# File 'lib/basecrm/http_client.rb', line 38

def post(path, body={}, headers={})
  request(:post, path, body, headers)
end

#put(path, body = {}, headers = {}) ⇒ Object



42
43
44
# File 'lib/basecrm/http_client.rb', line 42

def put(path, body={}, headers={})
  request(:put, path, body, headers)
end

#request(method, path, data = {}, headers = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/basecrm/http_client.rb', line 50

def request(method, path, data={}, headers={})
  options = {
    headers: @headers.merge(headers.to_h)
  }

  case method.to_s.downcase.to_sym
  when :get, :head, :delete
    options[:query] = encode_params(data) unless data.empty?
  else
    unless data.empty?
      options[:headers]['Content-Type'] = 'application/json'
      options[:body] = Envelope.wrap(data)
    end
  end

  path = "#{@api_version}#{path}"

  res = instance_eval <<-RUBY, __FILE__, __LINE__ + 1
    @client.#{method}(path) do |req|
      req.body = options[:body] if options[:body]
      req.headers.update(options[:headers])
      req.params.update(options[:query]) if options[:query]
    end
  RUBY

  body = extract_body(res)
  @config.logger.debug body if @config.debug? && body && @config.logger
  [res.status, res.headers, body]
rescue Faraday::ConnectionFailed => e
  raise ConnectionError, e.message
end