Class: NbApiClient::UrlBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/nb_api_client/url_builder.rb

Overview

Builds a fully-qualified NationBuilder API URL for a given nation and path, appending the nation's OAuth access token as a query parameter (unless the path is an OAuth endpoint, which is unauthenticated).

Instance Method Summary collapse

Constructor Details

#initialize(nation, path) ⇒ UrlBuilder

Returns a new instance of UrlBuilder.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
# File 'lib/nb_api_client/url_builder.rb', line 8

def initialize(nation, path)
  @nation = nation
  @path = path

  raise ArgumentError, "Path cannot be empty" if @path.blank?
end

Instance Method Details

#urlObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/nb_api_client/url_builder.rb', line 15

def url
  url_string = @path.start_with?("http") ? @path : @nation.url + @path

  uri = URI.parse(url_string)
  new_query_ar = if @path.start_with?("/oauth/")
    URI.decode_www_form(String(uri.query))
  else
    URI.decode_www_form(String(uri.query)) << ["access_token", @nation.token]
  end
  uri.query = URI.encode_www_form(new_query_ar)

  uri.to_s
end