Class: Akatus::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/akatus/service.rb

Instance Method Summary collapse

Instance Method Details

#send_requestObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/akatus/service.rb', line 5

def send_request

  path   = self.class::PATH
  method = self.class::METHOD

  url     = Akatus.config.api_url + path + ".json"
  payload = self.to_payload

  begin

    if method == :post
      data = RestClient.post(url, payload.to_json, :content_type => :json, :accept => :json)
    elsif method == :get
      data = RestClient.get(url, { :params => payload })
    else
      raise "Invalid method: #{method}"
    end

    JSON.parse(data)['resposta']

  rescue RestClient::UnprocessableEntity => exc
    message = JSON.load(exc.response)['resposta']['descricao']
    raise Akatus::UnprocessableEntityError.new(message)
  end

end