Giter VIP home page Giter VIP logo

micky's Introduction

Micky

Micky makes simple HTTP requests (GET/HEAD), follows redirects, handles exceptions (invalid hosts/URIs, server errors, timeouts, redirect loops), automatically parses responses (JSON, etc.), is very lightweight, and has no dependency.

Micky is for those times you would have used Net::HTTP or OpenURI, but don’t want to bother handling all the sneaky things mentionned above, and don’t want to add heavy dependencies to your app.

Installation

Add this line to your application’s Gemfile:

gem 'micky'

And then execute:

$ bundle

Or install it yourself as:

$ gem install micky

Usage

Micky provides two methods: get and head.

On successful requests, it will return a subclass of Net::HTTPReponse. For any error it might encounter during the request (invalid hosts/URIs, server errors, timeouts, redirect loops), it will return nil.

response = Micky.get('http://google.com')
response.content_type # "text/html"
response.body         # "<!doctype html><html ..."

response = Micky.get('http://invalidhost.foo')
response # nil

Classic example

if Micky.head(params[:website_url])
  # User provided a valid URL
  url = URI(params[:website_url])
  url.path = '/favicon.ico'

  if favicon = Micky.get(url)
    # Do whatever with the raw `favicon.data`, for whatever reason
  else
    # This site has no favicon, or a broken one, too bad
  end
else
  # Some error happened, display error message to user
end

Automatically parse responses into Ruby objects

Micky::Response#body always returns the response as a string. To parse this string into a Ruby object, use Micky::Response#data.

Responses with Content-Type: application/json are automatically parsed by Ruby’s JSON library.

response = Micky.get('http://urls.api.twitter.com/1/urls/count.json?url=dropmeme.com')
response.content_type # 'application/json'

# plain string
response.body # '{"count":33,"url":"http://dropmeme.com/"}'

# proper hash
response.data # {"count"=>33, "url"=>"http://dropmeme.com/"}

Add custom parsers

To add custom response parsers for specific content-types, insert lambdas in the Micky.parsers hash.

For instance, to parse HTML documents with Nokogiri:

Micky.parsers['text/html'] = -> (body) {
  Nokogiri::HTML(body)
}

Overwrite the default application/json parser to use Oj:

Micky.parsers['application/json'] = -> (body) {
  begin
    Oj.load(body)
  rescue Oj::ParseError
  end
}

Parse images into mini_magick instances:

parser = -> (body) {
  begin
    MiniMagick::Image.read(body)
  rescue MiniMagick::Invalid
  end
}

%w[image/png image/jpeg image/jpg image/gif].each do |type|
  Micky.parsers[type] = parser
end

TODO

  • Add tests
  • Better document configuration options in README

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

© 2013 Rafaël Blais Masson. Micky is released under the MIT license.

micky's People

Contributors

rafbm avatar

Stargazers

Ali Farag avatar

Watchers

Rémi Prévost avatar James Cloos avatar  avatar

Forkers

ali-farag

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.