Class: Heroics::Client
- Inherits:
-
Object
- Object
- Heroics::Client
- Defined in:
- lib/heroics/client.rb
Overview
An HTTP client with methods mapped to API resources.
Instance Method Summary collapse
-
#initialize(resources, url) ⇒ Client
constructor
Instantiate an HTTP client.
-
#inspect ⇒ Object
(also: #to_s)
Get a simple human-readable representation of this client instance.
-
#method_missing(name) ⇒ Resource
Find a resource.
Constructor Details
#initialize(resources, url) ⇒ Client
Instantiate an HTTP client.
10 11 12 13 |
# File 'lib/heroics/client.rb', line 10 def initialize(resources, url) @resources = resources @url = url end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Resource
Find a resource.
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/heroics/client.rb', line 20 def method_missing(name) name = name.to_s resource = @resources[name] if resource.nil? # Try substituting underscores for dashes name = name.to_s.gsub('_', '-') resource = @resources[name] if resource.nil? raise NoMethodError.new("undefined method `#{name}' for #{to_s}") end end resource end |
Instance Method Details
#inspect ⇒ Object Also known as: to_s
Get a simple human-readable representation of this client instance.
35 36 37 38 39 40 41 |
# File 'lib/heroics/client.rb', line 35 def inspect url = URI.parse(@url) unless url.password.nil? url.password = 'REDACTED' end "#<Heroics::Client url=\"#{url.to_s}\">" end |