Class: IdpSdkRuby::ExtractionTaskClient

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

Instance Method Summary collapse

Constructor Details

#initialize(token: nil, region: nil, isOauth: nil) ⇒ ExtractionTaskClient

Returns a new instance of ExtractionTaskClient.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/idp_ruby.rb', line 116

def initialize(token:nil,region:nil, isOauth:nil)
    @token=token
    @region=region
    @isOauth=isOauth

    if region === 'test'
        region = ''
    else
        region = '-'+region
    end
    @url_post = "https://idp"+region + \
        ".6estates.com/customer/extraction/fields/async"
    @url_get = "https://idp"+region + \
        ".6estates.com/customer/extraction/field/async/result/"
end

Instance Method Details

#create(file: nil, file_type: nil, lang: nil, customer: nil, customer_param: nil, callback: nil, auto_callback: nil, callback_mode: nil, hitl: nil) ⇒ Object

Raises:



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/idp_ruby.rb', line 132

def create(file:nil, file_type:nil, lang:nil,
    customer:nil, customer_param:nil, callback:nil,
    auto_callback:nil, callback_mode:nil, hitl:nil)
    if FileType.new().FileType.count {|x|x==file_type} == 0
        raise IDPException.new("Invalid file type")
    end
    if file.nil?
        raise IDPException.new("File is required")
    end
    if @isOauth
        headers = {"Authorization"=> @token}
    else
        headers = {"X-ACCESS-TOKEN"=> @token}
    end
    # files = {"file": file}
    data = {'fileType'=> file_type, 'lang'=> lang, 'customer'=> customer,
            'customerParam'=> customer_param, 'callback'=> callback,
            'autoCallback'=>auto_callback, 'callbackMode'=> callback_mode,
            'hitl'=> hitl, "file"=>file}
    data=data.delete_if{|key,value|value.nil?}

    r = JSON.parse(RestClient.post(@url_post, data, headers))
    if r['status']==200
        return Task.new(r)
    end
    raise IDPException.new(r['message'])
end

#result(task_id = nil) ⇒ Object

Raises:



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/idp_ruby.rb', line 160

def result(task_id=nil)
    if task_id.nil?
        raise IDPConfigurationException.new('Task ID is required')
    end
    if @isOauth
        headers = {"Authorization"=> @token}
    else
        headers = {"X-ACCESS-TOKEN"=> @token}
    end
    r = JSON.parse(RestClient.get(@url_get+task_id.to_s, headers))

    if r['status']==200
        return TaskResult.new(r)
    end
    raise IDPException.new(r['message'])
end

#run_simple_task(file: nil, file_type: nil, poll_interval: 3, timeout: 600) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/idp_ruby.rb', line 177

def run_simple_task(file:nil, file_type:nil, poll_interval:3, timeout:600)
    """
        Run simple extraction task 
        :param file: Pdf/image file. Only one file is allowed to be uploaded each time
        :type file: file
        :param file_type: The code of the file type (e.g., CBKS). Please see details of File Type Code.
        :type file_type: FileType
        :param poll_interval: Interval to poll the result from api, in seconds
        :type poll_interval: float
        :param timeout: Timeout in seconds
        :type timeout: float
    """
    ct=timeout/poll_interval
    task = create(file:file, file_type:file_type)
    task_result = result(task.task_id)
    while(task_result.status=='Doing' or task_result.status=='Init')
        if (ct-=1) == 0
            raise IDPException.new('Task timeout exceeded: {timeout}')
        end
        puts "Task is doing. Please wait."
        sleep(poll_interval)
        task_result = result(task.task_id)
        
        if task_result.status == 'Done'
            return task_result
        end
    end
    return task_result
end