Sha256: 40cf694cc4f57c44f00ec17b9eb2002433de6851ea41f3a2dfc5d2c9295fdc56
Contents?: true
Size: 1.81 KB
Versions: 1
Compression:
Stored size: 1.81 KB
Contents
require "uri" module Chuckle class Client include Chuckle::Options attr_accessor :options, :cache def initialize(options = {}) self.options = DEFAULT_OPTIONS.merge(options) self.cache = Cache.new(self) sanity! end # # main entry points # def create_request(uri, body = nil) uri = URI.parse(uri.to_s) if !uri.is_a?(URI) Request.new(self, uri, body) end def get(uri) run(create_request(uri)) end def post(uri, body) body = case body when Hash Util.hash_to_query(body) else body.to_s end run(create_request(uri, body)) end def run(request) response = cache.get(request) || curl(request) raise_errors(response) response end def inspect #:nodoc: self.class.name end protected # make sure curl command exists def sanity! system("which curl > /dev/null") raise "Chuckle requires curl. Please install it." if $? != 0 end def curl(request) vputs request.uri rate_limit!(request) cache.set(request, Curl.new(request)) end def raise_errors(response) # raise errors if necessary error = if response.curl_exit_code "curl exit code #{response.curl_exit_code}" elsif response.code >= 400 "http status #{response.code}" end return if !error if !cache_errors? cache.clear(response.request) end raise Error.new("Chuckle::Error, #{error}", response) end def vputs(s) puts "chuckle: #{s}" if verbose? end def rate_limit!(request) return if !request.uri.host @last_request ||= Time.at(0) sleep = (@last_request + rate_limit) - Time.now sleep(sleep) if sleep > 0 @last_request = Time.now end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
chuckle-1.0.1 | lib/chuckle/client.rb |