lib/full_circle/connection.rb in full_circle-0.1.0 vs lib/full_circle/connection.rb in full_circle-0.2.0

- old
+ new

@@ -1,18 +1,50 @@ +require "net/http" module FullCircle class Connection - attr_reader :domain + attr_reader :domain, :cache - def initialize(domain) + # domain - domain of directory (ex. 360durango.com) + # args + # - cache - Caching object to use + def initialize(domain, args={}) @domain = domain + @cache = args.fetch(:cache){NullCache.new} end def base_uri "http://api.#{domain}/1.0/" end def call_api_method(method_name, query_params={}) - HTTParty.get(method_name,base_uri: base_uri, query: query_params) + uri_str = uri_string(method_name, query_params) + uri = URI(uri_str) + + body = cache.fetch(uri_str) do + response_text = Net::HTTP.get(uri) + cache.store(uri_str, response_text) + end + + Response.new body + + end + + NullCache = Class.new do + def fetch(key) + yield + end + + def store(key, value) + value + end + end + + private + Response = Struct.new(:body) + + + def uri_string(method_name, query_params) + "#{base_uri}#{method_name}?#{query_params.to_query}" end end end