Class: Travis::Client::Session
- Inherits:
-
Object
- Object
- Travis::Client::Session
show all
- Includes:
- Methods
- Defined in:
- lib/travis/client/session.rb
Constant Summary
collapse
- PRIMITIVE =
[nil, false, true]
- SSL_OPTIONS =
{ :ca_file => Tools::Assets['cacert.pem'] }
Instance Attribute Summary collapse
Instance Method Summary
collapse
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.
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/travis/client/session.rb', line 25
def initialize(options = Travis::Client::ORG_URI)
@headers = {}
@cache = {}
@instruments = []
@agent_info = []
@config = nil
@faraday_adapter = defined?(Typhoeus) ? :typhoeus : :net_http
@ssl = SSL_OPTIONS
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
['Accept'] = 'application/vnd.travis-ci.2+json'
set_user_agent
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
|
#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
|
Returns the value of attribute headers.
23
24
25
|
# File 'lib/travis/client/session.rb', line 23
def
@headers
end
|
#instruments ⇒ Object
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
229
230
231
232
233
|
# File 'lib/travis/client/session.rb', line 229
def clear_cache
reset_entities
clear_find_cache
self
end
|
#clear_cache! ⇒ Object
235
236
237
238
239
|
# File 'lib/travis/client/session.rb', line 235
def clear_cache!
reset_entities
@cache.clear
self
end
|
#config ⇒ Object
130
131
132
|
# File 'lib/travis/client/session.rb', line 130
def config
@config ||= get_raw('/config')['config'] || {}
end
|
#delete(*args) ⇒ Object
166
167
168
|
# File 'lib/travis/client/session.rb', line 166
def delete(*args)
load delete_raw(*args)
end
|
#delete_raw(*args) ⇒ Object
198
199
200
|
# File 'lib/travis/client/session.rb', line 198
def delete_raw(*args)
raw(:delete, *args)
end
|
#find_many(entity, args = {}) ⇒ Object
97
98
99
100
|
# File 'lib/travis/client/session.rb', line 97
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
91
92
93
94
95
|
# File 'lib/travis/client/session.rb', line 91
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/travis/client/session.rb', line 102
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
162
163
164
|
# File 'lib/travis/client/session.rb', line 162
def get(*args)
load get_raw(*args)
end
|
#get_raw(*args) ⇒ Object
182
183
184
|
# File 'lib/travis/client/session.rb', line 182
def get_raw(*args)
raw(:get, *args)
end
|
#inspect ⇒ Object
225
226
227
|
# File 'lib/travis/client/session.rb', line 225
def inspect
"#<#{self.class}: #{uri}>"
end
|
#instrument(&block) ⇒ Object
245
246
247
|
# File 'lib/travis/client/session.rb', line 245
def instrument(&block)
instruments << block
end
|
#load(data) ⇒ Object
134
135
136
137
138
139
140
141
|
# File 'lib/travis/client/session.rb', line 134
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
143
144
145
146
147
148
149
150
151
|
# File 'lib/travis/client/session.rb', line 143
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
170
171
172
|
# File 'lib/travis/client/session.rb', line 170
def patch(*args)
load patch_raw(*args)
end
|
#patch_raw(*args) ⇒ Object
194
195
196
|
# File 'lib/travis/client/session.rb', line 194
def patch_raw(*args)
raw(:patch, *args)
end
|
#post(*args) ⇒ Object
174
175
176
|
# File 'lib/travis/client/session.rb', line 174
def post(*args)
load post_raw(*args)
end
|
#post_raw(*args) ⇒ Object
186
187
188
|
# File 'lib/travis/client/session.rb', line 186
def post_raw(*args)
raw(:post, *args)
end
|
#preload(list) ⇒ Object
153
154
155
156
157
158
159
160
|
# File 'lib/travis/client/session.rb', line 153
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
249
250
251
|
# File 'lib/travis/client/session.rb', line 249
def private_channels?
access_token and user.channels != ['common']
end
|
#put(*args) ⇒ Object
178
179
180
|
# File 'lib/travis/client/session.rb', line 178
def put(*args)
load put_raw(*args)
end
|
#put_raw(*args) ⇒ Object
190
191
192
|
# File 'lib/travis/client/session.rb', line 190
def put_raw(*args)
raw(:put, *args)
end
|
#raw(verb, url, *args) ⇒ Object
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
# File 'lib/travis/client/session.rb', line 202
def raw(verb, url, *args)
url = url.sub(/^\//, '')
result = instrumented(verb.to_s.upcase, url, *args) do
connection.public_send(verb, url, *args) do |request|
next if request.path !~ /^https?:/ or request.path.start_with? api_endpoint
request..delete("Authorization")
end
end
case result.status
when 0 then raise Travis::Client::Error, 'SSL error: could not verify peer'
when 200..299 then JSON.parse(result.body) rescue result.body
when 301, 303 then raw(:get, result.['Location'])
when 302, 307, 308 then raw(verb, result.['Location'])
when 401 then raise Travis::Client::NotLoggedIn, 'not logged in'
when 403 then raise Travis::Client::NotLoggedIn, 'invalid access token'
when 404 then raise Travis::Client::NotFound, result.body
when 400..499 then raise Travis::Client::Error, result.status
when 500..599 then raise Travis::Client::Error, "server error (#{result.status})"
else raise Travis::Client::Error, "unhandled status code #{result.status}"
end
end
|
#reload(entity) ⇒ Object
123
124
125
126
127
128
|
# File 'lib/travis/client/session.rb', line 123
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
118
119
120
121
|
# File 'lib/travis/client/session.rb', line 118
def reset(entity)
entity.attributes.clear
entity
end
|
#session ⇒ Object
241
242
243
|
# File 'lib/travis/client/session.rb', line 241
def session
self
end
|
#uri ⇒ Object
42
43
44
|
# File 'lib/travis/client/session.rb', line 42
def uri
connection.url_prefix.to_s if connection
end
|
#uri=(uri) ⇒ Object
56
57
58
59
60
61
62
|
# File 'lib/travis/client/session.rb', line 56
def uri=(uri)
clear_cache!
self.connection = Faraday.new(:url => uri, :ssl => ssl) do |faraday|
faraday.request :url_encoded
faraday.adapter(*faraday_adapter)
end
end
|