Last active
August 29, 2015 14:08
-
-
Save jeremywrowe/59114ded6434335b71dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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