Class: Travis::Client::Session
- Inherits:
-
Object
- Object
- Travis::Client::Session
- Includes:
- Methods
- Defined in:
- lib/travis/client/session.rb
Constant Summary collapse
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#agent_info ⇒ Object
Returns the value of attribute agent_info.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#debug_http ⇒ Object
Returns the value of attribute debug_http.
-
#faraday_adapter ⇒ Object
Returns the value of attribute faraday_adapter.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#instruments ⇒ Object
readonly
Returns the value of attribute instruments.
-
#ssl ⇒ Object
Returns the value of attribute ssl.
Instance Method Summary collapse
- #clear_cache ⇒ Object
- #clear_cache! ⇒ Object
- #config ⇒ Object
- #delete(*args) ⇒ Object
- #delete_raw(*args) ⇒ Object
- #find_many(entity, args = {}) ⇒ Object
- #find_one(entity, id = nil) ⇒ Object
- #find_one_or_many(entity, args = nil) ⇒ Object
- #get(*args) ⇒ Object
- #get_raw(*args) ⇒ Object
-
#initialize(options = Travis::Client::ORG_URI) ⇒ Session
constructor
A new instance of Session.
- #inspect ⇒ Object
- #instrument(&block) ⇒ Object
- #load(data) ⇒ Object
- #load_entity(key, value) ⇒ Object
- #patch(*args) ⇒ Object
- #patch_raw(*args) ⇒ Object
- #post(*args) ⇒ Object
- #post_raw(*args) ⇒ Object
- #preload(list) ⇒ Object
- #private_channels? ⇒ Boolean
- #put(*args) ⇒ Object
- #put_raw(*args) ⇒ Object
- #raw(verb, url, *args) ⇒ Object
- #reload(entity) ⇒ Object
- #reset(entity) ⇒ Object
- #session ⇒ Object
- #uri ⇒ Object
- #uri=(uri) ⇒ Object
Methods included from Methods
#account, #accounts, #api_endpoint, #api_endpoint=, #artifact, #broadcasts, #build, #cancel, #explicit_api_endpoint?, #github_auth, #hooks, #job, #lint, #listen, #repo, #repos, #restart, #user
Constructor Details
#initialize(options = Travis::Client::ORG_URI) ⇒ Session
Returns a new instance of Session.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/travis/client/session.rb', line 26 def initialize( = Travis::Client::ORG_URI) @headers = {} @cache = {} @instruments = [] @agent_info = [] @config = nil @faraday_adapter = defined?(Typhoeus) ? :typhoeus : :net_http @ssl = SSL_OPTIONS = { :uri => } unless .respond_to? :each_pair .each_pair { |key, value| public_send("#{key}=", value) } raise ArgumentError, "neither :uri nor :connection specified" unless connection headers['Accept'] = 'application/vnd.travis-ci.2+json' set_user_agent check_ssl end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
23 24 25 |
# File 'lib/travis/client/session.rb', line 23 def access_token @access_token end |
#agent_info ⇒ Object
Returns the value of attribute agent_info.
23 24 25 |
# File 'lib/travis/client/session.rb', line 23 def agent_info @agent_info end |
#connection ⇒ Object
Returns the value of attribute connection.
23 24 25 |
# File 'lib/travis/client/session.rb', line 23 def connection @connection end |
#debug_http ⇒ Object
Returns the value of attribute debug_http.
24 25 26 |
# File 'lib/travis/client/session.rb', line 24 def debug_http @debug_http end |
#faraday_adapter ⇒ Object
Returns the value of attribute faraday_adapter.
23 24 25 |
# File 'lib/travis/client/session.rb', line 23 def faraday_adapter @faraday_adapter end |
#headers ⇒ Object
Returns the value of attribute headers.
23 24 25 |
# File 'lib/travis/client/session.rb', line 23 def headers @headers end |
#instruments ⇒ Object (readonly)
Returns the value of attribute instruments.
23 24 25 |
# File 'lib/travis/client/session.rb', line 23 def instruments @instruments end |
#ssl ⇒ Object
Returns the value of attribute ssl.
23 24 25 |
# File 'lib/travis/client/session.rb', line 23 def ssl @ssl end |
Instance Method Details
#clear_cache ⇒ Object
240 241 242 243 244 |
# File 'lib/travis/client/session.rb', line 240 def clear_cache reset_entities clear_find_cache self end |
#clear_cache! ⇒ Object
246 247 248 249 250 |
# File 'lib/travis/client/session.rb', line 246 def clear_cache! reset_entities @cache.clear self end |
#config ⇒ Object
133 134 135 |
# File 'lib/travis/client/session.rb', line 133 def config @config ||= get_raw('/config')['config'] || {} end |
#delete(*args) ⇒ Object
169 170 171 |
# File 'lib/travis/client/session.rb', line 169 def delete(*args) load delete_raw(*args) end |
#delete_raw(*args) ⇒ Object
201 202 203 |
# File 'lib/travis/client/session.rb', line 201 def delete_raw(*args) raw(:delete, *args) end |
#find_many(entity, args = {}) ⇒ Object
100 101 102 103 |
# File 'lib/travis/client/session.rb', line 100 def find_many(entity, args = {}) raise Travis::Client::Error, "cannot fetch #{entity}" unless entity.respond_to?(:many) and entity.many cached(entity, :many, args) { fetch_many(entity, args) } end |
#find_one(entity, id = nil) ⇒ Object
94 95 96 97 98 |
# File 'lib/travis/client/session.rb', line 94 def find_one(entity, id = nil) raise Travis::Client::Error, "cannot fetch #{entity}" unless entity.respond_to?(:many) and entity.many return create_entity(entity, entity.id_field => id) if entity.id? id cached(entity, :by, id) { fetch_one(entity, id) } end |
#find_one_or_many(entity, args = nil) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/travis/client/session.rb', line 105 def find_one_or_many(entity, args = nil) raise Travis::Client::Error, "cannot fetch #{entity}" unless entity.respond_to?(:many) and entity.many cached(entity, :one_or_many, args) do path = "/#{entity.many}" path, args = "#{path}/#{args}", {} unless args.is_a? Hash result = get(path, args) one = result[entity.one] if result.include? entity.many Array(one) + Array(result[entity.many]) else one end end end |
#get(*args) ⇒ Object
165 166 167 |
# File 'lib/travis/client/session.rb', line 165 def get(*args) load get_raw(*args) end |
#get_raw(*args) ⇒ Object
185 186 187 |
# File 'lib/travis/client/session.rb', line 185 def get_raw(*args) raw(:get, *args) end |
#inspect ⇒ Object
236 237 238 |
# File 'lib/travis/client/session.rb', line 236 def inspect "#<#{self.class}: #{uri}>" end |
#instrument(&block) ⇒ Object
256 257 258 |
# File 'lib/travis/client/session.rb', line 256 def instrument(&block) instruments << block end |
#load(data) ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/travis/client/session.rb', line 137 def load(data) result = {} (data || {}).each_pair do |key, value| entity = load_entity(key, value) result[key] = entity if entity end result end |
#load_entity(key, value) ⇒ Object
146 147 148 149 150 151 152 153 154 |
# File 'lib/travis/client/session.rb', line 146 def load_entity(key, value) type = Entity.subclass_for(key) if value.respond_to? :to_ary value.to_ary.map { |e| create_entity(type, e) } else create_entity(type, value) end rescue IndexError end |
#patch(*args) ⇒ Object
173 174 175 |
# File 'lib/travis/client/session.rb', line 173 def patch(*args) load patch_raw(*args) end |
#patch_raw(*args) ⇒ Object
197 198 199 |
# File 'lib/travis/client/session.rb', line 197 def patch_raw(*args) raw(:patch, *args) end |
#post(*args) ⇒ Object
177 178 179 |
# File 'lib/travis/client/session.rb', line 177 def post(*args) load post_raw(*args) end |
#post_raw(*args) ⇒ Object
189 190 191 |
# File 'lib/travis/client/session.rb', line 189 def post_raw(*args) raw(:post, *args) end |
#preload(list) ⇒ Object
156 157 158 159 160 161 162 163 |
# File 'lib/travis/client/session.rb', line 156 def preload(list) list.group_by(&:class).each do |type, instances| next unless type.preloadable? ids = instances.map { |e| e.id unless e.complete? }.compact find_many(type, :ids => ids) if ids.any? end list end |
#private_channels? ⇒ Boolean
260 261 262 |
# File 'lib/travis/client/session.rb', line 260 def private_channels? !!config['pusher']['private'] end |
#put(*args) ⇒ Object
181 182 183 |
# File 'lib/travis/client/session.rb', line 181 def put(*args) load put_raw(*args) end |
#put_raw(*args) ⇒ Object
193 194 195 |
# File 'lib/travis/client/session.rb', line 193 def put_raw(*args) raw(:put, *args) end |
#raw(verb, url, *args) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/travis/client/session.rb', line 205 def raw(verb, url, *args) url = url.sub(/^\//, '') result = instrumented(verb.to_s.upcase, url, *args) do if url !~ /^https?:/ or url.start_with? api_endpoint connection.public_send(verb, url, *args) else Faraday.public_send(verb, url, *args) { |r| r.headers.delete("Authorization") } end end case result.status when 0 then raise Travis::Client::SSLError, 'SSL error: could not verify peer' when 200..299 then JSON.parse(result.body) rescue result.body when 301, 303 then raw(:get, result.headers['Location']) when 302, 307, 308 then raw(verb, result.headers['Location']) when 401 then raise Travis::Client::NotLoggedIn, 'not logged in' when 403 then body = JSON.parse(result.body) rescue {} if body["error_type"] == "migrated_repository" raise Travis::Client::RepositoryMigrated, body["error_message"] else raise Travis::Client::NotLoggedIn, 'invalid access token' end when 404 then raise Travis::Client::NotFound, result.body when 422 then raise Travis::Client::ValidationFailed, result.body when 400..499 then raise Travis::Client::Error, "%s: %p" % [result.status, result.body] when 500..599 then raise Travis::Client::Error, "server error (%s: %p)" % [result.status, result.body] else raise Travis::Client::Error, "unhandled status code #{result.status}" end end |
#reload(entity) ⇒ Object
126 127 128 129 130 131 |
# File 'lib/travis/client/session.rb', line 126 def reload(entity) reset(entity) result = fetch_one(entity.class, entity.id) entity.update_attributes(result.attributes) if result.attributes != entity.attributes result end |
#reset(entity) ⇒ Object
121 122 123 124 |
# File 'lib/travis/client/session.rb', line 121 def reset(entity) entity.attributes.clear entity end |
#session ⇒ Object
252 253 254 |
# File 'lib/travis/client/session.rb', line 252 def session self end |
#uri ⇒ Object
44 45 46 |
# File 'lib/travis/client/session.rb', line 44 def uri connection.url_prefix.to_s if connection end |
#uri=(uri) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/travis/client/session.rb', line 58 def uri=(uri) clear_cache! self.connection = Faraday.new(:url => uri, :ssl => ssl) do |faraday| faraday.request :url_encoded faraday.response :logger if debug_http faraday.adapter(*faraday_adapter) end end |