Module: GW2::HTTPS

Included in:
Resource
Defined in:
lib/gw2/https.rb

Constant Summary collapse

DEFAULT_REQUEST =
{ action: "Get", ssl: true }

Instance Method Summary collapse

Instance Method Details

#query_string(query_hash = {}) ⇒ Object



6
7
8
9
10
11
# File 'lib/gw2/https.rb', line 6

def query_string(query_hash = {})
  string = query_hash.collect{ |k,v| "#{k}=#{v}" }.join("&")
  string.prepend("?") unless string.empty?

  string
end

#request(end_point = "", attr = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gw2/https.rb', line 13

def request(end_point = "", attr = {})
  attr = DEFAULT_REQUEST.merge(attr)

  if V1_ENDPOINTS.any? { |word| end_point.include?(word) }
    uri = URI.parse(BASE_URL_V1 + end_point + query_string(attr[:query] || {}))
  else
    uri = URI.parse(BASE_URL_V2 + end_point + query_string(attr[:query] || {}))
  end

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = attr[:ssl]
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE if attr[:ssl] # need to get a cert -_____-

  request = Net::HTTP.const_get(attr[:action]).new(uri.request_uri)
  attr[:headers].each { |key, value| request[key.to_s] = value } if attr[:headers]

  request.set_form_data(attr[:form_data]) if attr[:form_data]
  http.request(request)
end