Class: EYCli::Api
- Inherits:
-
Object
- Object
- EYCli::Api
- Defined in:
- lib/ey_cli/api.rb
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
Instance Method Summary collapse
- #connection ⇒ Object
- #delete(path, params = {}, headers = {}) ⇒ Object
- #fetch_token(times = 3) ⇒ Object
- #get(path, params = {}, headers = {}) ⇒ Object
-
#initialize(endpoint = nil) ⇒ Api
constructor
A new instance of Api.
- #post(path, body, params = {}, headers = {}, auth = true) ⇒ Object
- #put(path, body, params = {}, headers = {}) ⇒ Object
- #read_token(file = nil) ⇒ Object
- #request(path, params = {}, headers = {}, auth = true) ⇒ Object
- #save_token(token, file = nil) ⇒ Object
Constructor Details
#initialize(endpoint = nil) ⇒ Api
Returns a new instance of Api.
9 10 11 |
# File 'lib/ey_cli/api.rb', line 9 def initialize(endpoint = nil) @endpoint = endpoint || ENV['EY_ENDPOINT'] || 'https://cloud.engineyard.com/api/v2/' end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
7 8 9 |
# File 'lib/ey_cli/api.rb', line 7 def endpoint @endpoint end |
Instance Method Details
#connection ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/ey_cli/api.rb', line 95 def connection @connection ||= Faraday::Connection.new(:url => endpoint) do |builder| builder.adapter Faraday.default_adapter builder.use Faraday::Response::ParseJson builder.use FaradayStack::FollowRedirects builder.response :raise_error end end |
#delete(path, params = {}, headers = {}) ⇒ Object
62 63 64 |
# File 'lib/ey_cli/api.rb', line 62 def delete(path, params = {}, headers = {}) request(path, {:method => :delete}.merge(params), headers) end |
#fetch_token(times = 3) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ey_cli/api.rb', line 22 def fetch_token(times = 3) EYCli.term.say("I don't know who you are, please log in with your Engine Yard Cloud credentials.") begin email = EYCli.term.ask("Email: ") password = EYCli.term.ask("Password: ", true) response = post("authenticate", nil, { :email => email, :password => password }, {}, false) save_token(response.body['api_token']) rescue Faraday::Error::ClientError EYCli.term.warning "Invalid username or password; please try again." if (times -= 1) > 0 retry else exit 1 end end end |
#get(path, params = {}, headers = {}) ⇒ Object
50 51 52 |
# File 'lib/ey_cli/api.rb', line 50 def get(path, params = {}, headers = {}) request(path, {:method => :get}.merge(params), headers) end |
#post(path, body, params = {}, headers = {}, auth = true) ⇒ Object
54 55 56 |
# File 'lib/ey_cli/api.rb', line 54 def post(path, body, params = {}, headers = {}, auth = true) request(path, {:method => :post, :body => body}.merge(params), headers, auth) end |
#put(path, body, params = {}, headers = {}) ⇒ Object
58 59 60 |
# File 'lib/ey_cli/api.rb', line 58 def put(path, body, params = {}, headers = {}) request(path, {:method => :put, :body => body}.merge(params), headers) end |
#read_token(file = nil) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/ey_cli/api.rb', line 13 def read_token(file = nil) file ||= ENV['EYRC'] || File.("~/.eyrc") return nil unless File.exists?(file) if data = YAML.load_file(file) (data[endpoint] && data[endpoint]['api_token']) || data['api_token'] end end |
#request(path, params = {}, headers = {}, auth = true) ⇒ Object
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 |
# File 'lib/ey_cli/api.rb', line 66 def request(path, params = {}, headers = {}, auth = true) http_method = params.delete(:method).to_s.downcase http_body = params.delete(:body) @auth_token ||= read_token || fetch_token if auth if ENV['EY_DEBUG'] puts %Q{-- DEBUG -- PATH : #{path} PARAMS : #{params.inspect} HEADERS : #{headers.inspect} } end connection.send(http_method) do |req| req.path = path req.params.merge! params req.headers.merge! headers req.headers["X-EY-Cloud-Token"] = @auth_token if auth req.body = http_body if http_body end rescue Faraday::Error::ClientError => e raise e unless e.response[:status] == 401 @auth_token = fetch_token if auth params[:method] = http_method params[:body] = http_body retry end |
#save_token(token, file = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/ey_cli/api.rb', line 40 def save_token(token, file = nil) file ||= ENV['EYRC'] || File.("~/.eyrc") data = File.exists?(file) ? YAML.load_file(file) : {} data[endpoint] = {"api_token" => token} File.open(file, "w"){|f| YAML.dump(data, f) } token end |