Class: WebApi

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

Overview

This Gem

Constant Summary collapse

VERSION =
'1.0.210927'
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.



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

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



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

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
  # user passed data as first argument?
  extension, data = '', extension unless extension.is_a? String
  @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



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

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



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

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