Sha256: 35fa53da02e0f38ecc1fdb39c02ba3adf1f013668d9a443c98663583c6482102

Contents?: true

Size: 1010 Bytes

Versions: 1

Compression:

Stored size: 1010 Bytes

Contents

require 'json'
require 'typhoeus'

module Ocar
  extend self

  TYPES = %w(cartas paquetes dni partidas).freeze
  $hydra = Typhoeus::Hydra.new

  def get_package(track_id)
    requests = setup_request(track_id)
    run_request
    get_response requests
  end

  private

  def setup_request(track_id)
    # e.g. http://www.oca.com.ar/?q=package-locator&type=paquetes&number=1808200000001055400
    TYPES.map do |type|
      request = Typhoeus::Request.new(
        'http://www.oca.com.ar',
        method: :get,
        params: { q: 'package-locator',
                  type: type, number: track_id },
        headers: {
          Accept: 'application/json, text/javascript, */*; q=0.01'
        }
      )
      $hydra.queue(request)
      request
    end
  end

  def run_request
    $hydra.run
  end

  def get_response(requests)
    results = []
    requests.map do |request|
      parsed = JSON.parse request.response.body
      results << parsed if parsed['success'] == true
    end
    results
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ocar-0.2.1 lib/ocar/client.rb