Sha256: 216cdee5f6015350aad0d61e607c3e243f35bebb7988dfb36695c78e2b6c618c

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

require 'ocrsdk'
require 'webmock'

module OCRSDK::Mock
  MOCKS_PATH = File.realpath(File.join(File.dirname(__FILE__), '..', '..', 'mocks'))

  class << self
    def success
      stub_process_image response(:process_image, :success)
      stub_get_task_status response(:get_task_status, :completed)
      stub_result response(:result, :simple)
    end

    def in_progress
      success
      stub_get_task_status response(:get_task_status, :in_progress)
    end

    def not_enough_credits
      success
      stub_process_image response(:process_image, :not_enough_credits)
      stub_get_task_status response(:get_task_status, :not_enough_credits)
    end

    def stub_process_image(response)
      WebMock::API.stub_request(:post, /.*:.*@cloud.ocrsdk.com\/processImage/).to_return(body: response)
    end

    def stub_get_task_status(response)
      WebMock::API.stub_request(:get,  /.*:.*@cloud.ocrsdk.com\/getTaskStatus\?taskId=.*/).to_return(body: response)
    end

    def stub_result(response)
      WebMock::API.stub_request(:get, 'http://cloud.ocrsdk.com/result_url').to_return(body: response)
    end

    def response(method, status)
      path = File.join(MOCKS_PATH, method.to_s, "#{status.to_s}.xml")

      unless File.exist? path
        warn "No predefined xml response for #{method}/#{status} found. Path: #{path}"
      end

      File.new(path).read
    end    
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ocrsdk-0.3.4 lib/ocrsdk/mock.rb
ocrsdk-0.3.3 lib/ocrsdk/mock.rb
ocrsdk-0.3.2 lib/ocrsdk/mock.rb
ocrsdk-0.3.1 lib/ocrsdk/mock.rb
ocrsdk-0.3.0 lib/ocrsdk/mock.rb