Class: ExtDirect::Api
- Inherits:
-
Object
- Object
- ExtDirect::Api
- Defined in:
- lib/ext_direct/api.rb
Class Method Summary collapse
-
.expose(class_to_expose, options = {}) ⇒ Hash
Expose methods from a class and his ancestor, if class was inherited.
-
.expose_all(class_dir) ⇒ Object
Expose all classes in a directory(conviniance method).
-
.exposed_api(show_parameters = false) ⇒ Hash
Exposed API data.
-
.router_url ⇒ String
Router url.
- .router_url=(url = '/router') ⇒ Object
-
.to_json ⇒ String
Return all exposed classes as a JSON String.
-
.to_raw ⇒ Hash
Return a Hash of all exposed classes.
Class Method Details
.expose(class_to_expose, options = {}) ⇒ Hash
Expose methods from a class and his ancestor, if class was inherited
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 49 50 51 52 53 54 55 |
# File 'lib/ext_direct/api.rb', line 16 def self.expose(class_to_expose, = {}) @exposed_api_raw = {} if @exposed_api_raw.nil? methods = [] if .include?(:only) raw_methods = [:only] || [] else raw_methods = class_to_expose.instance_methods(false) - ([:except] || []) raw_methods += class_to_expose.methods(false) - ([:except] || []) raw_methods += class_to_expose.ancestors[1].methods(false) - ([:except] || []) raw_methods += class_to_expose.ancestors[1].instance_methods(false) - ([:except] || []) end raw_methods.uniq! raw_methods.each do |m| name = m parameters = [] method_to_run = nil if class_to_expose.methods(false).include?(m) method_to_run = class_to_expose.method(m) elsif class_to_expose.instance_methods(false).include?(m) method_to_run = class_to_expose.instance_method(m) elsif class_to_expose.ancestors[1].methods(false).include?(m) method_to_run = class_to_expose.ancestors[1].method(m) elsif class_to_expose.ancestors[1].instance_methods(false).include?(m) method_to_run = class_to_expose.ancestors[1].instance_method(m) end unless method_to_run.nil? method_to_run.parameters.each do |p| parameters << {:name => p[1].to_s, :optional => p[0] == :opt} end end methods << {:name => name, :parameters => parameters} end @exposed_api_raw.store(class_to_expose.name, methods) end |
.expose_all(class_dir) ⇒ Object
Expose all classes in a directory(conviniance method)
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ext_direct/api.rb', line 61 def self.expose_all(class_dir) @exposed_api_raw = {} Dir.glob("#{class_dir}/**/*.rb").each do |r| rr = r.split("#{class_dir}/")[1].gsub('.rb','') puts "#{class_dir}/#{rr}" require "#{class_dir}/#{rr}" klass = self.class.const_get(rr.split('_').map{|c| c.capitalize}.join('')) self.expose klass end end |
.exposed_api(show_parameters = false) ⇒ Hash
Returns exposed API data.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/ext_direct/api.rb', line 114 def self.exposed_api(show_parameters = false) result = @exposed_api_raw unless show_parameters result = {} @exposed_api_raw.each do |k,v| methods = [] v.each do |method| methods << {:name => method[:name], :len => method[:parameters].size} end result.store(k,methods) end end result end |
.router_url ⇒ String
Returns router url.
105 106 107 |
# File 'lib/ext_direct/api.rb', line 105 def self.router_url @router_url end |
.router_url=(url = '/router') ⇒ Object
97 98 99 |
# File 'lib/ext_direct/api.rb', line 97 def self.router_url=(url = '/router') @router_url = url end |
.to_json ⇒ String
Return all exposed classes as a JSON String
78 79 80 81 |
# File 'lib/ext_direct/api.rb', line 78 def self.to_json api = self.to_raw "REMOTING_API = #{api.to_json};" end |
.to_raw ⇒ Hash
Return a Hash of all exposed classes
87 88 89 90 91 92 |
# File 'lib/ext_direct/api.rb', line 87 def self.to_raw api = {:url => @router_url || '/router', :type => 'remoting', :actions => self.exposed_api} end |