class Koala::HTTPService::Request

Attributes

raw_args[R]
raw_options[R]
raw_path[R]
raw_verb[R]

Public Class Methods

new(path:, verb:, args: {}, options: {}) click to toggle source

@param path the server path for this request @param args (see Koala::Facebook::API#api) @param 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.

@param options various flags to indicate which server to use. (see Koala::Facebook::API#api) @param options @option options :video use the server designated for video uploads @option options :beta use the beta tier @option options :use_ssl force https, even if not needed @option options :json whether or not to send JSON to Facebook

   # File lib/koala/http_service/request.rb
17 def initialize(path:, verb:, args: {}, options: {})
18   @raw_path = path
19   @raw_args = args
20   @raw_verb = verb
21   @raw_options = options
22 end

Public Instance Methods

get_args() click to toggle source
   # File lib/koala/http_service/request.rb
61 def get_args
62   raw_verb == "get" ? args : {}
63 end
json?() click to toggle source

Whether or not this request should use JSON.

@return true or false

   # File lib/koala/http_service/request.rb
80 def json?
81   raw_options[:format] == :json
82 end
options() click to toggle source

Calculates a set of request options to pass to Faraday.

@return a hash combining GET parameters (if appropriate), default options, and any specified for the request.

   # File lib/koala/http_service/request.rb
69 def options
70   # figure out our options for this request
71   add_ssl_options(
72     # for GETs, we pass the params to Faraday to encode
73     {params: get_args}.merge(HTTPService.http_options).merge(raw_options)
74   )
75 end
path() click to toggle source

Determines the path to be requested on Facebook, incorporating an API version if specified.

@return the original path, with API version if appropriate.

   # File lib/koala/http_service/request.rb
34 def path
35   # if an api_version is specified and the path does not already contain
36   # one, prepend it to the path
37   api_version = raw_options[:api_version] || Koala.config.api_version
38   if api_version && !path_contains_api_version?
39     begins_with_slash = raw_path[0] == "/"
40     divider = begins_with_slash ? "" : "/"
41     "/#{api_version}#{divider}#{raw_path}"
42   else
43     raw_path
44   end
45 end
post_args() click to toggle source

Determines any arguments to be sent in a POST body.

@return {} for GET; the provided args for POST; those args with the method parameter for other values

   # File lib/koala/http_service/request.rb
51 def post_args
52   if raw_verb == "get"
53     {}
54   elsif raw_verb == "post"
55     args
56   else
57     args.merge(method: raw_verb)
58   end
59 end
server() click to toggle source

The address of the appropriate Facebook server.

@return a complete server address with protocol

   # File lib/koala/http_service/request.rb
87 def server
88   uri = "#{options[:use_ssl] ? "https" : "http"}://#{Koala.config.graph_server}"
89   # if we want to use the beta tier or the video server, make those substitutions as
90   # appropriate
91   replace_server_component(
92     replace_server_component(uri, options[:video], Koala.config.video_replace),
93     options[:beta],
94     Koala.config.beta_replace
95   )
96 end
verb() click to toggle source

Determines which type of request to send to Facebook. Facebook natively accepts GETs and POSTs, for others we have to include the method in the post body.

@return one of get or post

   # File lib/koala/http_service/request.rb
27 def verb
28   ["get", "post"].include?(raw_verb) ? raw_verb : "post"
29 end

Protected Instance Methods

add_ssl_options(opts) click to toggle source
    # File lib/koala/http_service/request.rb
108 def add_ssl_options(opts)
109   # require https if there's a token
110   return opts unless raw_args["access_token"]
111 
112   {
113     use_ssl: true,
114     ssl: {verify: true}.merge(opts[:ssl] || {})
115   }.merge(opts)
116 end
args() click to toggle source

The arguments to include in the request.

    # File lib/koala/http_service/request.rb
101 def args
102   raw_args.inject({}) do |hash, (key, value)|
103     # Resolve UploadableIOs into data Facebook can work with
104     hash.merge(key => value.is_a?(UploadableIO) ? value.to_upload_io : value)
105   end
106 end
path_contains_api_version?() click to toggle source

Determines whether a given path already contains an API version.

@param path the URL path.

@return true or false accordingly.

    # File lib/koala/http_service/request.rb
123 def path_contains_api_version?
124   # looks for "/$MAJOR[.$MINOR]/" in the path
125   match = /^\/?(v\d+(?:\.\d+)?)\//.match(raw_path)
126   !!(match && match[1])
127 end
replace_server_component(host, condition_met, replacement) click to toggle source
    # File lib/koala/http_service/request.rb
129 def replace_server_component(host, condition_met, replacement)
130   return host unless condition_met
131   host.gsub(Koala.config.host_path_matcher, replacement)
132 end