Class: Cage::Console
- Inherits:
-
Object
show all
- Defined in:
- lib/cage/console.rb
Constant Summary
collapse
- CONNECTION_VARIABLES =
[:scheme, :domain, :prefix, :headers]
- HTTP_METHOD_REGEX =
/^(?:get|post|put|delete)$/i
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Console.
9
10
11
12
13
14
15
|
# File 'lib/cage/console.rb', line 9
def initialize
@scheme = "http"
@domain = "rubygems.org"
@prefix = "api/v1/gems/"
@headers = {}
reinitialize_connection
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
22
23
24
25
26
27
28
|
# File 'lib/cage/console.rb', line 22
def method_missing sym, *args, &block
if sym =~ HTTP_METHOD_REGEX
http sym, *args, &block
else
super
end
end
|
Class Method Details
.start! ⇒ Object
50
51
52
|
# File 'lib/cage/console.rb', line 50
def self.start!
new.pry
end
|
Instance Method Details
#http(method, *args, &block) ⇒ Object
38
39
40
|
# File 'lib/cage/console.rb', line 38
def http method, *args, &block
@last_response = Cage::Response.new connection.send method, *args, &block
end
|
#reinitialize_connection ⇒ Object
17
18
19
20
|
# File 'lib/cage/console.rb', line 17
def reinitialize_connection
@connection = Faraday::Connection.new "#{scheme}://#{domain}/#{prefix}",
:headers => @headers
end
|
#respond_to?(method) ⇒ Boolean
30
31
32
33
34
35
36
|
# File 'lib/cage/console.rb', line 30
def respond_to? method
if method =~ HTTP_METHOD_REGEX
true
else
super
end
end
|
#set(convar, value) ⇒ Object
42
43
44
45
46
47
48
|
# File 'lib/cage/console.rb', line 42
def set convar, value
raise ArgumentError, "#{convar} isn't a connection variable" unless
CONNECTION_VARIABLES.include? convar
instance_variable_set :"@#{convar}", value
reinitialize_connection
value
end
|