Class: Xplenty::API
- Inherits:
-
Object
- Object
- Xplenty::API
- Defined in:
- lib/xplenty/api.rb,
lib/xplenty/api/jobs.rb,
lib/xplenty/api/mock.rb,
lib/xplenty/api/errors.rb,
lib/xplenty/api/version.rb,
lib/xplenty/api/clusters.rb,
lib/xplenty/api/watchers.rb,
lib/xplenty/api/mock/jobs.rb,
lib/xplenty/api/cluster_plans.rb,
lib/xplenty/api/mock/clusters.rb,
lib/xplenty/api/vendor/okjson.rb
Defined Under Namespace
Constant Summary collapse
- HEADERS =
{ 'Accept' => 'application/vnd.xplenty+json', 'User-Agent' => "xplenty-rb/#{Xplenty::API::VERSION}", }
- OPTIONS =
{ :headers => {}, :host => 'api.xplenty.com', :nonblock => false, :scheme => 'https' }
- VERSION =
"0.1.1"
Instance Method Summary collapse
-
#cluster_plans ⇒ Object
GET /<accountID>/api/cluster_plans.
-
#cluster_watchers(clusterID) ⇒ Object
GET /<accountID>/api/clusters/cluster_id/watchers.
-
#clusters(options = {}) ⇒ Object
GET /<accountID>/api/clusters.
-
#create_cluster(cluster_info) ⇒ Object
POST /<accountID>/api/clusters.
-
#get_cluster_info(clusterID) ⇒ Object
GET /<accountID>/api/clusters/<clusterID>.
-
#get_job_info(jobID) ⇒ Object
GET <accountID>/api/jobs/<jobID>.
-
#initialize(options = {}) ⇒ API
constructor
A new instance of API.
-
#job_watchers(jobID) ⇒ Object
GET /<accountID>/api/jobs/job_id/watchers.
-
#jobs(options = {}) ⇒ Object
GET <accountID>/api/jobs.
- #request(params, &block) ⇒ Object
-
#run_job(clusterID, packageID, args = {}) ⇒ Object
POST /<accountID>/api/jobs.
-
#stop_watching_cluster(clusterID) ⇒ Object
DELETE /<accountID>/api/clusters/clusterID/watchers.
-
#stop_watching_job(jobID) ⇒ Object
DELETE /<accountID>/api/jobs/job_id/watchers.
-
#terminate_cluster(clusterID) ⇒ Object
DELETE /<accountID>/api/clusters/<clusterID>.
-
#terminate_job(jobID) ⇒ Object
DELETE /<accountID>/api/jobs/<jobID>.
-
#watch_cluster(clusterID) ⇒ Object
POST /<accountID>/api/clusters/clusterID/watchers.
-
#watch_job(jobID) ⇒ Object
POST /<accountID>/api/jobs/job_id/watchers.
Constructor Details
#initialize(options = {}) ⇒ API
Returns a new instance of API.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/xplenty/api.rb', line 40 def initialize(={}) = OPTIONS.merge() @api_key = .delete(:api_key) || ENV['XPLENTY_API_KEY'] @account_id = .delete(:account_id) || ENV['XPLENTY_ACCOUNT_ID'] @version = .delete(:version) @mime_type = "application/vnd.xplenty+json" if @version && @version.is_a?(Fixnum) @mime_type = @mime_type << "; version=#{@version}" end user_name = "#{@api_key}" [:headers] = HEADERS.merge({ 'Authorization' => "Basic #{Base64.encode64(user_name).gsub("\n", '')}", 'Accept' => @mime_type }).merge([:headers]) @connection = Excon.new("#{[:scheme]}://#{[:host]}", ) end |
Instance Method Details
#cluster_plans ⇒ Object
GET /<accountID>/api/cluster_plans
4 5 6 7 8 9 10 |
# File 'lib/xplenty/api/cluster_plans.rb', line 4 def cluster_plans request( :expects => 200, :method => :get, :path => "/#{account_id}/api/cluster_plans" ) end |
#cluster_watchers(clusterID) ⇒ Object
GET /<accountID>/api/clusters/cluster_id/watchers
22 23 24 25 26 27 28 |
# File 'lib/xplenty/api/watchers.rb', line 22 def cluster_watchers(clusterID) request( :expects => 200, :method => :get, :path => "/#{account_id}/api/clusters/#{clusterID}/watchers" ) end |
#clusters(options = {}) ⇒ Object
GET /<accountID>/api/clusters
27 28 29 30 31 32 33 34 |
# File 'lib/xplenty/api/clusters.rb', line 27 def clusters(={}) request( :expects => 200, :method => :get, :path => "/#{account_id}/api/clusters", :query => ) end |
#create_cluster(cluster_info) ⇒ Object
POST /<accountID>/api/clusters
4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/xplenty/api/clusters.rb', line 4 def create_cluster(cluster_info) cluster_info = cluster_info.inject({}) { |v, hv| v.merge({"cluster[#{hv.first}]" => hv.last}) } request( :expects => 201, :method => :post, :path => "/#{account_id}/api/clusters", :query => cluster_info ) end |
#get_cluster_info(clusterID) ⇒ Object
GET /<accountID>/api/clusters/<clusterID>
18 19 20 21 22 23 24 |
# File 'lib/xplenty/api/clusters.rb', line 18 def get_cluster_info(clusterID) request( :expects => 200, :method => :get, :path => "/#{account_id}/api/clusters/#{clusterID}" ) end |
#get_job_info(jobID) ⇒ Object
GET <accountID>/api/jobs/<jobID>
4 5 6 7 8 9 10 |
# File 'lib/xplenty/api/jobs.rb', line 4 def get_job_info(jobID) request( :expects => 200, :method => :get, :path => "/#{account_id}/api/jobs/#{jobID}" ) end |
#job_watchers(jobID) ⇒ Object
GET /<accountID>/api/jobs/job_id/watchers
49 50 51 52 53 54 55 |
# File 'lib/xplenty/api/watchers.rb', line 49 def job_watchers(jobID) request( :expects => 200, :method => :get, :path => "/#{account_id}/api/jobs/#{jobID}/watchers" ) end |
#jobs(options = {}) ⇒ Object
GET <accountID>/api/jobs
13 14 15 16 17 18 19 20 |
# File 'lib/xplenty/api/jobs.rb', line 13 def jobs(={}) request( :expects => 200, :method => :get, :path => "/#{account_id}/api/jobs", :query => ) end |
#request(params, &block) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/xplenty/api.rb', line 59 def request(params, &block) begin response = @connection.request(params, &block) rescue Excon::Errors::HTTPStatusError => error klass = case error.response.status when 400 then Xplenty::API::Errors::BadRequest when 401 then Xplenty::API::Errors::Unauthorized when 402 then Xplenty::API::Errors::PaymentRequired when 403 then Xplenty::API::Errors::Forbidden when 404 then Xplenty::API::Errors::NotFound when 406 then Xplenty::API::Errors::NotAcceptable when 415 then Xplenty::API::Errors::UnsupportedMediaType when 422 then Xplenty::API::Errors::UnprocessableEntity when 429 then Xplenty::API::Errors::TooManyRequests when 500 then Xplenty::API::Errors::RequestFailed when 502 then Xplenty::API::Errors::BadGateway when 503 then Xplenty::API::Errors::ServiceUnavailable else Xplenty::API::Errors::ErrorWithResponse end reerror = klass.new(error., error.response) reerror.set_backtrace(error.backtrace) raise(reerror) end if response.body && !response.body.empty? if response.headers['Content-Encoding'] == 'gzip' response.body = Zlib::GzipReader.new(StringIO.new(response.body)).read end begin response.body = Xplenty::API::OkJson.decode(response.body) #rescue # leave non-JSON body as is end end # reset (non-persistent) connection @connection.reset response end |
#run_job(clusterID, packageID, args = {}) ⇒ Object
POST /<accountID>/api/jobs
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/xplenty/api/jobs.rb', line 23 def run_job(clusterID, packageID, args = {}) query = args.inject({}) {|v, kv| v.merge( { "job[variables][#{kv.first}]" => kv.last }) } query = query.merge( { 'job[cluster_id]' => clusterID, 'job[package_id]' => packageID }) request( :expects => 201, :method => :post, :path => "/#{account_id}/api/jobs", :query => query ) end |
#stop_watching_cluster(clusterID) ⇒ Object
DELETE /<accountID>/api/clusters/clusterID/watchers
13 14 15 16 17 18 19 |
# File 'lib/xplenty/api/watchers.rb', line 13 def stop_watching_cluster(clusterID) request( :expects => 204, :method => :delete, :path => "/#{account_id}/api/clusters/#{clusterID}/watchers" ) end |
#stop_watching_job(jobID) ⇒ Object
DELETE /<accountID>/api/jobs/job_id/watchers
40 41 42 43 44 45 46 |
# File 'lib/xplenty/api/watchers.rb', line 40 def stop_watching_job(jobID) request( :expects => 204, :method => :delete, :path => "/#{account_id}/api/jobs/#{jobID}/watchers" ) end |
#terminate_cluster(clusterID) ⇒ Object
DELETE /<accountID>/api/clusters/<clusterID>
37 38 39 40 41 42 43 |
# File 'lib/xplenty/api/clusters.rb', line 37 def terminate_cluster(clusterID) request( :expects => 200, :method => :delete, :path => "/#{account_id}/api/clusters/#{clusterID}" ) end |
#terminate_job(jobID) ⇒ Object
DELETE /<accountID>/api/jobs/<jobID>
45 46 47 48 49 50 51 |
# File 'lib/xplenty/api/jobs.rb', line 45 def terminate_job(jobID) request( :expects => 200, :method => :delete, :path => "/#{account_id}/api/jobs/#{jobID}" ) end |
#watch_cluster(clusterID) ⇒ Object
POST /<accountID>/api/clusters/clusterID/watchers
4 5 6 7 8 9 10 |
# File 'lib/xplenty/api/watchers.rb', line 4 def watch_cluster(clusterID) request( :expects => 200, :method => :post, :path => "/#{account_id}/api/clusters/#{clusterID}/watchers" ) end |
#watch_job(jobID) ⇒ Object
POST /<accountID>/api/jobs/job_id/watchers
31 32 33 34 35 36 37 |
# File 'lib/xplenty/api/watchers.rb', line 31 def watch_job(jobID) request( :expects => 200, :method => :post, :path => "/#{account_id}/api/jobs/#{jobID}/watchers" ) end |