class Heroics::Client

An HTTP client with methods mapped to API resources.

Public Class Methods

new(resources, url) click to toggle source

Instantiate an HTTP client.

@param resources [Hash<String,Resource>] A hash that maps method names

to resources.

@param url [String] The URL used by this client.

# File lib/heroics/client.rb, line 10
def initialize(resources, url)
  # Transform resource keys via the ruby_name replacement semantics
  @resources = Hash[resources.map{ |k, v| [Heroics.ruby_name(k), v] }]
  @url = url
end

Public Instance Methods

inspect() click to toggle source

Get a simple human-readable representation of this client instance.

# File lib/heroics/client.rb, line 37
def inspect
  url = URI.parse(@url)
  unless url.password.nil?
    url.password = 'REDACTED'
  end
  "#<Heroics::Client url=\"#{url.to_s}\">"
end
Also aliased as: to_s
method_missing(name) click to toggle source

Find a resource.

@param name [String] The name of the resource to find. @raise [NoMethodError] Raised if the name doesn’t match a known resource. @return [Resource] The resource matching the name.

# File lib/heroics/client.rb, line 21
def method_missing(name)
  name = name.to_s
  resource = @resources[name]
  if resource.nil?
    # Find the name using the same ruby_name replacement semantics as when
    # we set up the @resources hash
    name = Heroics.ruby_name(name)
    resource = @resources[name]
    if resource.nil?
      raise NoMethodError.new("undefined method `#{name}' for #{to_s}")
    end
  end
  resource
end
to_s()
Alias for: inspect