lib/youtrack/client.rb in youtrack-0.0.1 vs lib/youtrack/client.rb in youtrack-0.0.2

- old
+ new

@@ -1,8 +1,7 @@ module Youtrack class Client - include HTTParty # holds the youTrack Server url # defaults to nil attr_accessor :url @@ -13,24 +12,64 @@ # stores the Server password credential # defaulst to nil attr_accessor :password # stores the response object - attr_accessor :response + attr_accessor :connection + # stores the auth_headers + attr_accessor :cookies + + # stores the scope of all subsequent api calls + attr_accessor :admin + + def admin? + true == @admin + end + def initialize(options={}) + @cookies = {} + @admin = false end - def server_endpoint - @server_endpoint ||= File.join(url, "rest") + # the server endpoint + def endpoint + @endpoint = File.join(url, 'rest') end def credentials_hash { login: login, password: password } end - def login! - @response = self.class.post(server_endpoint + "/login", { query: credentials_hash }) + + # Makes a login call and sets the Cookie headers + # + # Returns the status code of the connection call + def connect! + @connection = HTTParty.post(File.join(url, "rest/user/login"), body: credentials_hash ) + @cookies['Cookie'] = @connection.headers['set-cookie'] + @connection.code end + def connected? + !!(connection && connection.headers['set-cookie']) + end + + def users + resource(:user).new(self) + end + + def projects + resource(:project).new(self) + end + + def issues + resource(:issue).new(self) + end + + private + + def resource(resource_name) + Youtrack.const_get(resource_name.to_s.capitalize) + end end end