Class: Travis::Client::Session

Inherits:
Object
  • Object
show all
Includes:
Methods
Defined in:
lib/travis/client/session.rb

Constant Summary collapse

SSL_OPTIONS =
{ :ca_file => File.expand_path("../../cacert.pem", __FILE__) }

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Methods

#accounts, #api_endpoint, #api_endpoint=, #artifact, #broadcasts, #build, #cancel, #explicit_api_endpoint?, #github_auth, #job, #listen, #repo, #repos, #restart, #user, #worker, #workers

Constructor Details

#initialize(options = Travis::Client::ORG_URI) ⇒ Session

Returns a new instance of Session.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/travis/client/session.rb', line 22

def initialize(options = Travis::Client::ORG_URI)
  @headers         = {}
  @cache           = {}
  @instruments     = []
  @agent_info      = []
  @config          = nil
  @faraday_adapter = defined?(Typhoeus) ? :typhoeus : :net_http

  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
  headers['Accept'] = 'application/json; version=2'
  set_user_agent
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



20
21
22
# File 'lib/travis/client/session.rb', line 20

def access_token
  @access_token
end

#agent_infoObject

Returns the value of attribute agent_info.



20
21
22
# File 'lib/travis/client/session.rb', line 20

def agent_info
  @agent_info
end

#connectionObject

Returns the value of attribute connection.



20
21
22
# File 'lib/travis/client/session.rb', line 20

def connection
  @connection
end

#faraday_adapterObject

Returns the value of attribute faraday_adapter.



20
21
22
# File 'lib/travis/client/session.rb', line 20

def faraday_adapter
  @faraday_adapter
end

#headersObject

Returns the value of attribute headers.



20
21
22
# File 'lib/travis/client/session.rb', line 20

def headers
  @headers
end

#instrumentsObject (readonly)

Returns the value of attribute instruments.



20
21
22
# File 'lib/travis/client/session.rb', line 20

def instruments
  @instruments
end

Instance Method Details

#clear_cacheObject



167
168
169
170
171
# File 'lib/travis/client/session.rb', line 167

def clear_cache
  reset_entities
  clear_find_cache
  self
end

#clear_cache!Object



173
174
175
176
177
# File 'lib/travis/client/session.rb', line 173

def clear_cache!
  reset_entities
  @cache.clear
  self
end

#configObject



124
125
126
# File 'lib/travis/client/session.rb', line 124

def config
  @config ||= get_raw('/config')['config'] || {}
end

#find_many(entity, args = {}) ⇒ Object

Raises:

  • (Travis::Error)


91
92
93
94
# File 'lib/travis/client/session.rb', line 91

def find_many(entity, args = {})
  raise Travis::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

Raises:

  • (Travis::Error)


85
86
87
88
89
# File 'lib/travis/client/session.rb', line 85

def find_one(entity, id = nil)
  raise Travis::Error, "cannot fetch #{entity}" unless entity.respond_to?(:many) and entity.many
  return create_entity(entity, "id" => id) if id.is_a? Integer
  cached(entity, :by, id) { fetch_one(entity, id) }
end

#find_one_or_many(entity, args = nil) ⇒ Object

Raises:

  • (Travis::Error)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/travis/client/session.rb', line 96

def find_one_or_many(entity, args = nil)
  raise Travis::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



141
142
143
# File 'lib/travis/client/session.rb', line 141

def get(*args)
  load get_raw(*args)
end

#get_raw(*args) ⇒ Object



145
146
147
# File 'lib/travis/client/session.rb', line 145

def get_raw(*args)
  raw(:get, *args)
end

#inspectObject



163
164
165
# File 'lib/travis/client/session.rb', line 163

def inspect
  "#<#{self.class}: #{uri}>"
end

#instrument(&block) ⇒ Object



183
184
185
# File 'lib/travis/client/session.rb', line 183

def instrument(&block)
  instruments << block
end

#load(data) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/travis/client/session.rb', line 128

def load(data)
  result = {}
  (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

#post_raw(*args) ⇒ Object



149
150
151
# File 'lib/travis/client/session.rb', line 149

def post_raw(*args)
  raw(:post, *args)
end

#private_channels?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/travis/client/session.rb', line 187

def private_channels?
  access_token and user.channels != ['common']
end

#put_raw(*args) ⇒ Object



153
154
155
# File 'lib/travis/client/session.rb', line 153

def put_raw(*args)
  raw(:put, *args)
end

#raw(verb, *args) ⇒ Object



157
158
159
160
161
# File 'lib/travis/client/session.rb', line 157

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

#reload(entity) ⇒ Object



117
118
119
120
121
122
# File 'lib/travis/client/session.rb', line 117

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



112
113
114
115
# File 'lib/travis/client/session.rb', line 112

def reset(entity)
  entity.attributes.clear
  entity
end

#sessionObject



179
180
181
# File 'lib/travis/client/session.rb', line 179

def session
  self
end

#uriObject



38
39
40
# File 'lib/travis/client/session.rb', line 38

def uri
  connection.url_prefix.to_s if connection
end

#uri=(uri) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/travis/client/session.rb', line 47

def uri=(uri)
  clear_cache!
  self.connection = Faraday.new(:url => uri, :ssl => SSL_OPTIONS) do |faraday|
    faraday.request  :url_encoded
    faraday.response :json
    faraday.response :follow_redirects
    faraday.response :raise_error
    faraday.adapter(*faraday_adapter)
  end
end