Class: Parliament::Request::UrlRequest

Inherits:
BaseRequest show all
Defined in:
lib/parliament/request/url_request.rb

Overview

URL request object, allowing the user to build a URL to make a request to an API.

Since:

  • 0.7.5

Constant Summary

Constants inherited from BaseRequest

BaseRequest::CONNECTTIMEOUT, BaseRequest::TIMEOUT

Instance Attribute Summary collapse

Attributes inherited from BaseRequest

#query_params

Instance Method Summary collapse

Methods inherited from BaseRequest

#get, #post

Constructor Details

#initialize(base_url: nil, headers: nil, builder: nil, decorators: nil) ⇒ UrlRequest

Creates a new instance of Parliament::Request::UrlRequest.

request = Parliament::Request::UrlRequest.new(base_url: ‘example.com’, headers: { ‘Access-Token’ => ‘12345678’ }) This will create a request with the Access-Token set to 12345678.

Examples:

Passing headers

Parameters:

  • base_url (String) (defaults to: nil)

    the base url of our api. (expected: example.com - without the trailing slash).

  • headers (Hash) (defaults to: nil)

    the headers being sent in the request.

  • builder (Parliament::Builder) (defaults to: nil)

    the builder to use in order to build a response.

  • decorators (Module) (defaults to: nil)

    the decorator module to use in order to provide possible alias methods for any objects created by the builder.

See Also:

  • BaseRequest#initialize.

Since:

  • 0.7.5



22
23
24
25
26
27
# File 'lib/parliament/request/url_request.rb', line 22

def initialize(base_url: nil, headers: nil, builder: nil, decorators: nil)
  @endpoint_parts = []
  base_url ||= ENV['PARLIAMENT_BASE_URL']

  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *params, &block) ⇒ Parliament::Request::UrlRequest

Overrides ruby’s method_missing to allow creation of URLs through method calls.

Examples:

Adding a simple URL part

request = Parliament::Request::UrlRequest.new(base_url: 'http://example.com')

# url: http://example.com/people
request.people

Adding a simple URL part with parameters

request = Parliament::Request::UrlRequest.new(base_url: 'http://example.com')

# url: http://example.com/people/123456
request.people('123456')

Chaining URL parts and using hyphens

request = Parliament::Request::UrlRequest.new(base_url: 'http://example.com')

# url: http://example.com/people/123456/foo/bar/hello-world/7890
request.people('123456').foo.bar('hello-world', '7890')

Parameters:

  • method (Symbol)

    the ‘method’ (url part) we are processing.

  • params (Array<Object>)

    parameters passed to the specified method (url part).

  • block (Block)

    additional block (kept for compatibility with method_missing API).

Returns:

Since:

  • 0.7.5



54
55
56
57
58
59
60
61
62
63
# File 'lib/parliament/request/url_request.rb', line 54

def method_missing(method, *params, &block)
  @endpoint_parts << method.to_s

  @endpoint_parts << params
  @endpoint_parts = @endpoint_parts.flatten!

  block&.call

  self || super
end

Instance Attribute Details

#base_urlString (readonly)

the endpoint for our API which we will build our requests on. (expected: example.com - without the trailing slash).

Returns:

  • (String)

    the current value of base_url



9
10
11
# File 'lib/parliament/request/url_request.rb', line 9

def base_url
  @base_url
end

#headersHash (readonly)

the headers being sent in the request.

Returns:

  • (Hash)

    the current value of headers



9
10
11
# File 'lib/parliament/request/url_request.rb', line 9

def headers
  @headers
end

Instance Method Details

#query_urlObject

Since:

  • 0.7.5



72
73
74
75
76
77
78
79
# File 'lib/parliament/request/url_request.rb', line 72

def query_url
  uri_string = [@base_url, @endpoint_parts].join('/').gsub(' ', '%20')

  uri = URI.parse(uri_string)
  uri.query = URI.encode_www_form(@query_params) unless @query_params.empty?

  uri.to_s
end

#respond_to_missing?(_, _ = false) ⇒ Boolean

This class always responds to method calls, even those missing. Therefore, respond_to_missing? always returns true.

Returns:

  • (Boolean)

    always returns true.

Since:

  • 0.7.5



68
69
70
# File 'lib/parliament/request/url_request.rb', line 68

def respond_to_missing?(_, _ = false)
  true # responds to everything, always
end

#set_url_params(params) ⇒ Parliament::Request::UrlRequest

Returns self (this is to allow method chaining).

Returns:

Since:

  • 0.7.5



82
83
84
85
86
# File 'lib/parliament/request/url_request.rb', line 82

def set_url_params(params)
  @query_params = @query_params.merge(params)

  self
end