Class: RubySuperparser::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/superparser.rb

Constant Summary collapse

API_URL =
'https://api.superparser.com/parse'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Returns a new instance of Client.



10
11
12
# File 'lib/ruby/superparser.rb', line 10

def initialize(api_key)
  @api_key = api_key
end

Instance Method Details

#parse(file_path) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby/superparser.rb', line 14

def parse(file_path)
  uri = URI(API_URL)

  request = Net::HTTP::Post::Multipart.new(uri.path,
    'file_name' => UploadIO.new(File.open(file_path), 'application/pdf')
  )
  request['accept'] = 'application/json'
  request['X-API-Key'] = @api_key
  request['Content-Type'] = 'multipart/form-data'

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

  response = http.request(request)

  if response.is_a?(Net::HTTPSuccess)
    puts 'Successfully parsed the document'
    return response.body
  else
    puts 'Failed to parse the document'
    return nil
  end
end