-
Notifications
You must be signed in to change notification settings - Fork 321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Response#parse #61
Conversation
@@ -118,6 +118,18 @@ def to_s | |||
body.to_s | |||
end | |||
|
|||
# Return parsed response body according to its content type | |||
def parsed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I'd prefer calling this method just #parse
. Would be curious what others think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dislike parse
as it's a verb... I might be wrong here.
Probably?:
def parse
# ...
end
alias :parsed :parse
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After discussion I agreed that #parse
is better, as it's better conforms with #to_s
.
Can you rebase against master? @sferik, thoughts on this? |
To be honest, the fact that this library attempts to handle MIME types seems a bit ambitious. I suppose this is a nice convenience, as long as we only support JSON. Not sure about the name Also it seems strange that the consumer needs to |
I could go either way on this. I think it's convenient, but if it doesn't do a good job it probably isn't helping anyone.
Seems good
I am also down to discuss this as I too am not super happy with
Agree. |
Access to parsed body was nuked by merging #39. This patch brings it back (feature, not the API). Example: require 'json' res = HTTP.get('https://api.github.com/repos/tarcieri/http') res.parse['description'] # => "The HTTP Gem: a simple Ruby DSL for making HTTP requests"
@tarcieri rebased I agree that
json.prepare do
begin
require "multi_json"
rescue LoadError
fail "MultiJSON was not required"
end
end
# MimeType API changes proposal
module HTTP
# Yes, HTTP bundles its own MIME type library. Maybe it should be spun off
# as a separate gem or something.
class MimeType
def ready?
prepare unless @ready
@ready = true
end
# should be implemented on concrete adapter
def parse(obj)
obj
end
# should be implemented on concrete adapter
def emit(obj)
obj
end
# should be implemented on concrete adapter
def prepare; end
def depend_on(lib)
require lib
rescue LoadError
fail "#{self.class.adapter_name} adapter depends on #{lib}"
end
private :depend_on
class << self
def adapter_name shortcut
# ...
end
def adapter_mime *mimetypes
# ...
end
end
end
# built in adapters
require 'http/mime_types/json'
require 'http/mime_types/xml' The proposed idea will allow following adapter declaration: class HTTP::MimeType::JSON < HTTP::MimeType
adapter_name :json
adapter_mime "application/json"
def prepare
depend_on "multi_json"
end
def parse(obj)
MultiJSON.load obj
end
def emit(obj)
MultiJSON.dump obj
end
end |
My main reason why I need multiply MIME-types per adapter is because I need to parse # append another mime type to be served by XML adapter
HTTP::MimeType::XML.addapter_mime "application/atom+xml" |
I'm not a fan of MultiJSON:
For the most part I like your |
OK. I tend to agree about MultiJSON. In a worst case if one would like to use OJ he will be able to easily monkey-patch built-in adapter after all. In this case it's really worth to support only JSON out of the box. Will serve two purposes: fairly common adapter (for Web) and example of how to write adapters. I think I'll raise different PR with API changes for MimeType - it won't affect this PR anyway. |
After some discussions I'm closing this PR. MimeType was broken and it's current state doesn't worth to spend time on updating it. |
Closed in favor of #64 :D |
I mean |
Access to parsed body was nuked by merging #39.
This patch brings it back (feature, not the API).
Example