Module: Koala::HTTPService

Defined in:
lib/koala/http_service.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, 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 servers for Facebook. These are read into the config OpenStruct, and can be overridden via Koala.config.

{
  :graph_server => 'graph.facebook.com',
  :dialog_host => 'www.facebook.com',
  :rest_server => 'api.facebook.com',
  # certain Facebook services (beta, video) require you to access different
  # servers. If you're using your own servers, for instance, for a proxy,
  # you can change both the matcher and the replacement values.
  # So for instance, if you're talking to fbproxy.mycompany.com, you could
  # set up beta.fbproxy.mycompany.com for FB's beta tier, and set the
  # matcher to /\.fbproxy/ and the beta_replace to '.beta.fbproxy'.
  :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.



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

def faraday_middleware
  @faraday_middleware
end

.http_optionsObject

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



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

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



121
122
123
124
125
126
# File 'lib/koala/http_service.rb', line 121

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

.make_request(path, args, verb, options = {}) ⇒ Koala::HTTPService::Response

Note:

You’ll rarely need to call this method directly.

Makes a request directly to Facebook.

Parameters:

  • path

    the server path for this request

  • verb

    the HTTP method to use. If not get or post, this will be turned into a POST request with the appropriate :method specified in the arguments.

  • args

    arguments to be sent to Facebook

  • options (defaults to: {})

    request-related options for Koala and Faraday. See github.com/arsduo/koala/wiki/HTTP-Services for additional options.

Returns:

Raises:

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

See Also:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/koala/http_service.rb', line 76

def self.make_request(path, args, verb, options = {})
  # if the verb isn't get or post, send it as a post argument
  args.merge!({:method => verb}) && verb = "post" if verb != "get" && verb != "post"

  # turn all the keys to strings (Faraday has issues with symbols under 1.8.7) and resolve UploadableIOs
  params = args.inject({}) {|hash, kv| hash[kv.first.to_s] = kv.last.is_a?(UploadableIO) ? kv.last.to_upload_io : kv.last; hash}

  # figure out our options for this request
  request_options = {:params => (verb == "get" ? params : {})}.merge(http_options || {}).merge(process_options(options))
  request_options[:use_ssl] = true if args["access_token"] # require https if there's a token
  if request_options[:use_ssl]
    ssl = (request_options[:ssl] ||= {})
    ssl[:verify] = true unless ssl.has_key?(:verify)
  end

  # if an api_version is specified and the path does not already contain
  # one, prepend it to the path
  api_version = request_options[:api_version] || Koala.config.api_version
  if api_version && !path_contains_api_version?(path)
    begins_with_slash = path[0] == "/"
    divider = begins_with_slash ? "" : "/"
    path = "/#{api_version}#{divider}#{path}"
  end

  # set up our Faraday connection
  # we have to manually assign params to the URL or the
  conn = Faraday.new(server(request_options), faraday_options(request_options), &(faraday_middleware || DEFAULT_MIDDLEWARE))

  response = conn.send(verb, path, (verb == "post" ? params : {}))

  # Log URL information
  Koala::Utils.debug "#{verb.upcase}: #{path} params: #{params.inspect}"
  Koala::HTTPService::Response.new(response.status.to_i, response.body, response.headers)
end

.path_contains_api_version?(path) ⇒ Boolean

Determines whether a given path already contains an API version.

Parameters:

  • path

    the URL path.

Returns:

  • (Boolean)

    true or false accordingly.



133
134
135
136
# File 'lib/koala/http_service.rb', line 133

def self.path_contains_api_version?(path)
  match = /^\/?(v\d+(?:\.\d+)?)\//.match(path)
  !!(match && match[1])
end

.server(options = {}) ⇒ Object

The address of the appropriate Facebook server.

Parameters:

  • options (defaults to: {})

    various flags to indicate which server to use.

Options Hash (options):

  • :rest_api (Object)

    use the old REST API instead of the Graph API

  • :video (Object)

    use the server designated for video uploads

  • :beta (Object)

    use the beta tier

  • :use_ssl (Object)

    force https, even if not needed

Returns:

  • a complete server address with protocol



52
53
54
55
56
57
# File 'lib/koala/http_service.rb', line 52

def self.server(options = {})
  server = "#{options[:rest_api] ? Koala.config.rest_server : Koala.config.graph_server}"
  server.gsub!(Koala.config.host_path_matcher, Koala.config.video_replace) if options[:video]
  server.gsub!(Koala.config.host_path_matcher, Koala.config.beta_replace) if options[:beta]
  "#{options[:use_ssl] ? "https" : "http"}://#{server}"
end