Class: Amara::API
Direct Known Subclasses
Activity, Client, Languages, Message, Path, Teams, Teams::Applications, Teams::Members, Teams::Projects, Teams::SafeMembers, Teams::Tasks, Users, Videos, Videos::Languages, Videos::Languages::Subtitles, Videos::Urls
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.
21
22
23
24
|
# File 'lib/amara/api.rb', line 21
def initialize(options={}, &block)
apply_options(options)
yield(self) if block_given?
end
|
Instance Attribute Details
#current_options ⇒ Object
Returns the value of attribute current_options.
10
11
12
|
# File 'lib/amara/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/amara/api.rb', line 26
def apply_options(options={})
self.current_options ||= ActiveSupport::HashWithIndifferentAccess.new(Amara.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
138
139
140
141
142
143
144
|
# File 'lib/amara/api.rb', line 138
def args_to_options(args)
params = if args.is_a?(String) || args.is_a?(Symbol)
{"#{self.class.name.demodulize.downcase.singularize}_id" => args}
elsif args.is_a?(Hash)
args
end
end
|
#base_path ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/amara/api.rb', line 77
def base_path
parts = self.class.name.split("::").inject([]){|a, c|
if c != 'Amara'
base = c.underscore
a << base.tr('_','-')
a << current_options["#{base.singularize}_id"] if current_options["#{base.singularize}_id"]
end
a
}
parts.join('/')
end
|
#check_for_error(response) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/amara/api.rb', line 59
def check_for_error(response)
status_code_type = response.status.to_s[0]
case status_code_type
when "2"
when "4"
if response.status == 404
raise NotFoundError.new("Resource not found", response)
else
raise ClientError.new("Whoops, error back from Amara: #{response.status}", response)
end
when "5"
raise ServerError.new("Whoops, error back from Amara: #{response.status}", response)
else
raise UnknownError.new("Unrecongized status code: #{response.status}", response)
end
end
|
#create(params = {}) ⇒ Object
111
112
113
114
|
# File 'lib/amara/api.rb', line 111
def create(params={})
self.current_options = current_options.merge(args_to_options(params))
request(:post, base_path, {data: params})
end
|
#create!(params = {}) ⇒ Object
116
117
118
|
# File 'lib/amara/api.rb', line 116
def create!(params={})
create(force_raise_errors(params))
end
|
#delete(params = {}) ⇒ Object
129
130
131
132
|
# File 'lib/amara/api.rb', line 129
def delete(params={})
self.current_options = current_options.merge(args_to_options(params))
request(:delete, base_path)
end
|
#delete!(params = {}) ⇒ Object
134
135
136
|
# File 'lib/amara/api.rb', line 134
def delete!(params={})
delete(force_raise_errors(params))
end
|
#force_raise_errors(params) ⇒ Object
146
147
148
149
150
151
|
# File 'lib/amara/api.rb', line 146
def force_raise_errors(params)
(params || {}).with_indifferent_access.tap do |p|
p[:options] = ActiveSupport::HashWithIndifferentAccess.new(p[:options])
p[:options][:raise_errors] = true
end
end
|
#get(params = {}) ⇒ Object
102
103
104
105
|
# File 'lib/amara/api.rb', line 102
def get(params={})
self.current_options = current_options.merge(args_to_options(params))
request(:get, base_path)
end
|
#get!(params = {}) ⇒ Object
107
108
109
|
# File 'lib/amara/api.rb', line 107
def get!(params={})
get(force_raise_errors(params))
end
|
#list(params = {}) ⇒ Object
93
94
95
96
|
# File 'lib/amara/api.rb', line 93
def list(params={})
self.current_options = current_options.merge(args_to_options(params))
request(:get, base_path, paginate(params))
end
|
#list!(params = {}) ⇒ Object
98
99
100
|
# File 'lib/amara/api.rb', line 98
def list!(params={})
list(force_raise_errors(params))
end
|
#paginate(params = {}) ⇒ Object
89
90
91
|
# File 'lib/amara/api.rb', line 89
def paginate(params={})
params.reverse_merge(limit: 20, offset: 0)
end
|
#request(method, path, params = {}) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/amara/api.rb', line 34
def request(method, path, params={}) unless (method && [:get, :post, :put, :patch, :delete].include?(method))
raise ArgumentError, "whoops, that isn't a valid http method: #{method}"
end
options = current_options.merge(params[:options] || {})
request_params = params.except(:options)
conn = connection(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, request_params)
when :post, :put
request.path = request_path
request.body = request_params[:data] ? request_params[:data].to_json : nil
end
end
amara_response = Amara::Response.new(response, { api: self, method: method, path: path, params: params } )
check_for_error(response) if options[:raise_errors]
amara_response
end
|
#update(params = {}) ⇒ Object
120
121
122
123
|
# File 'lib/amara/api.rb', line 120
def update(params={})
self.current_options = current_options.merge(args_to_options(params))
request(:put, base_path, {data: params})
end
|
#update!(params = {}) ⇒ Object
125
126
127
|
# File 'lib/amara/api.rb', line 125
def update!(params={})
update(force_raise_errors(params))
end
|