Sha256: 0e76db55059a26a1fe76d51c5ad6a1ad979d3a14c4ba1b929882ad9fc85deee1

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

require 'httparty'
require "json"

module Morale
  class Client
    class Unauthorized  < RuntimeError; end
    class NotFound < RuntimeError; end
    
    include HTTParty
    format :json
    
    API_VERSION = 'v1'
    
    attr_accessor :api_key
    attr_reader   :subdomain
    
    def self.authorize(user, password, subdomain)
      client = new(subdomain)
      client.unauthorize
      client.api_key = client.class.post('/in', :body => { :email => user, :password => password })["api_key"]
      return client
    end
    
    def self.accounts(user)
      client = new
      client.unauthorize
      response = client.class.get("/accounts", :query => { :email => user })
      raise Unauthorized if response.code == 401
      raise NotFound if response.code == 404
      response
    end
    
    def accounts
      authorize
      response = self.class.get("/accounts", :query => { :api_key => @api_key })
      raise Unauthorized if response.code == 401
      raise NotFound if response.code == 404
      response
    end
    
    def initialize(subdomain="", api_key="")
      @api_key = api_key
      @subdomain = subdomain
      #TODO: Save the domain in a config file
      self.class.default_options[:base_uri] = HTTParty.normalize_base_uri("#{subdomain}#{"." unless subdomain.nil? || subdomain.empty?}lvh.me:3000/api/#{API_VERSION}")
    end
    
    def projects
      authorize
      response = self.class.get('/projects')
      raise Unauthorized if response.code == 401
      raise NotFound if response.code == 404
      response
    end
    
    def tickets(options={})
    end
    
    def ticket(project_id, command)
      authorize
      response = self.class.post("/projects/#{project_id}/tickets", :body => { :command => command })
      raise Unauthorized if response.code == 401
      raise NotFound if response.code == 404
      response
    end
    
    def authorize
      self.class.basic_auth @subdomain, @api_key
    end
    
    def unauthorize
      self.class.basic_auth nil, nil
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
morale-0.1.0 lib/morale/client.rb