Class: Urlbox::Client

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

Constant Summary collapse

BASE_API_URL =
'https://api.urlbox.io/v1/'.freeze
POST_END_POINT =
'render'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, api_secret: nil, api_host_name: nil) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
16
17
# File 'lib/urlbox/client.rb', line 9

def initialize(api_key: nil, api_secret: nil, api_host_name: nil)
  if api_key.nil? && ENV['URLBOX_API_KEY'].nil?
    raise Urlbox::Error, "Missing api_key or ENV['URLBOX_API_KEY'] not set"
  end

  @api_key = api_key || ENV['URLBOX_API_KEY']
  @api_secret = api_secret || ENV['URLBOX_API_SECRET']
  @base_api_url = init_base_api_url(api_host_name)
end

Instance Method Details

#delete(options) ⇒ Object



23
24
25
26
# File 'lib/urlbox/client.rb', line 23

def delete(options)
  processed_options, format = process_options(options)
  HTTP.delete("#{@base_api_url}#{@api_key}/#{format}?#{processed_options}")
end

#generate_url(options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/urlbox/client.rb', line 48

def generate_url(options)
  processed_options, format = process_options(options)

  if @api_secret
    "#{@base_api_url}" \
    "#{@api_key}/#{token(processed_options)}/#{format}" \
    "?#{processed_options}"
  else
    "#{@base_api_url}" \
    "#{@api_key}/#{format}" \
    "?#{processed_options}"
  end
end

#get(options) ⇒ Object



19
20
21
# File 'lib/urlbox/client.rb', line 19

def get(options)
  HTTP.timeout(100).follow.get(generate_url(options))
end

#head(options) ⇒ Object



28
29
30
31
32
33
# File 'lib/urlbox/client.rb', line 28

def head(options)
  processed_options, format = process_options(options)
  HTTP.timeout(100)
      .follow
      .head("#{@base_api_url}#{@api_key}/#{format}?#{processed_options}")
end

#post(options) ⇒ Object

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/urlbox/client.rb', line 35

def post(options)
  raise Urlbox::Error, Urlbox::Error.missing_api_secret_error_message if @api_secret.nil?

  unless options.key?(:webhook_url)
    warn('webhook_url not supplied, you will need to poll the statusUrl in order to get your result')
  end

  processed_options, _format = process_options_post_request(options)
  HTTP.timeout(5)
      .headers('Content-Type': 'application/json', 'Authorization': "Bearer #{@api_secret}")
      .post("#{@base_api_url}#{POST_END_POINT}", json: processed_options)
end