module Koala::HTTPService

Constants

DEFAULT_MIDDLEWARE

Koala's default middleware stack. We encode requests in a Facebook-compatible multipart request, and use whichever adapter has been configured for this application.

DEFAULT_SERVERS

Default server information for Facebook. These can be overridden by setting config values. See Koala.config.

Attributes

faraday_middleware[RW]

A customized stack of Faraday middleware that will be used to make each request.

http_options[RW]

A default set of HTTP options (see github.com/arsduo/koala/wiki/HTTP-Services)

Public Class Methods

encode_params(param_hash) click to toggle source

Encodes a given hash into a query string. This is used mainly by the Batch API nowadays, since Faraday handles this for regular cases.

@param params_hash a hash of values to CGI-encode and appropriately join

@example

Koala.http_service.encode_params({:a => 2, :b => "My String"})
=> "a=2&b=My+String"

@return the appropriately-encoded string

   # File lib/koala/http_service.rb
80 def self.encode_params(param_hash)
81   ((param_hash || {}).sort_by{|k, v| k.to_s}.collect do |key_and_value|
82     value = key_and_value[1]
83     unless value.is_a? String
84       value = value.to_json
85     end
86     "#{key_and_value[0].to_s}=#{CGI.escape value}"
87   end).join("&")
88 end
make_request(request) click to toggle source

Makes a request directly to Facebook. @note You'll rarely need to call this method directly.

@see Koala::Facebook::API#api @see Koala::Facebook::GraphAPIMethods#graph_call

@param request a Koala::HTTPService::Request object

@raise an appropriate connection error if unable to make the request to Facebook

@return [Koala::HTTPService::Response] a response object representing the results from Facebook

   # File lib/koala/http_service.rb
48 def self.make_request(request)
49   # set up our Faraday connection
50   conn = Faraday.new(request.server, faraday_options(request.options), &(faraday_middleware || DEFAULT_MIDDLEWARE))
51 
52   if request.verb == "post" && request.json?
53     # JSON requires a bit more handling
54     # remember, all non-GET requests are turned into POSTs, so this covers everything but GETs
55     response = conn.post do |req|
56       req.path = request.path
57       req.headers["Content-Type"] = "application/json"
58       req.body = request.post_args.to_json
59       req
60     end
61   else
62     response = conn.send(request.verb, request.path, request.post_args)
63   end
64 
65   # Log URL information
66   Koala::Utils.debug "#{request.verb.upcase}: #{request.path} params: #{request.raw_args.inspect}"
67   Koala::HTTPService::Response.new(response.status.to_i, response.body, response.headers)
68 end

Private Class Methods

faraday_options(options) click to toggle source
   # File lib/koala/http_service.rb
92 def self.faraday_options(options)
93   valid_options = [:request, :proxy, :ssl, :builder, :url, :parallel_manager, :params, :headers, :builder_class]
94   Hash[ options.select { |key,value| valid_options.include?(key) } ]
95 end