Class: MustardClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/MustardClient/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(url = nil, token = nil) ⇒ Client

Returns a new instance of Client.



5
6
7
8
# File 'lib/MustardClient/client.rb', line 5

def initialize(url= nil, token=nil)
  @mustard_url = url
  @user_token = token
end

Instance Method Details

#execute(command) ⇒ Object

Raises:

  • (Exception)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/MustardClient/client.rb', line 10

def execute(command)

  raise Exception if command[:method].nil? || command[:route].nil?

  #Suppress errors that return a response
  begin
    if command[:method] == :get
      r = RestClient.get command[:route], command[:headers]
    elsif command[:method] == :post
      r = RestClient.post command[:route], command[:params], command[:headers]
    elsif command[:method] == :put
      r = RestClient.put command[:route], command[:params], command[:headers]
    elsif command[:method] == :delete
      r = RestClient.delete command[:route], command[:headers]
    end
  rescue RestClient::ExceptionWithResponse => e
    r = e.response

    raise SessionExpiredError if r.include? "Invalid User Token"
  end

  JSON.parse(r)

end