Class: Koala::HTTPService::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/koala/http_service/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, verb:, args: {}, options: {}) ⇒ Request

Returns a new instance of Request.

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.

  • options (defaults to: {})

    various flags to indicate which server to use. (see Koala::Facebook::API#api)

  • options (defaults to: {})
  • args (defaults to: {})

    arguments to be sent to Facebook

Options Hash (options:):

  • :video (Object)

    use the server designated for video uploads

  • :beta (Object)

    use the beta tier

  • :use_ssl (Object)

    force https, even if not needed

  • :json (Object)

    whether or not to send JSON to Facebook



17
18
19
20
21
22
# File 'lib/koala/http_service/request.rb', line 17

def initialize(path:, verb:, args: {}, options: {})
  @raw_path = path
  @raw_args = args
  @raw_verb = verb
  @raw_options = options
end

Instance Attribute Details

#raw_argsObject (readonly)

Returns the value of attribute raw_args.



4
5
6
# File 'lib/koala/http_service/request.rb', line 4

def raw_args
  @raw_args
end

#raw_optionsObject (readonly)

Returns the value of attribute raw_options.



4
5
6
# File 'lib/koala/http_service/request.rb', line 4

def raw_options
  @raw_options
end

#raw_pathObject (readonly)

Returns the value of attribute raw_path.



4
5
6
# File 'lib/koala/http_service/request.rb', line 4

def raw_path
  @raw_path
end

#raw_verbObject (readonly)

Returns the value of attribute raw_verb.



4
5
6
# File 'lib/koala/http_service/request.rb', line 4

def raw_verb
  @raw_verb
end

Instance Method Details

#get_argsObject



61
62
63
# File 'lib/koala/http_service/request.rb', line 61

def get_args
  raw_verb == "get" ? args : {}
end

#json?Boolean

Whether or not this request should use JSON.

Returns:

  • (Boolean)

    true or false



80
81
82
# File 'lib/koala/http_service/request.rb', line 80

def json?
  raw_options[:format] == :json
end

#optionsObject

Calculates a set of request options to pass to Faraday.

any specified for the request.

Returns:

  • a hash combining GET parameters (if appropriate), default options, and



69
70
71
72
73
74
75
# File 'lib/koala/http_service/request.rb', line 69

def options
  # figure out our options for this request
  add_ssl_options(
    # for GETs, we pass the params to Faraday to encode
    {params: get_args}.merge(HTTPService.http_options).merge(raw_options)
  )
end

#pathObject

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

Returns:

  • the original path, with API version if appropriate.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/koala/http_service/request.rb', line 34

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

#post_argsObject

Determines any arguments to be sent in a POST body.

other values

Returns:

  • {} for GET; the provided args for POST; those args with the method parameter for



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

def post_args
  if raw_verb == "get"
    {}
  elsif raw_verb == "post"
    args
  else
    args.merge(method: raw_verb)
  end
end

#serverObject

The address of the appropriate Facebook server.

Returns:

  • a complete server address with protocol



87
88
89
90
91
92
93
94
95
96
# File 'lib/koala/http_service/request.rb', line 87

def server
  uri = "#{options[:use_ssl] ? "https" : "http"}://#{Koala.config.graph_server}"
  # if we want to use the beta tier or the video server, make those substitutions as
  # appropriate
  replace_server_component(
    replace_server_component(uri, options[:video], Koala.config.video_replace),
    options[:beta],
    Koala.config.beta_replace
  )
end

#verbObject

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.

Returns:

  • one of get or post



27
28
29
# File 'lib/koala/http_service/request.rb', line 27

def verb
  ["get", "post"].include?(raw_verb) ? raw_verb : "post"
end