Class: Echonest::ApiMethods::Base
- Inherits:
-
Object
- Object
- Echonest::ApiMethods::Base
- Defined in:
- lib/echonest/api.rb
Class Method Summary collapse
- .build_params_with_validation(args, required, required_any, option) ⇒ Object
- .method_with_option(id, option, &block) ⇒ Object
- .method_with_required_any(category, method_id, http_method, required, required_any, option, proc, block = nil) ⇒ Object
- .validator(required, required_any, option) ⇒ Object
Instance Method Summary collapse
-
#initialize(api) ⇒ Base
constructor
A new instance of Base.
- #request(*args) ⇒ Object
Constructor Details
#initialize(api) ⇒ Base
Returns a new instance of Base.
133 134 135 |
# File 'lib/echonest/api.rb', line 133 def initialize(api) @api = api end |
Class Method Details
.build_params_with_validation(args, required, required_any, option) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/echonest/api.rb', line 189 def build_params_with_validation(args, required, required_any, option) = {} # api_key is common parameter. required -= %w[api_key] required.each do |name| name = name.to_sym raise ArgumentError.new("%s is required" % name) unless args[:required][name] [name] = args[:required][name] end if required_any.length > 0 unless required_any.inject(false){|r,i| r || args[:required_any].include?(i.to_sym)} raise ArgumentError.new("%s is required" % required_any.join(' or ')) end key = required_any.find {|name| args[:required_any].include?(name.to_sym)} [key.to_sym] = args[:required_any][key.to_sym] if key end if args[:option] && !args[:option].empty? option.each do |name| name = name.to_sym [name] = args[:option][name] if args[:option][name] end end end |
.method_with_option(id, option, &block) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/echonest/api.rb', line 167 def method_with_option(id, option, &block) unless block block = Proc.new {|response| response.body} end required = %w[] required_any = %w[] method = :request http_method = :get define_method(id) do |*args| name = "#{self.class.to_s.split('::')[-1].downcase}/#{id.to_s}" block.call(send(method, name, http_method, ApiMethods::Base.validator(required, required_any, option).call( :option => args.length > 0 ? args[0] : {}))) end end |
.method_with_required_any(category, method_id, http_method, required, required_any, option, proc, block = nil) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/echonest/api.rb', line 143 def method_with_required_any(category, method_id, http_method, required, required_any, option, proc, block=nil) unless block block = Proc.new {|response| response.body} end method = :request required ||= %w[api_key] define_method(method_id) do |*args| name = "#{category.downcase}/#{method_id.to_s}" if args.length > 0 param_required = {} required.each do |k| k = k.to_sym param_required[k] = args[0].delete(k) if args[0][k] end param_option = args[0] end params = ApiMethods::Base.validator(required, required_any, option).call( :required => param_required, :required_any => proc.call(self), :option => param_option) block.call(send(method, name, http_method, params)) end end |
.validator(required, required_any, option) ⇒ Object
183 184 185 186 187 |
# File 'lib/echonest/api.rb', line 183 def validator(required, required_any, option) Proc.new do |args| ApiMethods::Base.build_params_with_validation(args, required, required_any, option) end end |
Instance Method Details
#request(*args) ⇒ Object
137 138 139 140 |
# File 'lib/echonest/api.rb', line 137 def request(*args) name, http_method, params = args @api.request(name, http_method, params) end |