Class: URL::Service::EndpointBuilder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/url/endpoint_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, endpoint, params = {}, &blk) ⇒ EndpointBuilder

Create a method missing enironment to build out all the methods on the endpoint object



9
10
11
12
13
14
15
16
# File 'lib/url/endpoint_builder.rb', line 9

def initialize base_url, endpoint, params={}, &blk
  @url = base_url.dup
  @url.add_to_path endpoint.to_s
  @url.params.merge!(params)
  @_endpoint = Endpoint.new(@url)
  
  instance_eval &blk if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &blk) ⇒ Object

Build any additional methods sent



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/url/endpoint_builder.rb', line 27

def method_missing *args, &blk
  if m = caller.first.match(/^(#{__FILE__}:\d+):in `method_missing'$/) # protect against a typo within this function creating a stack overflow
    raise "Method missing calling itself with #{args.first} in #{m[1]}"
  end
  is_built_in = false
  # If you define a method named get,post,create,etc don't require the method type
  if Endpoint::BUILT_IN_METHODS.include?(args.first) && !Endpoint::BUILT_IN_METHODS.include?(args[1])
    name = args.shift

    if Endpoint::BUILT_IN_MAP.has_key?(name.to_sym)
      method = name.to_sym
      is_built_in = true
    else
      method = Endpoint::BULT_IN_MAP.find do |meth,aliases|
        aliases.include?(name)
      end

      method = method[0] if method
    end
  else
    name = args.shift
    method = args.shift if args.first.is_a?(Symbol)
  end
  name = name.to_sym

  method ||= :get
  
  options = args.shift||{}
  options[:requires] ||= []
  options[:requires] = [options[:requires]] unless options[:requires].is_a?(Array)
  options[:into] ||= blk if block_given?
  @_endpoint.method_inflate_into[name] = options[:into]

  @_endpoint.class_eval "    def \#{name} force_params={}, args={}\n      params = \#{(options[:default]||{}).inspect}.merge(force_params)\n      \#{(options[:requires]||[]).inspect}.each do |req|\n        raise RequiredParameter, \"\#{name} endpoint requires the \"<<req<<\" paramerter\" unless params.include?(req.to_sym) || params.include?(req.to_s)\n      end\n\n      if \#{is_built_in.inspect}\n        super(params,args)\n      else\n        transform_response(\#{method}(params,args),method_inflate_into[\#{name.inspect}])\n      end\n    end\n  RUBY\nend\n", __FILE__, __LINE__

Instance Attribute Details

#_endpointObject (readonly)

Returns the value of attribute _endpoint.



4
5
6
# File 'lib/url/endpoint_builder.rb', line 4

def _endpoint
  @_endpoint
end

Instance Method Details

#returns(arg = nil, &blk) ⇒ Object



21
22
23
24
# File 'lib/url/endpoint_builder.rb', line 21

def returns arg=nil, &blk
  blk = arg unless block_given?
  @_endpoint.inflate_into = blk
end