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
-
.faraday_middleware ⇒ Object
A customized stack of Faraday middleware that will be used to make each request.
-
.http_options ⇒ Object
A default set of HTTP options (see github.com/arsduo/koala/wiki/HTTP-Services).
Class Method Summary collapse
-
.encode_params(param_hash) ⇒ Object
Encodes a given hash into a query string.
-
.make_request(path, args, verb, options = {}) ⇒ Koala::HTTPService::Response
Makes a request directly to Facebook.
-
.path_contains_api_version?(path) ⇒ Boolean
Determines whether a given path already contains an API version.
-
.server(options = {}) ⇒ Object
The address of the appropriate Facebook server.
Class Attribute Details
.faraday_middleware ⇒ Object
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_options ⇒ Object
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 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.
131 132 133 134 135 136 |
# File 'lib/koala/http_service.rb', line 131 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
You’ll rarely need to call this method directly.
Makes a request directly to Facebook.
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 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/koala/http_service.rb', line 76 def self.make_request(path, args, verb, = {}) # if the verb isn't get or post, send it as a post argument with a method param 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 = {:params => (verb == "get" ? params : {})}.merge( || {}).merge() [:use_ssl] = true if args["access_token"] # require https if there's a token if [:use_ssl] ssl = ([: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 = [: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(), (), &(faraday_middleware || DEFAULT_MIDDLEWARE)) # remember, all non-GET requests are turned into POSTs -- see the the start of this method if verb == "post" && [:format] == :json response = conn.post do |req| req.path = path req.headers["Content-Type"] = "application/json" req.body = params.to_json req end else response = conn.send(verb, path, (verb == "post" ? params : {})) end # 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.
143 144 145 146 |
# File 'lib/koala/http_service.rb', line 143 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.
52 53 54 55 56 57 |
# File 'lib/koala/http_service.rb', line 52 def self.server( = {}) server = "#{[:rest_api] ? Koala.config.rest_server : Koala.config.graph_server}" server.gsub!(Koala.config.host_path_matcher, Koala.config.video_replace) if [:video] server.gsub!(Koala.config.host_path_matcher, Koala.config.beta_replace) if [:beta] "#{[:use_ssl] ? "https" : "http"}://#{server}" end |