Class: Pandadoc::Api::Document

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

Constant Summary collapse

DOCUMENT_STATUS_MAP =
{
  'document.draft': 0,
  'document.sent': 1,
  'document.completed': 2,
  'document.viewed': 5,
  'document.waiting_approval': 6,
  'document.approved': 7,
  'document.rejected': 8,
  'document.waiting_pay': 9,
  'document.paid': 10,
  'document.voided': 11
}.freeze

Instance Method Summary collapse

Instance Method Details

#auth_header(token) ⇒ Object



119
120
121
# File 'lib/pandadoc/api/document.rb', line 119

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

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pandadoc/api/document.rb', line 60

def create(token, params = {})
  validations = {
    name: { required: true, type: String },
    template_uuid: { required: true, type: String },
    recipients: { required: true, type: Array },
    tokens: { required: false, type: Hash },
    fields: { required: false, type: Hash },
    metadata: { required: false, type: Hash },
    pricing_tables: { required: false, type: Array }
  }

  HTTParty.post("#{Pandadoc::Api::API_ROOT}/documents",
                headers: { 'Authorization' => auth_header(token), 'Content-Type': 'application/json' },
                body: validated_params(params, validations))
end

#details(token, document_id) ⇒ Object



81
82
83
84
# File 'lib/pandadoc/api/document.rb', line 81

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

#download(token, document_id) ⇒ Object



114
115
116
117
# File 'lib/pandadoc/api/document.rb', line 114

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


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/pandadoc/api/document.rb', line 98

def link(token, document_id, params = {})
  validations = {
    recipient: { required: true, type: String },
    lifetime: { required: false, type: Integer }
  }

  response = HTTParty.post("#{Pandadoc::Api::API_ROOT}/documents/#{document_id}/session",
                           headers: { 'Authorization' => auth_header(token), 'Content-Type': 'application/json' },
                           body: validated_params(params, validations))

  json_response = JSON.parse(response.body, symbolize_names: true)
  session_id = json_response[:id]

  "https://app.pandadoc.com/s/#{session_id}"
end

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pandadoc/api/document.rb', line 45

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

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

#send_doc(token, document_id, params = {}) ⇒ Object

send is already a Ruby thing, overriding it would be bad



87
88
89
90
91
92
93
94
95
96
# File 'lib/pandadoc/api/document.rb', line 87

def send_doc(token, document_id, params = {})
  validations = {
    message: { required: false, type: String },
    silent: { required: false, type: [TrueClass, FalseClass] }
  }

  HTTParty.post("#{Pandadoc::Api::API_ROOT}/documents/#{document_id}/send",
                headers: { 'Authorization' => auth_header(token), 'Content-Type': 'application/json' },
                body: validated_params(params, validations))
end

#status(token, document_id) ⇒ Object



76
77
78
79
# File 'lib/pandadoc/api/document.rb', line 76

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

#validated_params(params, validations) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/pandadoc/api/document.rb', line 123

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