Class: ClassLink::RequestBuilder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Interface
Defined in:
lib/classlink_client/request_builder.rb

Constant Summary collapse

TEST_ARRAY =
[].freeze
TEST_HASH =
{}.freeze

Constants included from Interface

Interface::METHOD_NAME_PROXY, Interface::RESOURCES

Instance Method Summary collapse

Methods included from RequestSigning

#request

Constructor Details

#initialize(client) ⇒ RequestBuilder

Returns a new instance of RequestBuilder.



16
17
18
19
20
21
22
# File 'lib/classlink_client/request_builder.rb', line 16

def initialize(client)
  @client = client

  @path_parts = []
  @query = {}
  @last_request_call = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/classlink_client/request_builder.rb', line 47

def method_missing(method_name, *args, &block)
  # prevent accidental method_missing recursion in development
  # which can make things hard to debug.
  super if @in_method_missing
  @in_method_missing = true

  if result.respond_to?(method_name)
    result.public_send(method_name, *args, &block)
  else
    super
  end
ensure
  @in_method_missing = false
end

Instance Method Details

#build_urlObject



40
41
42
43
44
45
# File 'lib/classlink_client/request_builder.rb', line 40

def build_url
  URI.join(base_url, *@path_parts).tap do |url|
    url.path = url.path[..-2]
    url.query = @query.to_query
  end
end

#chain(request, options) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/classlink_client/request_builder.rb', line 24

def chain(request, options)
  @last_request_call = request
  @query.merge!(options[:query]) if options[:query]
  @path_parts += [request, *options[:segments]].map{ |s| "#{s}/" }

  self
end

#executeObject



36
37
38
# File 'lib/classlink_client/request_builder.rb', line 36

def execute
  request(build_url)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
# File 'lib/classlink_client/request_builder.rb', line 62

def respond_to_missing?(method_name, include_private = false)
  if RESOURCES.include?(@last_request_call)
    TEST_ARRAY.respond_to?(method_name)
  elsif RESOURCES.map(&:singularize).include?(@last_request_call)
    TEST_HASH.respond_to?(method_name)
  else
    super
  end
end

#resultObject



32
33
34
# File 'lib/classlink_client/request_builder.rb', line 32

def result
  @result ||= execute
end