Class: Firebrigade::API
- Inherits:
-
RCRest
- Object
- RCRest
- Firebrigade::API
- Defined in:
- lib/firebrigade/api.rb
Overview
Firebrigade::API is an API for submitting build information to firebrigade.seattlerb.org/
All #get_ and #add_ methods return an instance of the Object fetched or created, so get_target will return a Target object.
All #add_ methods will return an instance of a pre-existing Object if one already exists.
Defined Under Namespace
Classes: Build, Error, InvalidLogin, NotFound, Owner, Project, Target, Version, WrongAPIVersion
Constant Summary collapse
- VERSION =
The version of Firebrigade::API you are using
'1.0.0'
- API_VERSION =
Supported Firebrigade API version.
'1.0.0'
Instance Method Summary collapse
-
#add_build(version_id, target_id, successful, duration, log) ⇒ Object
Adds a Build with
version_id
andtarget_id
, reporting thesuccessful
status, theduration
taken, and thelog
. -
#add_owner(name) ⇒ Object
Adds an Owner with
name
. -
#add_project(name, owner_id) ⇒ Object
Adds a Project with
name
owned byowner_id
. -
#add_target(version, release_date, platform) ⇒ Object
Adds a Target with a Ruby
version
, Rubyrelease_date
and Rubyplatform
. -
#add_version(name, project_id) ⇒ Object
Adds Version
name
toproject_id
. -
#check_error(xml) ⇒ Object
Checks for errors in
xml
and raises an appropriate Exception. -
#get_build(version_id, target_id) ⇒ Object
Retrieves a Build for
version_id
andtarget_id
. -
#get_owner(name) ⇒ Object
Retrieves an Owner matching
name
. -
#get_project(name, owner_id) ⇒ Object
Retrieves a Project matching
name
andowner_id
. -
#get_target(version, release_date, platform) ⇒ Object
Retrieves a Target matching
version
,release_date
andplatform
. -
#get_version(name, project_id) ⇒ Object
Retrieves a Version matching
name
nadproject_id
. -
#initialize(host, username, password) ⇒ API
constructor
Creates a new Firebrigade::API that will connect to
host
withusername
andpassword
. -
#make_multipart(params) ⇒ Object
Makes a multipart POST from
params
. -
#make_url(method, params) ⇒ Object
Makes a URL for
method
andparams
. -
#parse_response(xml) ⇒ Object
Creates an Object from
xml
. -
#set_hash(params) ⇒ Object
Sets the request hash value for
params
.
Constructor Details
#initialize(host, username, password) ⇒ API
Creates a new Firebrigade::API that will connect to host
with username
and password
.
81 82 83 84 85 |
# File 'lib/firebrigade/api.rb', line 81 def initialize(host, username, password) @username = username @password = password @url = URI.parse "http://#{host}/api/REST/" end |
Instance Method Details
#add_build(version_id, target_id, successful, duration, log) ⇒ Object
Adds a Build with version_id
and target_id
, reporting the successful
status, the duration
taken, and the log
.
91 92 93 94 95 96 |
# File 'lib/firebrigade/api.rb', line 91 def add_build(version_id, target_id, successful, duration, log) post_multipart :add_build, :version_id => version_id, :target_id => target_id, :successful => successful, :duration => duration, :log => log end |
#add_owner(name) ⇒ Object
Adds an Owner with name
.
101 102 103 |
# File 'lib/firebrigade/api.rb', line 101 def add_owner(name) post :add_owner, :name => name end |
#add_project(name, owner_id) ⇒ Object
Adds a Project with name
owned by owner_id
.
108 109 110 |
# File 'lib/firebrigade/api.rb', line 108 def add_project(name, owner_id) post :add_project, :name => name, :owner_id => owner_id end |
#add_target(version, release_date, platform) ⇒ Object
Adds a Target with a Ruby version
, Ruby release_date
and Ruby platform
.
116 117 118 119 |
# File 'lib/firebrigade/api.rb', line 116 def add_target(version, release_date, platform) post :add_target, :version => version, :release_date => release_date, :platform => platform, :api_version => API_VERSION end |
#add_version(name, project_id) ⇒ Object
Adds Version name
to project_id
.
124 125 126 |
# File 'lib/firebrigade/api.rb', line 124 def add_version(name, project_id) post :add_version, :name => name, :project_id => project_id end |
#check_error(xml) ⇒ Object
Checks for errors in xml
and raises an appropriate Exception.
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/firebrigade/api.rb', line 131 def check_error(xml) # :nodoc: error = xml.elements['/error/message'] return unless error case error.text when /No such \w+ exists/ then raise NotFound, error.text when 'Invalid login' then raise InvalidLogin, error.text when /Your API version/ then raise WrongAPIVersion, error.text else raise Error, error.text end end |
#get_build(version_id, target_id) ⇒ Object
Retrieves a Build for version_id
and target_id
.
146 147 148 |
# File 'lib/firebrigade/api.rb', line 146 def get_build(version_id, target_id) get :get_build, :version_id => version_id, :target_id => target_id end |
#get_owner(name) ⇒ Object
Retrieves an Owner matching name
.
153 154 155 |
# File 'lib/firebrigade/api.rb', line 153 def get_owner(name) get :get_owner, :name => name end |
#get_project(name, owner_id) ⇒ Object
Retrieves a Project matching name
and owner_id
.
160 161 162 |
# File 'lib/firebrigade/api.rb', line 160 def get_project(name, owner_id) get :get_project, :name => name, :owner_id => owner_id end |
#get_target(version, release_date, platform) ⇒ Object
Retrieves a Target matching version
, release_date
and platform
.
167 168 169 170 171 |
# File 'lib/firebrigade/api.rb', line 167 def get_target(version, release_date, platform) get :get_target, :version => version, :release_date => release_date, :platform => platform, :username => @username, :api_version => API_VERSION end |
#get_version(name, project_id) ⇒ Object
Retrieves a Version matching name
nad project_id
.
176 177 178 |
# File 'lib/firebrigade/api.rb', line 176 def get_version(name, project_id) get :get_version, :name => name, :project_id => project_id end |
#make_multipart(params) ⇒ Object
Makes a multipart POST from params
.
183 184 185 186 187 |
# File 'lib/firebrigade/api.rb', line 183 def make_multipart(params) # :nodoc: set_hash params super params end |
#make_url(method, params) ⇒ Object
Makes a URL for method
and params
.
192 193 194 195 196 |
# File 'lib/firebrigade/api.rb', line 192 def make_url(method, params) # :nodoc: set_hash params if method.to_s =~ /^add_/ super method, params end |
#parse_response(xml) ⇒ Object
Creates an Object from xml
.
201 202 203 204 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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/firebrigade/api.rb', line 201 def parse_response(xml) # :nodoc: ok = xml.elements['/ok'] raise RCRest::Error, xml.to_s if ok.nil? child = ok.elements[1] obj = nil case child.name when 'build' then obj = Build.new obj.id = child.elements['id'].text.to_i obj.successful = child.elements['successful'].text == 'true' obj.duration = child.elements['duration'].text.to_f obj.target_id = child.elements['target_id'].text.to_i obj.version_id = child.elements['version_id'].text.to_i obj.created_on = Time.parse child.elements['created_on'].text when 'owner' then obj = Owner.new obj.id = child.elements['id'].text.to_i obj.name = child.elements['name'].text when 'project' then obj = Project.new obj.id = child.elements['id'].text.to_i obj.name = child.elements['name'].text obj.owner_id = child.elements['owner_id'].text.to_i when 'target' then obj = Target.new obj.id = child.elements['id'].text.to_i obj.platform = child.elements['platform'].text obj.release_date = child.elements['release_date'].text obj.username = child.elements['username'].text obj.version = child.elements['version'].text when 'version' then obj = Version.new obj.id = child.elements['id'].text.to_i obj.name = child.elements['name'].text obj.project_id = child.elements['project_id'].text.to_i else raise "don't know how to create a #{child.name}" end obj rescue NoMethodError puts $! puts $!.backtrace.join("\n\t") puts puts xml raise end |
#set_hash(params) ⇒ Object
Sets the request hash value for params
.
253 254 255 256 257 258 259 260 261 262 |
# File 'lib/firebrigade/api.rb', line 253 def set_hash(params) param_values = params.sort_by { |n| n.to_s }.map do |name, value| "#{name}=#{URI.escape value.to_s}" end.join '&' hash = Digest::MD5.hexdigest "#{param_values}:#{@username}:#{@password}" params[:hash] = hash params[:user] = @username end |