Skip to content
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

Issue 226 - Don't create a new Faraday instance on every request #233

Merged
merged 4 commits into from
Feb 3, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/twitter/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,18 @@ def connection(options={})
:ssl => {:verify => false},
:url => options.fetch(:endpoint, endpoint),
}
Faraday.new(default_options.deep_merge(connection_options)) do |builder|
builder.use Twitter::Request::Phoenix if options[:phoenix]
@connection ||=Faraday.new(default_options.deep_merge(connection_options)) do |builder|
builder.use Twitter::Request::Phoenix
builder.use Twitter::Request::MultipartWithFile
builder.use Twitter::Request::TwitterOAuth, credentials if credentials?
builder.use Faraday::Request::Multipart
builder.use Faraday::Request::UrlEncoded
builder.use Twitter::Request::Gateway, gateway if gateway
builder.use Twitter::Response::RaiseClientError
builder.use Twitter::Response::ParseJson unless options[:raw]
builder.use Twitter::Response::ParseJson
builder.use Twitter::Response::RaiseServerError
builder.adapter(adapter)
end
end

end
end
4 changes: 3 additions & 1 deletion lib/twitter/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def post(path, params={}, options={})

# Perform an HTTP request
def request(method, path, params, options)
response = connection(options).send(method) do |request|
response = connection(options).run_request(method, nil, nil, nil) do |request|
request.options[:phoenix] = true if options[:phoenix]
request.options[:raw] = true if options[:raw]
case method.to_sym
when :delete, :get
request.url(path, params)
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/request/phoenix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def call(env)
# Not sure what what the X-Phx (Phoenix?) header is for but it's
# required to access certain undocumented resources
# e.g. GET urls/resolve
env[:request_headers]['X-Phx'] = 'true'
env[:request_headers]['X-Phx'] = 'true' if env[:request][:phoenix]

@app.call(env)
end
Expand Down
25 changes: 15 additions & 10 deletions lib/twitter/response/parse_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@ module Response
class ParseJson < Faraday::Response::Middleware

def parse(body)
case body
when ''
nil
when 'true'
true
when 'false'
false
else
::MultiJson.decode(body)
case body
when ''
nil
when 'true'
true
when 'false'
false
else
::MultiJson.decode(body)
end
end

def on_complete(env)
if respond_to? :parse
env[:body] = parse(env[:body]) unless env[:request][:raw] or [204,304].index env[:status]
end
end

end
end
end