Module: Koala::HTTPService

Defined in:
lib/koala/http_service.rb,
lib/koala/http_service/request.rb,
lib/koala/http_service/response.rb,
lib/koala/http_service/uploadable_io.rb,
lib/koala/http_service/multipart_request.rb

Defined Under Namespace

Classes: MultipartRequest, Request, Response, UploadableIO

Constant Summary collapse

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.

Proc.new do |builder|
  builder.use Koala::HTTPService::MultipartRequest
  builder.request :url_encoded
  builder.adapter Faraday.default_adapter
end
DEFAULT_SERVERS =

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

{
  :graph_server => 'graph.facebook.com',
  :dialog_host => 'www.facebook.com',
  :host_path_matcher => /\.facebook/,
  :video_replace => '-video.facebook',
  :beta_replace => '.beta.facebook'
}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.faraday_middlewareObject

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


11
12
13
# File 'lib/koala/http_service.rb', line 11

def faraday_middleware
  @faraday_middleware
end

.http_optionsObject

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


13
14
15
# File 'lib/koala/http_service.rb', line 13

def http_options
  @http_options
end

Class Method Details

.encode_params(param_hash) ⇒ Object

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

Examples:

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

Parameters:

  • params_hash

    a hash of values to CGI-encode and appropriately join

Returns:

  • the appropriately-encoded string


91
92
93
94
95
96
97
98
99
# File 'lib/koala/http_service.rb', line 91

def self.encode_params(param_hash)
  ((param_hash || {}).sort_by{|k, v| k.to_s}.collect do |key_and_value|
    value = key_and_value[1]
    unless value.is_a? String
      value = value.to_json
    end
    "#{key_and_value[0].to_s}=#{CGI.escape value}"
  end).join("&")
end

.make_request(request) ⇒ Koala::HTTPService::Response

Note:

You'll rarely need to call this method directly.

Makes a request directly to Facebook.

Parameters:

  • request

    a Koala::HTTPService::Request object

Returns:

Raises:

  • an appropriate connection error if unable to make the request to Facebook

See Also:


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/koala/http_service.rb', line 48

def self.make_request(request)
  # set up our Faraday connection
  conn = Faraday.new(request.server, faraday_options(request.options), &(faraday_middleware || DEFAULT_MIDDLEWARE))

  filtered_args = request.raw_args.dup.transform_keys(&:to_s)

  if Koala.config.mask_tokens
    %w(access_token input_token).each do |arg_token|
      if (token = filtered_args[arg_token])
        filtered_args[arg_token] = token[0, 10] + '*****' + token[-5, 5]
      end
    end
  end

  Koala::Utils.debug "STARTED => #{request.verb.upcase}: #{request.path} params: #{filtered_args.inspect}"

  if request.verb == "post" && request.json?
    # JSON requires a bit more handling
    # remember, all non-GET requests are turned into POSTs, so this covers everything but GETs
    response = conn.post do |req|
      req.path = request.path
      req.headers["Content-Type"] = "application/json"
      req.body = request.post_args.to_json
      req
    end
  else
    response = conn.send(request.verb, request.path, request.post_args)
  end

  Koala::Utils.debug "FINISHED => #{request.verb.upcase}: #{request.path} params: #{filtered_args.inspect}"
  Koala::HTTPService::Response.new(response.status.to_i, response.body, response.headers)
end