Coder Social home page Coder Social logo

extend "accepts" to accept a Proc. about http HOT 12 CLOSED

 avatar commented on June 16, 2024
extend "accepts" to accept a Proc.

from http.

Comments (12)

tarcieri avatar tarcieri commented on June 16, 2024

The current state of response parsing is up in the air. See #39.

Right now the body should get parsed automatically if the JSON gem is loaded. In the new API, I'd propose:

HTTP.accept(:json).get(url).parse_body

from http.

 avatar commented on June 16, 2024

looks good. could parse() accept a Proc to override the default handler?

from http.

tarcieri avatar tarcieri commented on June 16, 2024

Well, the point of #parse_body is it can know the Content-Type from the response and choose an appropriate parser. I'm not sure what an arbitrary Proc would get you... you could use #body.to_s to obtain a string and parse it however you want at that point ;)

from http.

 avatar commented on June 16, 2024

yeah, #to_s and checking the content-type manually works. my idea was flawed because i was assuming 'Accept' would guarantee a response of the same Content-Type but of course that isn't true. I was thinking about someone who wanted to use a (custom) JSON parser whenever that Content-Type is encountered though. is it going to be possible to extend the content-types that the HTTP library will understand and automatically parse as JSON?

maybe company X has defined their own content-type to describe the schema of a JSON document and would like to take advantage of the letting the http library parse the body. otherwise they're back to checking the Content-Type manually & doing the parsing themselves (i would figure).

from http.

tarcieri avatar tarcieri commented on June 16, 2024

@robgleeson the MIME type handlers are pluggable. See:

https://github.com/tarcieri/http/blob/master/lib/http/mime_types/json.rb

Arguably they could have a better API

from http.

 avatar commented on June 16, 2024

yeah, i think there should probably be an easy way to alias a custom content-type as being understood as JSON(or XML, CSV, or …) by the http lib if it has predefined handlers for JSON/XML/CSV. it would also be nice if I could extend the http lib to understand new content-types and define custom handlers so that the http lib interface doesn't end up being inconsistent or needing a different API for content-type's it doesn't account for.

from http.

 avatar commented on June 16, 2024

by "different API" I mean if I think I might get a content-type that http lib doesn't understand I need to check the content-type and handle the parsing myself, even if response could have a content-type that is understood, there's a chance it doesn't so i cannot rely on #parsed.

from http.

tarcieri avatar tarcieri commented on June 16, 2024

I also like this style of registering API adapters:

https://github.com/copiousfreetime/qup/blob/master/lib/qup/adapter/redis.rb#L10

So it would work like:

class MyHandler < HTTP::MimeType
  register 'application/myformat'

  def parse(data)
    do_awesome_stuff(data)
  end
end

from http.

tarcieri avatar tarcieri commented on June 16, 2024

If that's all you want, you could do it like this:

response = HTTP.accept(:json).get(url)
body = response.body.to_s

case response['Content-Type']
when 'application/myformat'
  ...
end

from http.

 avatar commented on June 16, 2024

yeah, I could, but then the parsed doesn't handle that case, so I can't use it (but I'd like to).
I'd be cool with registering new types as a subclass as long as I can register the type with http so that it is understood and used when #parsed is called.

from http.

 avatar commented on June 16, 2024

something like HTTP::Handler["application/myformat"] = MyHandler.new might be nice (that'd let me re-assign and re-use other handlers that the #parsed method uses).

from http.

 avatar commented on June 16, 2024

gonna close, original idea didn't make sense.

from http.

Related Issues (20)

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.