Class: ProjectoxfordSpid::RestAPI
- Inherits:
-
Object
- Object
- ProjectoxfordSpid::RestAPI
- Defined in:
- lib/projectoxford_spid.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
Returns the value of attribute api.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#rest ⇒ Object
Returns the value of attribute rest.
-
#subscription_key ⇒ Object
Returns the value of attribute subscription_key.
-
#url ⇒ Object
Returns the value of attribute url.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
-
#create_enrollment(identification_profile_id, file_location, options = {}) ⇒ Object
Usage: client.create_enrollment(id, file_url, {default: “true”, channel: “left”, gc: “true”, remove_silence: “wav”, gc: “true”}).
-
#create_profile(params = {}) ⇒ Object
Profile.
- #delete_profile(identification_profile_id) ⇒ Object
- #get_operation_status(operation_id) ⇒ Object
- #get_profile(identification_profile_id) ⇒ Object
- #get_profiles(params = {}) ⇒ Object
- #hash_to_params(myhash) ⇒ Object
-
#identification(identification_profile_ids, file_location, options = {}) ⇒ Object
Usage: identification([“id1”, “id2”], “file_url”, {channel: “left”, gc: “true”, default: “true”, remove_silence: “wav”, gc: “true”}).
-
#initialize(subscription_key, url = "https://api.projectoxford.ai/spid", version = "v1.0") ⇒ RestAPI
constructor
A new instance of RestAPI.
- #request(method, path, params = {}, content_type = nil, options = {}) ⇒ Object
- #reset_enrollment(identification_profile_id) ⇒ Object
- #verification(verification_profile_id, file_location, options = {}) ⇒ Object
Constructor Details
#initialize(subscription_key, url = "https://api.projectoxford.ai/spid", version = "v1.0") ⇒ RestAPI
Returns a new instance of RestAPI.
14 15 16 17 18 19 20 21 |
# File 'lib/projectoxford_spid.rb', line 14 def initialize(subscription_key, url="https://api.projectoxford.ai/spid", version="v1.0") @subscription_key = subscription_key @url = url.chomp('/') @version = version @api = @url + '/' + @version @headers = {"Ocp-Apim-Subscription-Key" => @subscription_key} @rest = RestClient::Resource.new(@api, headers: @headers) end |
Instance Attribute Details
#api ⇒ Object
Returns the value of attribute api.
12 13 14 |
# File 'lib/projectoxford_spid.rb', line 12 def api @api end |
#headers ⇒ Object
Returns the value of attribute headers.
12 13 14 |
# File 'lib/projectoxford_spid.rb', line 12 def headers @headers end |
#rest ⇒ Object
Returns the value of attribute rest.
12 13 14 |
# File 'lib/projectoxford_spid.rb', line 12 def rest @rest end |
#subscription_key ⇒ Object
Returns the value of attribute subscription_key.
12 13 14 |
# File 'lib/projectoxford_spid.rb', line 12 def subscription_key @subscription_key end |
#url ⇒ Object
Returns the value of attribute url.
12 13 14 |
# File 'lib/projectoxford_spid.rb', line 12 def url @url end |
#version ⇒ Object
Returns the value of attribute version.
12 13 14 |
# File 'lib/projectoxford_spid.rb', line 12 def version @version end |
Instance Method Details
#create_enrollment(identification_profile_id, file_location, options = {}) ⇒ Object
Usage: client.create_enrollment(id, file_url, {default: “true”, channel: “left”, gc: “true”, remove_silence: “wav”, gc: “true”})
125 126 127 128 |
# File 'lib/projectoxford_spid.rb', line 125 def create_enrollment(identification_profile_id, file_location, ={}) [:file] = open(file_location) return request("POST", "/identificationProfiles/#{identification_profile_id}/enroll/", {}, "multipart/form-data", ) end |
#create_profile(params = {}) ⇒ Object
Profile
107 108 109 110 |
# File 'lib/projectoxford_spid.rb', line 107 def create_profile(params={}) params["locale"] = "en-us" unless params["locale"] return request("POST", "/identificationProfiles/", params, "application/json") end |
#delete_profile(identification_profile_id) ⇒ Object
112 113 114 |
# File 'lib/projectoxford_spid.rb', line 112 def delete_profile(identification_profile_id) return request("DELETE", "/identificationProfiles/#{identification_profile_id}/") end |
#get_operation_status(operation_id) ⇒ Object
134 135 136 |
# File 'lib/projectoxford_spid.rb', line 134 def get_operation_status(operation_id) return request("GET", "/operations/#{operation_id}/") end |
#get_profile(identification_profile_id) ⇒ Object
120 121 122 |
# File 'lib/projectoxford_spid.rb', line 120 def get_profile(identification_profile_id) return request("GET", "/identificationProfiles/#{identification_profile_id}/") end |
#get_profiles(params = {}) ⇒ Object
116 117 118 |
# File 'lib/projectoxford_spid.rb', line 116 def get_profiles(params={}) return request("GET", "/identificationProfiles/") end |
#hash_to_params(myhash) ⇒ Object
23 24 25 |
# File 'lib/projectoxford_spid.rb', line 23 def hash_to_params(myhash) return myhash.map { |k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join("&") end |
#identification(identification_profile_ids, file_location, options = {}) ⇒ Object
Usage: identification([“id1”, “id2”], “file_url”, {channel: “left”, gc: “true”, default: “true”, remove_silence: “wav”, gc: “true”})
139 140 141 142 143 |
# File 'lib/projectoxford_spid.rb', line 139 def identification(identification_profile_ids, file_location, ={}) [:file] = open(file_location) identification_profile_ids = identification_profile_ids.join(",") return request("POST", "/identify?identificationProfileIds=#{identification_profile_ids}") end |
#request(method, path, params = {}, content_type = nil, options = {}) ⇒ Object
27 28 29 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 57 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/projectoxford_spid.rb', line 27 def request(method, path, params={}, content_type=nil, ={}) # Set content_type and accept header content_type ? content_type = {:content_type => content_type, :accept => :json} : content_type = {:accept => :json} case method when "POST" # set content to empty hash if its params == {} content = params.to_json if [:file] # content set to file when posting a file file = [:file] # add params to path if posting a file # path = path + "?:"+ hash_to_params(params) if params.any? # set content to binary wav file file = AudioHero::Sox.new(file).convert([:convert]) if [:convert] file = AudioHero::Sox.new(file).remove_silence([:remove_silence]) if [:remove_silence] content = file end begin r = @rest[path].post content, content_type rescue => e response = e end file.close! # close temp file if response # if error code = response.http_code response = JSON.parse(response.response.to_s) else # if success code = r.code r.to_s == "" ? raw = "{}" : raw = r.to_s response = JSON.parse(raw) response[:operation_location] = r.headers[:operation_location] if r.headers[:operation_location] end return [code, response] when "GET" # add params to path path = path + "?" + hash_to_params(params) if params.any? begin r = @rest[path].get(:accept => :json) rescue => e response = e end if response code = response.http_code response = JSON.parse(response.response.to_s) else code = r.code r.to_s == "" ? raw = "{}" : raw = r.to_s response = JSON.parse(raw) end return [code, response] when "DELETE" # add params to path path = path + "?" + hash_to_params(params) if params.any? begin r = @rest[path].delete(:accept => :json) rescue => e response = e end if response code = response.http_code response.response.to_s == "" ? raw = "{}" : raw = response.response.to_s response = JSON.parse(raw) return [code, response] else code = r.code return [code, ""] end end return [405, "Method Not Supported"] end |
#reset_enrollment(identification_profile_id) ⇒ Object
130 131 132 |
# File 'lib/projectoxford_spid.rb', line 130 def reset_enrollment(identification_profile_id) return request("POST", "/identificationProfiles/#{identification_profile_id}/reset/") end |
#verification(verification_profile_id, file_location, options = {}) ⇒ Object
145 146 147 148 |
# File 'lib/projectoxford_spid.rb', line 145 def verification(verification_profile_id, file_location, ={}) [:file] = open(file_location) return request("POST", "/verify?verificationProfileId=#{verification_profile_id}") end |