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

#api_endpoint, #api_endpoint=, #explicit_api_endpoint?, #github_auth, #repo, #repos, #user

Constructor Details

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

Returns a new instance of Session.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
# File 'lib/travis/client/session.rb', line 14

def initialize(options = Travis::Client::ORG_URI)
  @headers = {}
  @cache   = {}

  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/vnd.travis-ci.2+json, */*; q=0.01'
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



12
13
14
# File 'lib/travis/client/session.rb', line 12

def access_token
  @access_token
end

#connectionObject

Returns the value of attribute connection.



12
13
14
# File 'lib/travis/client/session.rb', line 12

def connection
  @connection
end

#headersObject

Returns the value of attribute headers.



12
13
14
# File 'lib/travis/client/session.rb', line 12

def headers
  @headers
end

Instance Method Details

#clear_cacheObject



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

def clear_cache
  reset_entities
  clear_find_cache
  self
end

#clear_cache!Object



128
129
130
131
132
# File 'lib/travis/client/session.rb', line 128

def clear_cache!
  reset_entities
  @cache.clear
  self
end

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

Raises:

  • (Travis::Error)


66
67
68
69
# File 'lib/travis/client/session.rb', line 66

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)


60
61
62
63
64
# File 'lib/travis/client/session.rb', line 60

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)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/travis/client/session.rb', line 71

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



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/travis/client/session.rb', line 93

def get(*args)
  result = {}
  get_raw(*args).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

#get_raw(*args) ⇒ Object



106
107
108
109
110
# File 'lib/travis/client/session.rb', line 106

def get_raw(*args)
  connection.get(*args).body
rescue Faraday::Error::ClientError => e
  handle_error(e)
end

#inspectObject



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

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

#post_raw(*args) ⇒ Object



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

def post_raw(*args)
  connection.post(*args).body
rescue Faraday::Error::ClientError => e
  handle_error(e)
end

#reload(entity) ⇒ Object



87
88
89
90
91
# File 'lib/travis/client/session.rb', line 87

def reload(entity)
  result = fetch_one(entity.class, entity.id)
  entity.update_attributes(result.attributes) if result.attributes != entity.attributes
  result
end

#sessionObject



134
135
136
# File 'lib/travis/client/session.rb', line 134

def session
  self
end

#uriObject



25
26
27
# File 'lib/travis/client/session.rb', line 25

def uri
  connection.url_prefix.to_s if connection
end

#uri=(uri) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/travis/client/session.rb', line 29

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