Module: CreateApiMethod

Included in:
Vimeo::Advanced::Base
Defined in:
lib/vimeo/advanced/base.rb

Instance Method Summary collapse

Instance Method Details

#create_api_method(method, vimeo_method, options = {}) ⇒ Object

Creates a method that calls a Vimeo Method through the Advanced API.

Parameters:

  • method (String)

    The name of the method being created.

  • vimeo_method (String)

    The name of the advanced API Method the function should call.

  • options (Hash) (defaults to: {})

    Some optional parameters.

Options Hash (options):

  • :required (Array)

    An array of required parameters.

  • :optional (Array)

    An array of optional parameters.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vimeo/advanced/base.rb', line 12

def create_api_method(method, vimeo_method, options={})
  options = { :required => [], :optional => [] }.merge(options)

  method = method.to_s
  camelized_method = camelize(method, false)

  raise ArgumentError, 'Required parameters must be an array.' unless options[:required].is_a? Array
  raise ArgumentError, 'Optional parameters must be an array.' unless options[:optional].is_a? Array

  required = options[:required].map { |r| r.to_s }.join(",")
  optional = options[:optional].map { |o| ":#{o} => nil" }.join(",")
  authorized = options.fetch(:authorized, true)

  parameters = "(#{required unless required.empty?}#{',' unless required.empty?}options={#{optional}})"

  method_string = <<-method

    def #{method}#{parameters}
      raise ArgumentError, 'Options must be a hash.' unless options.is_a? Hash

      sig_options = {
        :method => "#{vimeo_method}",
        :format => "json"
      }

      #{ options[:required].map { |r| "sig_options.merge! :#{r} => #{r}"}.join("\n") }
      #{ options[:optional].map { |o| "sig_options.merge! :#{o} => options[:#{o}] unless options[:#{o}].nil?" }.join("\n") }

      make_request sig_options, #{authorized ? "true" : "false"}
    end

    alias #{camelized_method} #{method}

  method

  class_eval method_string
end