Class: SendLayer::Client

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

Constant Summary collapse

BASE_URL =
'https://console.sendlayer.com/api/v1/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, options = {}) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
# File 'lib/sendlayer/client.rb', line 12

def initialize(api_key, options = {})
  @api_key = api_key
  @base_url = options[:base_url] || BASE_URL
  @timeout = options[:timeout] || 30
  @attachment_url_timeout = options[:attachment_url_timeout] || 30000
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



10
11
12
# File 'lib/sendlayer/client.rb', line 10

def api_key
  @api_key
end

#attachment_url_timeoutObject (readonly)

Returns the value of attribute attachment_url_timeout.



10
11
12
# File 'lib/sendlayer/client.rb', line 10

def attachment_url_timeout
  @attachment_url_timeout
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



10
11
12
# File 'lib/sendlayer/client.rb', line 10

def base_url
  @base_url
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



10
11
12
# File 'lib/sendlayer/client.rb', line 10

def timeout
  @timeout
end

Instance Method Details

#make_request(method, endpoint, data = nil, params = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sendlayer/client.rb', line 19

def make_request(method, endpoint, data = nil, params = {})
  uri = URI("#{@base_url}#{endpoint}")
  
  # Add query parameters
  unless params.empty?
    uri.query = URI.encode_www_form(params)
  end

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.read_timeout = @timeout

  case method.upcase
  when 'GET'
    request = Net::HTTP::Get.new(uri)
  when 'POST'
    request = Net::HTTP::Post.new(uri)
    request.body = data.to_json if data
    request['Content-Type'] = 'application/json'
  when 'DELETE'
    request = Net::HTTP::Delete.new(uri)
  else
    raise SendLayerError.new("Unsupported HTTP method: #{method}")
  end

  request['Authorization'] = "Bearer #{@api_key}"
  request['User-Agent'] = "SendLayer-Ruby/#{::SendLayer::VERSION}"

  begin
    response = http.request(request)
    handle_response(response)
  rescue Timeout::Error
    raise SendLayerError.new("Request timeout after #{@timeout} seconds")
  rescue => e
    raise SendLayerError.new("Network error: #{e.message}")
  end
end