Class: EY::API
Direct Known Subclasses
Defined Under Namespace
Classes: InvalidCredentials, RequestFailed
Instance Attribute Summary collapse
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Class Method Summary collapse
- .fetch_token(email, password) ⇒ Object
- .read_token(file = nil) ⇒ Object
- .request(path, opts = {}) ⇒ Object
- .save_token(token, file = nil) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #app_for_repo(repo) ⇒ Object
- #app_for_repo!(repo) ⇒ Object
- #apps ⇒ Object
- #environments ⇒ Object
-
#initialize(token = nil) ⇒ API
constructor
A new instance of API.
- #request(url, opts = {}) ⇒ Object
Constructor Details
#initialize(token = nil) ⇒ API
Returns a new instance of API.
7 8 9 10 11 |
# File 'lib/engineyard/api.rb', line 7 def initialize(token = nil) @token ||= token @token ||= self.class.read_token raise ArgumentError, "EY Cloud API token required" unless @token end |
Instance Attribute Details
#token ⇒ Object (readonly)
Returns the value of attribute token.
5 6 7 |
# File 'lib/engineyard/api.rb', line 5 def token @token end |
Class Method Details
.fetch_token(email, password) ⇒ Object
92 93 94 95 96 97 |
# File 'lib/engineyard/api.rb', line 92 def self.fetch_token(email, password) api_token = request("/authenticate", :method => "post", :params => { :email => email, :password => password })["api_token"] save_token(api_token) api_token end |
.read_token(file = nil) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/engineyard/api.rb', line 99 def self.read_token(file = nil) file ||= ENV['EYRC'] || File.("~/.eyrc") return false unless File.exists?(file) require 'yaml' data = YAML.load_file(file) if EY.config.default_endpoint? data["api_token"] else (data[EY.config.endpoint.to_s] || {})["api_token"] end end |
.request(path, opts = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 |
# File 'lib/engineyard/api.rb', line 44 def self.request(path, opts={}) require 'rest_client' require 'json' url = EY.config.endpoint + "api/v2#{path}" method = ((meth = opts.delete(:method)) && meth.to_s || "get").downcase.to_sym params = opts.delete(:params) || {} headers = opts.delete(:headers) || {} headers["Accept"] ||= "application/json" begin EY.ui.debug("Request", "#{method.to_s.upcase} #{url}") case method when :get, :delete, :head url.query = RestClient::Payload::UrlEncoded.new(params).to_s resp = RestClient.send(method, url.to_s, headers) else resp = RestClient.send(method, url.to_s, params, headers) end rescue RestClient::Unauthorized raise InvalidCredentials rescue Errno::ECONNREFUSED raise RequestFailed, "Could not reach the cloud API" rescue RestClient::ResourceNotFound raise RequestFailed, "The requested resource could not be found" rescue RestClient::RequestFailed => e raise RequestFailed, "#{e.}" rescue OpenSSL::SSL::SSLError raise RequestFailed, "SSL is misconfigured on your cloud" end if resp.body.empty? data = '' elsif resp.headers[:content_type] =~ /application\/json/ begin data = JSON.parse(resp.body) EY.ui.debug("Response", data) rescue JSON::ParserError EY.ui.debug("Raw response", resp.body) raise RequestFailed, "Response was not valid JSON." end else data = resp.body end data end |
.save_token(token, file = nil) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/engineyard/api.rb', line 113 def self.save_token(token, file = nil) file ||= ENV['EYRC'] || File.("~/.eyrc") require 'yaml' data = File.exists?(file) ? YAML.load_file(file) : {} if EY.config.default_endpoint? data.merge!("api_token" => token) else data.merge!(EY.config.endpoint.to_s => {"api_token" => token}) end File.open(file, "w"){|f| YAML.dump(data, f) } true end |
Instance Method Details
#==(other) ⇒ Object
13 14 15 16 |
# File 'lib/engineyard/api.rb', line 13 def ==(other) raise ArgumentError unless other.is_a?(self.class) self.token == other.token end |
#app_for_repo(repo) ⇒ Object
33 34 35 |
# File 'lib/engineyard/api.rb', line 33 def app_for_repo(repo) apps.find{|a| repo.urls.include?(a.repository_uri) } end |
#app_for_repo!(repo) ⇒ Object
37 38 39 |
# File 'lib/engineyard/api.rb', line 37 def app_for_repo!(repo) app_for_repo(repo) || raise(NoAppError.new(repo)) end |
#apps ⇒ Object
29 30 31 |
# File 'lib/engineyard/api.rb', line 29 def apps @apps ||= EY::Model::App.from_array(request('/apps')["apps"], :api => self) end |
#environments ⇒ Object
25 26 27 |
# File 'lib/engineyard/api.rb', line 25 def environments @environments ||= EY::Model::Environment.from_array(request('/environments')["environments"], :api => self) end |