lib/travis/client/session.rb in travis-1.0.3 vs lib/travis/client/session.rb in travis-1.1.0

- old
+ new

@@ -7,15 +7,17 @@ module Travis module Client class Session SSL_OPTIONS = { :ca_file => File.expand_path("../../cacert.pem", __FILE__) } include Methods - attr_reader :connection, :headers, :access_token + attr_reader :connection, :headers, :access_token, :instruments def initialize(options = Travis::Client::ORG_URI) - @headers = {} - @cache = {} + @headers = {} + @cache = {} + @instruments = [] + @config = nil options = { :uri => options } unless options.respond_to? :each_pair options.each_pair { |key, value| public_send("#{key}=", value) } raise ArgumentError, "neither :uri nor :connection specified" unless connection @@ -27,11 +29,11 @@ end def uri=(uri) clear_cache! self.connection = Faraday.new(:url => uri, :ssl => SSL_OPTIONS) do |faraday| - faraday.request :json + faraday.request :url_encoded faraday.response :json faraday.response :follow_redirects faraday.response :raise_error faraday.adapter(*faraday_adapter) end @@ -45,10 +47,11 @@ end def connection=(connection) clear_cache! connection.headers.merge! headers + @config = nil @connection = connection @headers = connection.headers end def headers=(headers) @@ -82,16 +85,26 @@ one end end end + def reset(entity) + entity.attributes.clear + entity + end + def reload(entity) + reset(entity) result = fetch_one(entity.class, entity.id) entity.update_attributes(result.attributes) if result.attributes != entity.attributes result end + def config + @config ||= get_raw('/config')['config'] + end + def get(*args) result = {} get_raw(*args).each_pair do |key, value| type = Entity.subclass_for(key) if value.respond_to? :to_ary @@ -102,17 +115,23 @@ end result end def get_raw(*args) - connection.get(*args).body - rescue Faraday::Error::ClientError => e - handle_error(e) + raw(:get, *args) end def post_raw(*args) - connection.post(*args).body + raw(:post, *args) + end + + def put_raw(*args) + raw(:put, *args) + end + + def raw(verb, *args) + instrumented(verb.to_s.upcase, *args) { connection.public_send(verb, *args).body } rescue Faraday::Error::ClientError => e handle_error(e) end def inspect @@ -133,10 +152,23 @@ def session self end + def instrument(&block) + instruments << block + end + private + + def instrumented(name, *args) + name = [name, *args.map(&:inspect)].join(" ") if args.any? + result = nil + chain = instruments + [proc { |n,l| result = yield }] + lift = proc { chain.shift.call(name, lift) } + lift.call + result + end def create_entity(type, data) id = Integer(data.fetch('id')) entity = cached(type, :id, id) { type.new(self, id) } entity.update_attributes(data)