Module: SchoolFriend::APIMethods::ClassMethods

Defined in:
lib/school_friend/api_methods.rb

Instance Method Summary collapse

Instance Method Details

#api_method(*methods) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/school_friend/api_methods.rb', line 11

def api_method(*methods)
  options = methods.last.is_a?(Hash) ? methods.pop : {}

  namespace    = name.sub(/^.+::/, '')
  namespace[0] = namespace[0].downcase

  methods.each do |method|
    call  = "session.api_call('#{namespace}.#{method.to_s.gsub(/_([a-z])/) { $1.upcase }}', params"
    call += options[:session_only] ? ', true)' : ')'

    class_eval <<-OES, __FILE__, __LINE__ + 1
      def #{method}(params = {})                          # def get_widgets(params = {})
        response = #{call}                                #   response = session.api_call('widget.getWidgets', params, true)
        if response.is_a?(Net::HTTPSuccess)               #   if response.is_a?(Net::HTTPSuccess)
          JSON.parse(response.body, :quirks_mode => true) #     JSON(response.body, :quirks_mode => true)
        else                                              #   else
          response.error!                                 #     response.error!
        end                                               #   end
      end                                                 # end
    OES
  end
end