Class: Speechmatics::API
- Inherits:
-
Object
- Object
- Speechmatics::API
- Includes:
- Connection
- Defined in:
- lib/speechmatics/api.rb
Direct Known Subclasses
Constant Summary
Constants included from Connection
Instance Attribute Summary collapse
-
#current_options ⇒ Object
Returns the value of attribute current_options.
Instance Method Summary collapse
- #apply_options(options = {}) ⇒ Object
- #args_to_options(args) ⇒ Object
- #base_path ⇒ Object
- #create(params = {}) ⇒ Object
- #delete(params = {}) ⇒ Object
- #get(params = {}) ⇒ Object
-
#initialize(options = {}) {|_self| ... } ⇒ API
constructor
A new instance of API.
- #list(params = {}) ⇒ Object
-
#request(method, path, params = {}) ⇒ Object
:nodoc:.
- #update(params = {}) ⇒ Object
Methods included from Connection
#connection, #merge_default_options
Constructor Details
#initialize(options = {}) {|_self| ... } ⇒ API
Returns a new instance of API.
21 22 23 24 |
# File 'lib/speechmatics/api.rb', line 21 def initialize(={}, &block) () yield(self) if block_given? end |
Instance Attribute Details
#current_options ⇒ Object
Returns the value of attribute current_options.
10 11 12 |
# File 'lib/speechmatics/api.rb', line 10 def @current_options end |
Instance Method Details
#apply_options(options = {}) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/speechmatics/api.rb', line 26 def (={}) self. ||= ActiveSupport::HashWithIndifferentAccess.new(Speechmatics.) self. = .merge(()) Configuration.keys.each do |key| send("#{key}=", [key]) end end |
#args_to_options(args) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/speechmatics/api.rb', line 91 def (args) if args.is_a?(String) || args.is_a?(Symbol) || args.is_a?(Numeric) {"#{self.class.name.demodulize.downcase.singularize}_id" => args.to_s} elsif args.is_a?(Hash) args end end |
#base_path ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/speechmatics/api.rb', line 54 def base_path parts = self.class.name.split("::").inject([]){|a, c| if c != 'Speechmatics' base = c.underscore a << base.tr('_','-') a << ["#{base.singularize}_id"] if ["#{base.singularize}_id"] end a } parts.join('/') + '/' end |
#create(params = {}) ⇒ Object
76 77 78 79 |
# File 'lib/speechmatics/api.rb', line 76 def create(params={}) self. = .merge((params)) request(:post, base_path, {data: params}) end |
#delete(params = {}) ⇒ Object
86 87 88 89 |
# File 'lib/speechmatics/api.rb', line 86 def delete(params={}) self. = .merge((params)) request(:delete, base_path) end |
#get(params = {}) ⇒ Object
71 72 73 74 |
# File 'lib/speechmatics/api.rb', line 71 def get(params={}) self. = .merge((params)) request(:get, base_path) end |
#list(params = {}) ⇒ Object
66 67 68 69 |
# File 'lib/speechmatics/api.rb', line 66 def list(params={}) self. = .merge((params)) request(:get, base_path) end |
#request(method, path, params = {}) ⇒ Object
:nodoc:
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/speechmatics/api.rb', line 34 def request(method, path, params={}) # :nodoc: unless (method && [:get, :post, :put, :patch, :delete].include?(method)) raise ArgumentError, "whoops, that isn't a valid http method: #{method}" end conn = connection((params[:options] || {}).merge()) request_path = (conn.path_prefix + '/' + path).gsub(/\/+/, '/') response = conn.send(method) do |request| case method.to_sym when :get, :delete request.url(request_path, params) when :post, :put request.path = request_path request.body = params[:data] end end Speechmatics::Response.parse(response, {api: self, method: method, path: path, params: params}) end |
#update(params = {}) ⇒ Object
81 82 83 84 |
# File 'lib/speechmatics/api.rb', line 81 def update(params={}) self. = .merge((params)) request(:put, base_path, {data: params}) end |