Blame view

lib/syspro/syspro_response.rb 1.57 KB
db76748d   Isaac Lewis   cop a bunch of St...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  require "nokogiri"
  
  module Syspro
    # SysproResponse encapsulates some vitals of a response that came back from
    # the Syspro API.
    class SysproResponse
      # The data contained by the HTTP body of the response deserialized from
      # JSON.
      attr_accessor :data
  
      # The raw HTTP body of the response.
      attr_accessor :http_body
  
      # A Hash of the HTTP headers of the response.
      attr_accessor :http_headers
  
      # The integer HTTP status code of the response.
      attr_accessor :http_status
  
      # The Syspro request ID of the response.
      attr_accessor :request_id
  
      # Initializes a SysproResponse object from a Hash like the kind returned as
      # part of a Faraday exception.
      #
      # This may throw JSON::ParserError if the response body is not valid JSON.
      def self.from_faraday_hash(http_resp)
        resp = SysproResponse.new
db76748d   Isaac Lewis   cop a bunch of St...
29
        resp.http_body = http_resp[:body]
a61f2fc8   Isaac Lewis   parse logon profile
30
        resp.data = Nokogiri::XML(resp.http_body)
db76748d   Isaac Lewis   cop a bunch of St...
31
32
33
34
35
36
37
38
39
40
41
        resp.http_headers = http_resp[:headers]
        resp.http_status = http_resp[:status]
        resp.request_id = http_resp[:headers]["Request-Id"]
        resp
      end
  
      # Initializes a SysproResponse object from a Faraday HTTP response object.
      #
      # This may throw JSON::ParserError if the response body is not valid JSON.
      def self.from_faraday_response(http_resp)
        resp = SysproResponse.new
db76748d   Isaac Lewis   cop a bunch of St...
42
        resp.http_body = http_resp.body
a61f2fc8   Isaac Lewis   parse logon profile
43
        resp.data = Nokogiri::XML(resp.http_body)
db76748d   Isaac Lewis   cop a bunch of St...
44
45
46
47
48
49
50
        resp.http_headers = http_resp.headers
        resp.http_status = http_resp.status
        resp.request_id = http_resp.headers["Request-Id"]
        resp
      end
    end
  end