Class: CorePro::Utils::Requestor

Inherits:
Object
  • Object
show all
Defined in:
lib/corepro/utils/requestor.rb

Constant Summary collapse

SDK_USER_AGENT =
"CorePro Ruby SDK v #{CorePro::VERSION}"
@@config =
begin
  if File.exists?('config.yml')
    YAML.load(File.open('config.yml'))
  else
    {}
  end
rescue ArgumentError => e
  puts "Could not parse YAML: #{e.message}"
end

Class Method Summary collapse

Class Method Details

.get(relativeUrl, classDef, connection, loggingObject) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/corepro/utils/requestor.rb', line 30

def self.get(relativeUrl, classDef, connection, loggingObject)
  connection ||= Connection.createFromConfig()
  if connection.headerValue.to_s.empty? || connection.domainName.to_s.empty?
    raise ArgumentError, 'A valid connection with apiKey, apiSecret, and domainName must be specified.'
  end
  uri = URI.parse("https://#{connection.domainName}#{relativeUrl}")
  if connection.proxyServerName != nil && connection.proxyPort != nil
    proxy = Net::HTTP::Proxy(connection.proxyServerName, connection.proxyPort, connection.proxyUser, connection.proxyPassword)
    http = proxy.new(uri.host, uri.port)
  else
    http = Net::HTTP.new(uri.host, uri.port)
  end
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  headers = { 'User-Agent' => SDK_USER_AGENT,
            'Content-Type' => 'application/json; charset=utf-8',
            'Accept' => 'application/json; charset=utf-8',
            'Authorization' => connection.headerValue,
            'Host' => connection.domainName}
  request = Net::HTTP::Get.new(uri.request_uri, headers)

  response = http.request(request)

  #Logger.write("hi mom", loggingObject)
  parseResponse(request, response, classDef, connection, loggingObject)

end

.parseResponse(req, resp, classDef, conn, loggingObject) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/corepro/utils/requestor.rb', line 87

def self.parseResponse(req, resp, classDef, conn, loggingObject)
  case resp.code
    when '501'
      raise 501
    when '502'
      raise 502
    when '503'
      raise 503
    when '504'
      raise 504
    when '505'
      raise 505
    else
      envelope = CorePro::Models::Envelope.new
      envelope.rawRequestBody = req.body
      envelope.rawResponseBody = resp.body
      parsedJson = JSON.parse(resp.body)
      envelope.from_json! parsedJson, { 'data' => classDef }
      if envelope.errors.length > 0
        raise CorePro::CoreProApiException.new(envelope.errors)
      else
        if classDef == nil
          # no class definition given, return raw envelope
          envelope
        else
          # class definition given, return just that data
          envelope.data
        end
      end
  end
end

.post(relativeUrl, classDef, toPost, connection, loggingObject) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/corepro/utils/requestor.rb', line 58

def self.post(relativeUrl, classDef, toPost, connection, loggingObject)
  connection ||= Connection.createFromConfig()
  if connection.headerValue.to_s.empty? || connection.domainName.to_s.empty?
    raise ArgumentError, 'A valid connection with apiKey, apiSecret, and domainName must be specified.'
  end

  uri = URI.parse("https://#{connection.domainName}#{relativeUrl}")
  if connection.proxyServerName != nil && connection.proxyPort != nil
    proxy = Net::HTTP::Proxy(connection.proxyServerName, connection.proxyPort, connection.proxyUser, connection.proxyPassword)
    http = proxy.new(uri.host, uri.port)
  else
    http = Net::HTTP.new(uri.host, uri.port)
  end
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  headers = { 'User-Agent' => SDK_USER_AGENT,
              'Content-Type' => 'application/json; charset=utf-8',
              'Accept' => 'application/json; charset=utf-8',
              'Authorization' => connection.headerValue,
              'Host' => connection.domainName}
  request = Net::HTTP::Post.new(uri.request_uri, headers)
  request.body = toPost.to_json
  response = http.request(request)

  #Logger.write("hi mom", loggingObject)
  parseResponse(request, response, classDef, connection, loggingObject)

end