lib/travis/client/session.rb in travis-1.1.3 vs lib/travis/client/session.rb in travis-1.2.0
- old
+ new
@@ -1,24 +1,24 @@
require 'travis/client'
require 'faraday'
-require 'faraday/adapter/net_http_persistent'
require 'faraday_middleware'
require 'json'
module Travis
module Client
class Session
SSL_OPTIONS = { :ca_file => File.expand_path("../../cacert.pem", __FILE__) }
include Methods
- attr_reader :connection, :headers, :access_token, :instruments
+ attr_reader :connection, :headers, :access_token, :instruments, :faraday_adapter
def initialize(options = Travis::Client::ORG_URI)
- @headers = {}
- @cache = {}
- @instruments = []
- @config = nil
+ @headers = {}
+ @cache = {}
+ @instruments = []
+ @config = nil
+ @faraday_adapter = :net_http_persistent
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
@@ -38,10 +38,15 @@
faraday.response :raise_error
faraday.adapter(*faraday_adapter)
end
end
+ def faraday_adapter=(adapter)
+ @faraday_adapter = adapter
+ self.uri &&= uri
+ end
+
def access_token=(token)
clear_cache!
@access_token = token
headers['Authorization'] = "token #{token}"
headers.delete('Authorization') unless token
@@ -102,23 +107,27 @@
def config
@config ||= get_raw('/config')['config']
end
- def get(*args)
+ def load(data)
result = {}
- get_raw(*args).each_pair do |key, value|
+ (data || {}).each_pair do |key, value|
type = Entity.subclass_for(key)
if value.respond_to? :to_ary
result[key] = value.to_ary.map { |e| create_entity(type, e) }
else
result[key] = create_entity(type, value)
end
end
result
end
+ def get(*args)
+ load get_raw(*args)
+ end
+
def get_raw(*args)
raw(:get, *args)
end
def post_raw(*args)
@@ -169,24 +178,20 @@
lift.call
result
end
def create_entity(type, data)
- id = Integer(data.fetch('id'))
+ id = type.cast_id(data.fetch('id'))
entity = cached(type, :id, id) { type.new(self, id) }
entity.update_attributes(data)
entity
end
def handle_error(e)
message = e.response[:body].to_str rescue e.message
klass = Travis::Client::NotFound if e.is_a? Faraday::Error::ResourceNotFound
klass ||= Travis::Client::Error
raise klass, message, e.backtrace
- end
-
- def faraday_adapter
- :net_http_persistent #Faraday.default_adapter
end
def reset_entities
subcaches do |subcache|
subcache[:id].each_value { |e| e.attributes.clear } if subcache.include? :id