Class: NOMS::HttpClient

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

Direct Known Subclasses

NCC::Client, CMDB, Real, RestMock, Nagui

Defined Under Namespace

Classes: Real, RestMock

Constant Summary collapse

@@mocked =
false

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt) ⇒ HttpClient

Returns a new instance of HttpClient.



92
93
94
95
96
# File 'lib/noms/httpclient.rb', line 92

def initialize(opt)
    @opt = opt
    @delegate = (@@mocked ? self.class.mockery.new(self) :
        NOMS::HttpClient::Real.new(self))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



172
173
174
# File 'lib/noms/httpclient.rb', line 172

def method_missing(meth, *args, &block)
    @delegate.send(meth, *args, &block)
end

Class Method Details

.mock!(datafile = nil) ⇒ Object



83
84
85
86
# File 'lib/noms/httpclient.rb', line 83

def self.mock!(datafile=nil)
    @@mocked = true
    @@mockdata = datafile
end

.mockeryObject



88
89
90
# File 'lib/noms/httpclient.rb', line 88

def self.mockery
    NOMS::HttpClient::RestMock
end

Instance Method Details

#allow_partial_updatesObject

Used mostly for mocking behavior



130
131
132
133
# File 'lib/noms/httpclient.rb', line 130

def allow_partial_updates
    # Replace during PUT
    false
end

#allow_put_to_createObject



135
136
137
# File 'lib/noms/httpclient.rb', line 135

def allow_put_to_create
    true
end

#config_keyObject



106
107
108
# File 'lib/noms/httpclient.rb', line 106

def config_key
    'httpclient'
end

#dbg(msg) ⇒ Object



147
148
149
150
151
# File 'lib/noms/httpclient.rb', line 147

def dbg(msg)
    if @opt.has_key? 'debug' and @opt['debug'] > 1
        puts "DBG(#{self.class}): #{msg}"
    end
end

#default_content_typeObject



139
140
141
# File 'lib/noms/httpclient.rb', line 139

def default_content_type
    'application/json'
end

#handle_mock(method, url, opt) ⇒ Object



102
103
104
# File 'lib/noms/httpclient.rb', line 102

def handle_mock(method, url, opt)
    false
end

#ignore_content_typeObject



143
144
145
# File 'lib/noms/httpclient.rb', line 143

def ignore_content_type
    false
end

#ltrim(s, c = '/') ⇒ Object



164
165
166
# File 'lib/noms/httpclient.rb', line 164

def ltrim(s, c='/')
    trim(s, c, :left)
end

#myconfig(key = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/noms/httpclient.rb', line 110

def myconfig(key=nil)
    unless @opt.has_key? config_key and @opt[config_key]
        raise NOMS::Error::Config, "Configuration provided to #{self.class} doesn't have a '#{config_key}' key"
    end

    cfg = @opt[config_key]

    if key
        unless cfg.has_key? key
            msg = "#{config_key} configuration doesn't have a '#{key}' property"
            msg += " (#{@opt[config_key].inspect})"
            raise NOMS::Error::Config, msg
        end
        cfg[key]
    else
        cfg
    end
end

#optObject



98
99
100
# File 'lib/noms/httpclient.rb', line 98

def opt
    @opt
end

#rtrim(s, c = '/') ⇒ Object



168
169
170
# File 'lib/noms/httpclient.rb', line 168

def rtrim(s, c='/')
    trim(s, c, :right)
end

#trim(s, c = '/', dir = :both) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/noms/httpclient.rb', line 153

def trim(s, c='/', dir=:both)
    case dir
    when :both
        trim(trim(s, c, :right), c, :left)
    when :right
        s.sub(Regexp.new(c + '+/'), '')
    when :left
        s.sub(Regexp.new('^' + c + '+'), '')
    end
end