Class: Cased::HTTP::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cased/http/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(url:, api_key:, raise_on_errors: false) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cased/http/client.rb', line 9

def initialize(url:, api_key:, raise_on_errors: false)
  @raise_on_errors = raise_on_errors
  @client = ::Faraday.new(url: url) do |conn|
    conn.headers[:user_agent] = "cased-ruby/v#{Cased::VERSION}"
    conn.headers[:content_type] = 'application/json'
    conn.headers[:accept] = 'application/json'
    conn.headers[:authorization] = "Bearer #{api_key}"

    conn.request :json
    conn.response :json, content_type: /\bjson$/

    conn.options.timeout = Cased.config.http_read_timeout
    conn.options.open_timeout = Cased.config.http_open_timeout
  end
end

Instance Method Details

#delete(url = nil, params = nil, headers = nil) ⇒ Object



55
56
57
58
59
60
# File 'lib/cased/http/client.rb', line 55

def delete(url = nil, params = nil, headers = nil)
  request(:delete, url, nil, headers) do |req|
    req.params.update(params) if params
    yield req if block_given?
  end
end

#get(url = nil, params = nil, headers = nil) ⇒ Object

Requests without bodies



41
42
43
44
45
46
# File 'lib/cased/http/client.rb', line 41

def get(url = nil, params = nil, headers = nil)
  request(:get, url, nil, headers) do |req|
    req.params.update(params) if params
    yield req if block_given?
  end
end

#head(url = nil, params = nil, headers = nil) ⇒ Object



48
49
50
51
52
53
# File 'lib/cased/http/client.rb', line 48

def head(url = nil, params = nil, headers = nil)
  request(:head, url, nil, headers) do |req|
    req.params.update(params) if params
    yield req if block_given?
  end
end

#patch(url = nil, body = nil, headers = nil, &block) ⇒ Object



35
36
37
# File 'lib/cased/http/client.rb', line 35

def patch(url = nil, body = nil, headers = nil, &block)
  request(:patch, url, body, headers, &block)
end

#post(url = nil, body = nil, headers = nil, &block) ⇒ Object



31
32
33
# File 'lib/cased/http/client.rb', line 31

def post(url = nil, body = nil, headers = nil, &block)
  request(:post, url, body, headers, &block)
end

#put(url = nil, body = nil, headers = nil, &block) ⇒ Object

Requests with bodies



27
28
29
# File 'lib/cased/http/client.rb', line 27

def put(url = nil, body = nil, headers = nil, &block)
  request(:put, url, body, headers, &block)
end

#trace(url = nil, params = nil, headers = nil) ⇒ Object



62
63
64
65
66
67
# File 'lib/cased/http/client.rb', line 62

def trace(url = nil, params = nil, headers = nil)
  request(:trace, url, nil, headers) do |req|
    req.params.update(params) if params
    yield req if block_given?
  end
end