Class: Morale::Client

Inherits:
Object
  • Object
show all
Extended by:
ConnectionStore
Includes:
HTTParty
Defined in:
lib/morale/client.rb

Defined Under Namespace

Classes: NotFound, Unauthorized

Constant Summary collapse

API_VERSION =
'v1'

Instance Attribute Summary collapse

Attributes included from Storage

#location

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConnectionStore

base_url, base_url=, default_location, delete_connection, location, location=, read_connection, write_connection

Methods included from Platform

#home_directory, #running_on_windows?

Methods included from Storage

#delete, #read, #write

Constructor Details

#initialize(subdomain = "", api_key = "") ⇒ Client

Returns a new instance of Client.



47
48
49
50
51
# File 'lib/morale/client.rb', line 47

def initialize(subdomain="", api_key="")
  @api_key = api_key
  @subdomain = subdomain
  self.class.default_options[:base_uri] = HTTParty.normalize_base_uri("#{subdomain}#{"." unless subdomain.nil? || subdomain.empty?}#{self.class.base_url}/api/#{API_VERSION}")
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



20
21
22
# File 'lib/morale/client.rb', line 20

def api_key
  @api_key
end

#subdomainObject (readonly)

Returns the value of attribute subdomain.



21
22
23
# File 'lib/morale/client.rb', line 21

def subdomain
  @subdomain
end

Class Method Details

.accounts(user) ⇒ Object

Raises:



30
31
32
33
34
35
36
37
# File 'lib/morale/client.rb', line 30

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

.authorize(user, password, subdomain) ⇒ Object



23
24
25
26
27
28
# File 'lib/morale/client.rb', line 23

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

Instance Method Details

#accountsObject

Raises:



39
40
41
42
43
44
45
# File 'lib/morale/client.rb', line 39

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

#authorizeObject



77
78
79
# File 'lib/morale/client.rb', line 77

def authorize
  self.class.basic_auth @subdomain, @api_key
end

#projectsObject

Raises:



53
54
55
56
57
58
59
# File 'lib/morale/client.rb', line 53

def projects
  authorize
  response = self.class.get('/projects')
  raise Unauthorized if response.code == 401
  raise NotFound if response.code == 404
  response
end

#ticket(project_id, command) ⇒ Object

Raises:



69
70
71
72
73
74
75
# File 'lib/morale/client.rb', line 69

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

#tickets(options = {}) ⇒ Object

Raises:



61
62
63
64
65
66
67
# File 'lib/morale/client.rb', line 61

def tickets(options={})
  authorize
  response = self.class.get("/projects/#{options[:project_id]}/tickets")
  raise Unauthorized if response.code == 401
  raise NotFound if response.code == 404
  response
end

#unauthorizeObject



81
82
83
# File 'lib/morale/client.rb', line 81

def unauthorize
  self.class.basic_auth nil, nil
end