Class: HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/oxford/face/http_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ HttpClient

Returns a new instance of HttpClient.



7
8
9
10
11
12
13
# File 'lib/oxford/face/http_client.rb', line 7

def initialize(api_key)
  uris = URI.parse("https://api.projectoxford.ai")
  @http = Net::HTTP.new(uris.host, uris.port)
  @http.use_ssl = true
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  @api_key = api_key
end

Instance Method Details

#delete(path, params = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/oxford/face/http_client.rb', line 75

def delete(path, params = {})
  request = Net::HTTP::Delete.new("/face/v1.0#{path}")
  request.content_type = "application/json"
  request['Ocp-Apim-Subscription-Key'] = @api_key

  if params.is_a?(Hash)
    request.set_form_data(params)
  else
    request.body = params
  end

  response = @http.request(request)
  response
end

#get(path, params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/oxford/face/http_client.rb', line 15

def get(path, params = {})
  request = Net::HTTP::Get.new("/face/v1.0#{path}")
  request.content_type = "application/json"
  request['Ocp-Apim-Subscription-Key'] = @api_key

  if params.is_a?(Hash)
    request.set_form_data(params)
  else
    request.body = params
  end

  response = @http.request(request)
  response
end

#patch(path, params = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/oxford/face/http_client.rb', line 60

def patch(path, params = {})
  request = Net::HTTP::Patch.new("/face/v1.0#{path}")
  request.content_type = "application/json"
  request['Ocp-Apim-Subscription-Key'] = @api_key

  if params.is_a?(Hash)
    request.body = params.to_json
  else
    request.body = params
  end

  response = @http.request(request)
  response
end

#post(path, params = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/oxford/face/http_client.rb', line 30

def post(path, params = {})
  request = Net::HTTP::Post.new("/face/v1.0#{path}")
  request.content_type = "application/json"
  request['Ocp-Apim-Subscription-Key'] = @api_key

  if params.is_a?(Hash)
    request.body = params.to_json
  else
    request.body = params
  end

  response = @http.request(request)
  response
end

#put(path, params = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/oxford/face/http_client.rb', line 45

def put(path, params = {})
  request = Net::HTTP::Put.new("/face/v1.0#{path}")
  request.content_type = "application/json"
  request['Ocp-Apim-Subscription-Key'] = @api_key

  if params.is_a?(Hash)
    request.body = params.to_json
  else
    request.body = params
  end

  response = @http.request(request)
  response
end