Class: DynamicPDFApi::PdfText

Inherits:
Endpoint
  • Object
show all
Defined in:
lib/ruby_client/PdfText.rb

Overview

Represents the pdf text endpoint.

Instance Attribute Summary collapse

Attributes inherited from Endpoint

#_endpoint_name, #api_key, #base_url

Instance Method Summary collapse

Methods inherited from Endpoint

#init

Constructor Details

#initialize(resource, start_page = 1, page_count = 0) ⇒ PdfText

Initializes a new instance of the PdfText class.

Parameters:

  • resource (PdfResource)

    The image resource of type PdfResource.

  • start_page (int) (defaults to: 1)

    The start page.

  • page_count (int) (defaults to: 0)

    The page count.



16
17
18
19
20
21
22
23
24
# File 'lib/ruby_client/PdfText.rb', line 16

def initialize(resource, start_page = 1, page_count = 0)
  @_endpoint_name = 'pdf-text'
  @start_page = 1
  @page_count = 0
  super()
  @resource = resource
  @start_page = start_page
  @page_count = page_count
end

Instance Attribute Details

#page_countObject

Gets or sets the page count.



34
35
36
# File 'lib/ruby_client/PdfText.rb', line 34

def page_count
  @page_count
end

#start_pageObject

Gets or sets the start page.



29
30
31
# File 'lib/ruby_client/PdfText.rb', line 29

def start_page
  @start_page
end

Instance Method Details

#processObject

Process the pdf resource to get pdf’s text.

Returns:

  • PdfTextResponse Returns collection of PdfTextResponse.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ruby_client/PdfText.rb', line 40

def process
  header = {
    'Authorization': "Bearer #{@api_key}",
    'Content-Length': @resource.data.length.to_s,
    'Expect': '100-continue',
    'Content-Type': 'application/pdf'
  }
  uri = URI.parse("#{@base_url}/v1.0/#{@_endpoint_name}")
  params = { 'startPage' => @start_page, 'pageCount' => @page_count }
  uri.query = URI.encode_www_form(params)

  request = Net::HTTP::Post.new(uri.request_uri, header)
  request.set_form_data(params)
  req_options = {
    use_ssl: uri.scheme == 'https',
    verify_mode: OpenSSL::SSL::VERIFY_NONE

  }

  request.content_type = 'application/pdf'
  request.body = @resource.data

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
  out_data = response.body
  ret_object = PdfTextResponse.new(out_data)
  ret_object.is_successful = false
  ret_object.status_code = response.code
  if ret_object.status_code == '200'
    ret_object.is_successful = true
  else
    out_data_json = JSON.parse(out_data)
    ret_object.error_json = out_data
    ret_object.error_message = if !out_data_json['message'].nil?
                                 out_data_json['message']
                               else
                                 "status_code : #{Net::HTTPResponse::CODE_TO_OBJ[ret_object.status_code]}"
                               end
    ret_object.error_id = out_data_json['id']
  end

  ret_object
end