Class: WEB_API::WebApi

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

Constant Summary collapse

DUMPER =
(defined? JSON)? JSON.method(:dump)  : :none
PARSER =
Hash.new :none

Instance Method Summary collapse

Constructor Details

#initialize(base = '', type: :get, data: {}, header: {}, dumper: DUMPER, parser: PARSER, &block) ⇒ WebApi

Returns a new instance of WebApi.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/web_api/web_api.rb', line 10

def initialize(base =  '',
               type:   :get,
               data:   {},
               header: {},
               dumper: DUMPER,
               parser: PARSER,
               &block)
  @base, @type, @data, @header, @dumper, @parser, @block =
   base,  type,  data,  header,  dumper,  parser,  block
  @strict = false
  @webmethods = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, extension = '', type: nil, data: {}, header: {}, dumper: nil, parser: nil, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/web_api/web_api.rb', line 43

def method_missing(symbol , extension =  '',
                            type:   nil,
                            data:   {},
                            header: {},
                            dumper: nil,
                            parser: nil,
                            &block)
  unless @webmethods.has_key? symbol
    super if @strict
    add symbol
  end
  extension, data = '', extension unless extension.is_a? String # user passed data as first argument
  @webmethods[symbol].run(extension, type, data, header, dumper, parser, &block)
end

Instance Method Details

#add(method, target = method.to_s, type: @type, data: {}, header: {}, dumper: @dumper, parser: @parser, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/web_api/web_api.rb', line 27

def add(method, target =  method.to_s,
                type:   @type,
                data:   {},
                header: {},
                dumper: @dumper,
                parser: @parser,
                &block)
  @webmethods[method] = WebApiMethod.new(@base+target,
                                         type,
                                         @data.merge(data),
                                         @header.merge(header),
                                         dumper,
                                         parser,
                                         &(block || @block))
end

#strict!(t = true) ⇒ Object



23
24
25
# File 'lib/web_api/web_api.rb', line 23

def strict!(t = true)
  @strict = t
end