Class: Pandadoc::Api::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/pandadoc/api/template.rb

Instance Method Summary collapse

Instance Method Details

#auth_header(token) ⇒ Object



24
25
26
# File 'lib/pandadoc/api/template.rb', line 24

def auth_header(token)
  "Bearer #{token}"
end

#details(token, template_id) ⇒ Object



19
20
21
22
# File 'lib/pandadoc/api/template.rb', line 19

def details(token, template_id)
  HTTParty.get("#{Pandadoc::Api::API_ROOT}/templates/#{template_id}/details",
               headers: { 'Authorization' => auth_header(token) })
end

#list(token, params = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/pandadoc/api/template.rb', line 6

def list(token, params = {})
  validations = {
    q: { required: false, type: String },
    tag: { required: false, type: Integer },
    count: { required: false, type: Integer },
    page: { required: false, type: Integer }
  }

  HTTParty.get("#{Pandadoc::Api::API_ROOT}/templates",
               headers: { 'Authorization' => auth_header(token) },
               query: validated_params(params, validations))
end

#validated_params(params, validations) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pandadoc/api/template.rb', line 28

def validated_params(params, validations)
  valid_keys = validations.keys
  valid_params = params.keep_if { |key| valid_keys.include? key }

  validations.each_pair do |key, validators|
    if validators[:required] == true && valid_params[key].nil?
      raise RequiredParameterError.new('Missing required parameter', key)
    end

    validators_type_array = validators[:type].is_a?(Array) ? validators[:type] : [validators[:type]]
    if valid_params[key] && !validators_type_array.include?(valid_params[key].class)
      raise ParameterTypeError.new("Invalid parameter type, received #{valid_params[key].class} requested #{validators[:type]}", valid_params[:key].class, validators[:type])
    end
  end

  valid_params
end