lib/syncano/connection.rb in syncano-4.0.0.alpha4 vs lib/syncano/connection.rb in syncano-4.0.0.pre
- old
+ new
@@ -4,84 +4,83 @@
class Connection
API_VERSION = 'v1'
AUTH_PATH = 'account/auth/'
METHODS = Set.new [:get, :post, :put, :delete, :head, :patch, :options]
- attr_accessor :api_key
- attr_accessor :user_key
-
- class << self
- def api_root
- "https://api.syncano.io"
- end
+ def self.api_root
+ ENV['API_ROOT']
end
- def http_fetcher
- HttpFetcher.new api_key, user_key
- end
-
def initialize(options = {})
self.api_key = options[:api_key]
self.email = options[:email]
self.password = options[:password]
- self.user_key = options[:user_key]
- self.conn = Faraday.new(self.class.api_root) do |faraday|
- faraday.path_prefix = API_VERSION
- faraday.request :multipart
- faraday.request :url_encoded
- faraday.adapter Faraday.default_adapter
- end
+
+ # TODO: take it easy with SSL for development only, temporary solution
+ self.conn = Faraday.new(self.class.api_root, ssl: { verify: false })
+ conn.path_prefix = API_VERSION
+ conn.request :url_encoded
end
def authenticated?
!api_key.nil?
end
- def authenticate
- api_key = request(:post, AUTH_PATH,
- email: email,
- password: password)['account_key']
- self.api_key = api_key
+ def authenticate(email, password)
+ self.email = email
+ self.password = password
+ authenticate!
end
+ def authenticate!
+ response = conn.post(AUTH_PATH, email: email, password: password)
+ body = parse_response(response)
+
+ case response
+ when Status.successful
+ self.api_key = body['account_key']
+ when Status.client_error
+ raise ClientError.new(body, response)
+ end
+ end
+
def request(method, path, params = {})
raise %{Unsupported method "#{method}"} unless METHODS.include? method
+ conn.headers['X-API-KEY'] = api_key
+ response = conn.send(method, path, params)
- conn.headers['X-API-KEY'] = api_key if api_key
- conn.headers['X-USER-KEY'] = user_key if user_key
- conn.headers['User-Agent'] = "Syncano Ruby Gem #{Syncano::VERSION}"
-
- raw_response = conn.send(method, path, params)
-
- Syncano::Response.handle ResponseWrapper.new(raw_response)
+ case response
+ when Status.no_content
+ when Status.successful
+ parse_response response
+ when Status.client_error # TODO figure out if we want to raise an excpetion on not found or not
+ raise ClientError.new(response.body, response)
+ end
end
private
- class ResponseWrapper < BasicObject
- def initialize(response)
- @response = response
- end
+ def parse_response(response)
+ JSON.parse(response.body)
+ end
- def method_missing(name, *args, &block)
- @response.__send__(name, *args, &block)
- end
+ class Status
+ class << self
+ def successful
+ ->(response) { (200...300).include? response.status }
+ end
- def status
- Status.new @response.status
- end
+ def client_error
+ ->(response) { (400...500).include? response.status }
+ end
- private
-
- class Status
- attr_accessor :code
-
- def initialize(code)
- self.code = code
+ def no_content
+ ->(response) { response.status == 204 }
end
end
end
+ attr_accessor :api_key
attr_accessor :api_root
attr_accessor :email
attr_accessor :password
attr_accessor :conn
end