Skip to content

Instantly share code, notes, and snippets.

@jeremywrowe
Last active August 29, 2015 14:08
Show Gist options
  • Save jeremywrowe/59114ded6434335b71dd to your computer and use it in GitHub Desktop.
Save jeremywrowe/59114ded6434335b71dd to your computer and use it in GitHub Desktop.
class Base
DEFAULT_PROC = -> () {}
def initialize(api_key, connection_class = External::Service, builder = -> (klass, key) { klass.new(key) })
@api_key = api_key
@builder = builder
@connection_class = connection_class
end
def execute(&block)
begin
[:ok, yield(connection)]
rescue Service::Error => e
[:err, e.message]
end
end
private
def connection
@builder.call(@connection_class, @api_key)
end
end
class Finder < Base
def query(endpoint)
key, value = execute { |connection| connection.get(endpoint) }
case [key, value.class]
when [:ok, Hash] then value['response']
when [:err, String] then value
else raise 'something happened I dont understand'
end
end
end
# Finder.new('my key').query('/double/rainbow') #=> "OMG IN THE SKY"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment