Class: Bloopi::HTTPService::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Request.

Parameters:

  • path (defaults to: path)

    the server path for this request

  • args (defaults to: {})
  • verb (defaults to: 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.

  • options (defaults to: {})

Options Hash (options:):

  • :use_ssl (Object)

    force https, even if not needed

  • :json (Object)

    whether or not to send JSON to Bloopi



15
16
17
18
19
20
# File 'lib/bloopi/http_service/request.rb', line 15

def initialize(path: path, verb: 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/bloopi/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/bloopi/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/bloopi/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/bloopi/http_service/request.rb', line 4

def raw_verb
  @raw_verb
end

Instance Method Details

#get_argsObject



53
54
55
# File 'lib/bloopi/http_service/request.rb', line 53

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

#json?Boolean

Whether or not this request should use JSON.

Returns:

  • (Boolean)

    true or false



72
73
74
# File 'lib/bloopi/http_service/request.rb', line 72

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



61
62
63
64
65
66
67
# File 'lib/bloopi/http_service/request.rb', line 61

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 Bloopi, incorporating an API version if specified.

Returns:

  • the original path, with API version if appropriate.



32
33
34
35
36
37
# File 'lib/bloopi/http_service/request.rb', line 32

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] || Bloopi.config.api_version
  "/#{api_version}/#{raw_path}"
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



43
44
45
46
47
48
49
50
51
# File 'lib/bloopi/http_service/request.rb', line 43

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 Bloopi server.

Returns:

  • a complete server address with protocol



79
80
81
# File 'lib/bloopi/http_service/request.rb', line 79

def server
  uri = "#{options[:use_ssl] ? "https" : "http"}://#{Bloopi.config.api_server}"
end

#verbObject

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

Returns:

  • one of get or post



25
26
27
# File 'lib/bloopi/http_service/request.rb', line 25

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