Skip to content

Instantly share code, notes, and snippets.

Created June 27, 2012 06:40
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save hemanth/3002015 to your computer and use it in GitHub Desktop.
HTTP client library is so convenient Faraday of Ruby

HTTP client library is so convenient Faraday of Ruby

Faraday's so convenient Ruby HTTP client library

The development of the API wrapper [RestClient gem] or I rest_client_gem
What you need for OAuth was using [Net / HTTP] net_http + [OAuth gem] the oauth_gem

After reading the source of the API library [Twitter gem] and twitter_gem [Instagram gem] such as instagram_gem
Thing [Faraday gem] that faraday_gem had been commonly used

Was examined somehow I started wondering about the Faraday
Was to use a Faraday [Tumblife gem] the tumblife_gem incidentally

Note that the following

# What is Faraday?

Faraday is an HTTP client lib that provides a common interface over many adapters (such as Net :: HTTP) and embraces the concept of Rack middleware when processing the request / response cycle.

Faraday is a HTTP client library
Provides a common interface adapter and a number of other Net :: HTTP
Be able to handle the request / response cycles in the interface, such as Rack middleware also

# Adapter that is supported by the Faraday?

  • Net / HTTP
  • Excon
  • Typhoeus
  • Patron
  • EventMachine

# Usage?

Like this can be used in
to declare the middleware for use with builder.use

`` `Ruby conn = Faraday :: (: url => '') do | builder | to URL-encode the builder.use Faraday :: Request :: UrlEncoded # request parameters to standard output builder.use Faraday :: Response :: Logger # request use the adapter builder.use Faraday :: Adapter :: NetHttp # Net / HTTP end

response = conn.get '/ api / nyan.json' # GET puts response.body `` `

Part of Faraday :: ... could also be written with or anti-

`Ruby conn = (: url => '') do | builder | builder.request: url_encoded builder.response: logger builder.adapter: net_http end `

You can also write the sending part of the GET request also also Section

`` `Ruby conn.get '/ api / nyan.json', {: color =>: black} # GET

response = conn.get do | req | # GET req.url '/ api / nyan.json', {: color =>: white} req.params [: size] =: big end `` `

POST is a way to write

`` `Ruby '/ api / wan.json', {: color =>: black} # POST "color = black" to do | req | req.url '/ api / wan.json' req.body = { : Color =>: black, : Size =>: big } end `` `

Can also upload files

`` `Ruby conn = (: url => '') do | builder | send data in a multi-part multipart #: builder.request builder.request: url_encoded builder.adapter: net_http end

params = { : Name => 'nyanco', : Picture => Faraday :: ('nyanco.jpg', 'image / jpeg') } conn.put '/ api / nyan.json', params `` `

# Use the middleware

In addition to the above, there is this middleware
An excerpt (presumably) only primary

  • Request *: Basic_authentication (basic authentication) *: (To reconnect until the specified number of times when the request fails) retry

  • Response *: (Throw an exception when a 4xx, 5xx error) raise_error

There is a nice library [FaradayMiddleware gem] that faraday_middleware_gem In addition to
Middleware will be able to use a variety of useful and use it

  • Request *: Oauth (/ [SimpleAuth gem] OAuth support dependent simple_auth_gem) *: Oauth2 (corresponding OAuth2)

  • Response *: (To cache the response) caching *: Parse_json (Hash return in the form of a JSON response / [JSON gem] dependent json_gem) *: Parse_xml (Hash return in the form of the XML response / [MultiXML gem] dependent multi_xml_gem) *: Mashify (return in the form of response Hashie :: Mash the Hash / [Hashie gem] dependent hashie_gem) *: (Follow the redirect) follow_redirects

That so convenient a place like this or

When you pass a parameter to the middleware, and later pass the second argument of the way when you declare the middleware
Middleware in order to write the note as it is called in reverse order they were declared

`Ruby conn = (options) do | builder | credentials = { : Consumer_key => consumer_key, : Consumer_secret => consumer_secret, : Token => oauth_token, : Token_secret => oauth_token_secret } builder.request: oauth, credentials end `

Or how to use the other options detailed look at me each official document

# To extend your own middleware

create a class call the method has been implemented
Useful because it takes care of and to inherit the Faraday :: Middleware

`` `Ruby class MyMiddlewre <Faraday :: Middleware def call (env) # If you want to do something with the request to list here

@ (env). On_complete do
  # If you want to write something for the response is here

end end `` `

to have the following env is entered in the Hash

  • Request phase *: Method *: Url *: Body

  • Response phase *: Status *: Body *: Response_headers

If you want to fuck only response have to do is inherit the Faraday :: Response :: Middleware

`Ruby class MyResponseMiddleware <Faraday :: Response :: Middleware def on_complete (env) # You do anything with the response end end `

Reference to an extension of your own because of the following middleware [Tumblife gem] in tumblife_gem the way

# I guess I use the Faraday what end?

  • Beautiful write declarative part HTTP request
  • Request / response that can be flexibly configured

I felt you are using myself or this much
I feel that ... since you are using the API library is undeniable famous
If you feel useful to become familiar with but

Because it corresponds to both OAuth / OAuth2 after
Do not have to use both a pain in the neck like a gem by API

# Other

Adapters that can be used in EventMachine has become a little care
Why send the request asynchronously or in parallel with and to use this
Let's examine so I Cut it so convenient to make the crawler etc.

# Reference

[Faraday gem wiki] faraday_wiki
[Faraday Middleware gem wiki] faraday_middleware_wiki

Copy link

This feels like reading documentation while having a bad trip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment