Class: ApprovalHub::API

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/approval_hub/api.rb

Overview

The ApprovalHub API

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, username, password, with_debugging = false) ⇒ API

Returns a new instance of API.

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
# File 'lib/approval_hub/api.rb', line 50

def initialize(url, username, password, with_debugging=false)
  raise ArgumentError if url.nil? || username.nil? || password.nil?
  unless url.nil?
    @uri = url
  end
  @auth = {:user => username, :pw => password}
  if with_debugging
    self.class.debug_output $stderr
  end
end

Class Method Details

.custom_config(config = {}) ⇒ Object

get an instance of your choosing requires a url, username, password and boolean indicating whether or not to turn on session debugging

Raises:

  • (ArgumentError)


44
45
46
47
# File 'lib/approval_hub/api.rb', line 44

def custom_config(config={})
  raise(ArgumentError, "config cannot be empty") if config.empty?
  new(config["url"], config["username"], config["password"], config["with_debugging"])
end

.prod(username, password) ⇒ Object

get an instance of the prod hub requires a username and password



22
23
24
# File 'lib/approval_hub/api.rb', line 22

def prod(username, password)
  new(URL_PROD,username,password)
end

.prod_with_debugging(username, password) ⇒ Object

get an instance of the prod hub with session debugging requires a username and password



36
37
38
# File 'lib/approval_hub/api.rb', line 36

def prod_with_debugging(username,password)
  new(URL_PROD,username,password, true)
end

.uat(username, password) ⇒ Object

get an instance of the uat hub requires a username and password



15
16
17
# File 'lib/approval_hub/api.rb', line 15

def uat(username, password)
  new(URL_UAT,username,password)
end

.uat_with_debugging(username, password) ⇒ Object

get an instance of the uat hub with session debugging requires a username and password



29
30
31
# File 'lib/approval_hub/api.rb', line 29

def uat_with_debugging(username,password)
  new(URL_UAT,username,password, true)
end

Instance Method Details

#approve_request(request_id, approver_id) ⇒ Object

Approve an open request Returns true if successful else false



168
169
170
# File 'lib/approval_hub/api.rb', line 168

def approve_request(request_id, approver_id)
  approve_request!(request_id,approver_id) rescue false
end

#approve_request!(request_id, approver_id) ⇒ Object

Same as approve_request but will throw exception on error



175
176
177
# File 'lib/approval_hub/api.rb', line 175

def approve_request!(request_id, approver_id)
  update_request(request_id,approver_id, ApprovalHub::APPROVE)
end

#cancel_request(request_id, approver_id) ⇒ Object

Cancel an open request Returns true if successful else false



182
183
184
# File 'lib/approval_hub/api.rb', line 182

def cancel_request(request_id, approver_id)
  cancel_request!(request_id,approver_id) rescue false
end

#cancel_request!(request_id, approver_id) ⇒ Object

Same as cancel_request but will throw exception on error



189
190
191
# File 'lib/approval_hub/api.rb', line 189

def cancel_request!(request_id, approver_id)
  update_request(request_id,approver_id, ApprovalHub::CANCEL)
end

#decline_request(request_id, approver_id, reason = nil) ⇒ Object

decline an open request. Takes an optional reason for rejection Returns true if success else false



197
198
199
# File 'lib/approval_hub/api.rb', line 197

def decline_request(request_id, approver_id, reason=nil)
  decline_request!(request_id,approver_id,reason) rescue false
end

#decline_request!(request_id, approver_id, reason = nil) ⇒ Object

Same as decline_request but will throw exception on error



204
205
206
# File 'lib/approval_hub/api.rb', line 204

def decline_request!(request_id, approver_id, reason=nil)
  update_request(request_id,approver_id, ApprovalHub::DECLINE,reason)
end

#details(request_id, &block) ⇒ Object

Retrieves a Detail object from the hub requires a request_id returns false if an error occured takes an optional block which will be passed the detail object



113
114
115
# File 'lib/approval_hub/api.rb', line 113

def details(request_id, &block)
  details!(request_id, &block) rescue false
end

#details!(request_id, &block) ⇒ Object

Same as details but will throw an exception if it encounters an error



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/approval_hub/api.rb', line 120

def details!(request_id, &block)
  raise "Request ID cannot be nil" if request_id.nil?
  begin
    options = {
      :query => {
        :requestID => request_id
      }.merge!(@auth)
    }
    detail = self.class.get( @uri + DETAILS, options )
    if detail["response"]["status"] == 200
      detail = Detail.new(detail["ApprovalList"][0])
      if block_given?
        yield detail
      else
        detail
      end
    else
      raise ArgumentError, "request not found"
    end
  rescue Exception => e
    raise e, "Unable to retrieve details for #{request_id}"
  end
end

#register(request = Request.new, &block) ⇒ Object

Register your approval request with the hub Registration takes a Request object or a block returns true is successful else false

example1:

api = ApprovalHub::API.uat("someuser","somepwd")
api.register do |r|
   r.request_id        = "001"
   r.requestor_id      = "neilcuk"
   r.approver_id       = "kasperdulles[,others..]"
   r.short_description = "A summary"
   r.long_description  = "A lengthier expose"
   r.url               = "http://url.to.source/system"
end

example2:

api = ApprovalHub::API.uat("someuser","somepwd")
r = ApprovalHub::Request.new
r.request_id        = "001"
r.requestor_id      = "neilcuk"
r.approver_id       = "kasperdulles[,others..]"
r.short_description = "A summary"
r.long_description  = "A lengthier expose"
r.url               = "http://url.to.source/system"
api.register(r)


87
88
89
# File 'lib/approval_hub/api.rb', line 87

def register(request = Request.new, &block)
 register!(request,&block) rescue false
end

#register!(request = Request.new, &block) ⇒ Object

Same as register but will throw an exception if it encounteres an error



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/approval_hub/api.rb', line 94

def register!(request = Request.new, &block)
  begin
    options = assemble_request(request,&block) 
   request = self.class.get( @uri + REGISTER, options )
  rescue Exception => e
    raise e, "Unable to register approval request"
  end
  if request.response.code != "200"
    raise "Approval hub denied request registration with code #{request.response.code}"
  end
  true
end

#status(request_id) ⇒ Object

Returns the status of the provided request id or false if some error occured



147
148
149
# File 'lib/approval_hub/api.rb', line 147

def status(request_id)
  status!(request_id) rescue false
end

#status!(request_id) ⇒ Object

Same as status but will throw an excption if it encounters an error



154
155
156
157
158
159
160
161
162
163
# File 'lib/approval_hub/api.rb', line 154

def status!(request_id)
  detail = details!(request_id)
  if detail.open?
    "Pending Approval"
  elsif detail.approved?
    "Approved"
  elsif detail.declined?
    "Declined"
  end
end