Class: Speechmatics::API

Inherits:
Object
  • Object
show all
Includes:
Connection
Defined in:
lib/speechmatics/api.rb

Direct Known Subclasses

Client, User, User::Jobs

Constant Summary

Constants included from Connection

Connection::ALLOWED_OPTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Connection

#connection, #merge_default_options

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ API

Returns a new instance of API.

Yields:

  • (_self)

Yield Parameters:



21
22
23
24
# File 'lib/speechmatics/api.rb', line 21

def initialize(options={}, &block)
  apply_options(options)
  yield(self) if block_given?
end

Instance Attribute Details

#current_optionsObject

Returns the value of attribute current_options.



10
11
12
# File 'lib/speechmatics/api.rb', line 10

def current_options
  @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 apply_options(options={})
  self.current_options ||= ActiveSupport::HashWithIndifferentAccess.new(Speechmatics.options)
  self.current_options = current_options.merge(args_to_options(options))
  Configuration.keys.each do |key|
    send("#{key}=", current_options[key])
  end
end

#args_to_options(args) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/speechmatics/api.rb', line 91

def args_to_options(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_pathObject



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 << current_options["#{base.singularize}_id"] if current_options["#{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.current_options = current_options.merge(args_to_options(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.current_options = current_options.merge(args_to_options(params))
  request(:delete, base_path)
end

#get(params = {}) ⇒ Object



71
72
73
74
# File 'lib/speechmatics/api.rb', line 71

def get(params={})
  self.current_options = current_options.merge(args_to_options(params))
  request(:get, base_path)
end

#list(params = {}) ⇒ Object



66
67
68
69
# File 'lib/speechmatics/api.rb', line 66

def list(params={})
  self.current_options = current_options.merge(args_to_options(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(current_options))
  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.current_options = current_options.merge(args_to_options(params))
  request(:put, base_path, {data: params})
end