Sha256: 31e77fec1e033acb87145ada7a6a1b5a9c7f8c9dc6f59f58d6ce9ae6e14fc9c8

Contents?: true

Size: 808 Bytes

Versions: 1

Compression:

Stored size: 808 Bytes

Contents

require 'uri'
require 'curb'

module Parsable
  class Remote
    # uses the response from a remote location

    def initialize
    end

    def method_missing(method_sym, *arguments, &block)
      if uri = uri?(method_sym)
        get_response(uri)
      end
    end

    private

    def get_response uri
      0.upto(2) do |i|
        begin
          Curl::Easy.perform(uri.to_s) do |http|
            http.connect_timeout = 2
            http.on_success { |easy| @body = easy.body_str }
          end
        rescue Curl::Err::CurlError
        end

        break if @body
      end
      @body
    end

    def uri? string
      uri = URI.parse(string)
      if uri && uri.kind_of?(URI::HTTP)
        uri
      else
        false
      end
    rescue URI::InvalidURIError
      false
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
parsable-0.2.0 lib/parsable/remote.rb