Class: LVS::JsonService::Base
- Inherits:
-
Object
- Object
- LVS::JsonService::Base
- Includes:
- Request
- Defined in:
- lib/lvs/json_service/base.rb
Instance Attribute Summary collapse
-
#fields ⇒ Object
Returns the value of attribute fields.
Class Method Summary collapse
- .add_service(service) ⇒ Object
- .agp_location=(value) ⇒ Object
- .auth_cert=(value) ⇒ Object
- .auth_key=(value) ⇒ Object
- .auth_key_pass=(value) ⇒ Object
- .caller_id=(value) ⇒ Object
- .debug(message) ⇒ Object
- .define_service(name, service, options = {}, &block) ⇒ Object
- .encrypted=(value) ⇒ Object
- .fake_service(name, json, options = {}) ⇒ Object
- .field_prefix=(value) ⇒ Object
- .ignore_missing ⇒ Object
- .ignore_missing=(value) ⇒ Object
- .parse_result(response) ⇒ Object
- .require_ssl? ⇒ Boolean
- .service_prefix=(value) ⇒ Object
- .services ⇒ Object
- .site=(value) ⇒ Object
Methods included from Request
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object (protected)
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/lvs/json_service/base.rb', line 243 def method_missing(name, *args) name = name.to_s if name == "respond_to?" # don't know why this hack is necessary, but it is at the moment... return respond_to?(args[0]) end if name == "to_s" # don't know why this hack is necessary, but it is at the moment... return to_s end key = name_to_key(name) value = value_for_key(key) if name =~ /=$/ @data[key] = ManuallySetData.new(args[0]) value = @data[key] elsif name =~ /\?$/ value = @data[name_to_key("has_#{key}")] !(value == 0 || value.blank?) elsif name =~ /^has_/ !(value == 0 || value.blank?) else if (value.is_a?(ManuallySetData)) value = value.data elsif (value.is_a?(Hash)) value = self.class.new(value) elsif (value.is_a?(Array)) value = value.collect {|v| if v.is_a?(Hash) or v.is_a?(Array) then self.class.new(v) else v end } elsif name =~ /date$/ value = Time.at(value/1000) end end if value.nil? if self.class.ignore_missing self.class.debug("Method #{name} with key #{key} called on #{self.class} but is non-existant, returned nil") return nil else raise NoMethodError.new("Method #{name} with key #{key} called on #{self.class} but is non-existant, returned nil") end end value end |
Instance Attribute Details
#fields ⇒ Object
Returns the value of attribute fields.
10 11 12 |
# File 'lib/lvs/json_service/base.rb', line 10 def fields @fields end |
Class Method Details
.add_service(service) ⇒ Object
45 46 47 48 |
# File 'lib/lvs/json_service/base.rb', line 45 def add_service(service) @services ||= [] @services = @services << service end |
.agp_location=(value) ⇒ Object
41 42 43 |
# File 'lib/lvs/json_service/base.rb', line 41 def agp_location=(value) @agp_location = value end |
.auth_cert=(value) ⇒ Object
29 30 31 |
# File 'lib/lvs/json_service/base.rb', line 29 def auth_cert=(value) @auth_cert = value end |
.auth_key=(value) ⇒ Object
33 34 35 |
# File 'lib/lvs/json_service/base.rb', line 33 def auth_key=(value) @auth_key = value end |
.auth_key_pass=(value) ⇒ Object
37 38 39 |
# File 'lib/lvs/json_service/base.rb', line 37 def auth_key_pass=(value) @auth_key_pass = value end |
.caller_id=(value) ⇒ Object
58 59 60 |
# File 'lib/lvs/json_service/base.rb', line 58 def caller_id=(value) @caller_id = value end |
.debug(message) ⇒ Object
79 80 81 |
# File 'lib/lvs/json_service/base.rb', line 79 def debug() LVS::JsonService::Logger.debug " \033[1;4;32mLVS::JsonService\033[0m #{}" end |
.define_service(name, service, options = {}, &block) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/lvs/json_service/base.rb', line 87 def define_service(name, service, = {}, &block) service_name = name service_path = service.split('.') if service_path.size <= 2 internal_service = service prefix = @service_prefix else internal_service = service_path[-2..-1].join('.') prefix = service_path[0..-3].join('.') + '.' end [:encrypted] = @encrypted if @encrypted [:caller_id] = @caller_id if @caller_id [:auth_cert] = @auth_cert if @auth_cert [:auth_key] = @auth_key if @auth_key [:auth_key_pass] = @auth_key_pass if @auth_key_pass (class<<self;self;end).send :define_method, service_name do |*args, &block| method_params, flags = args method_params ||= {} [:defaults] ||= {} [:defaults].each_pair do |key, value| method_params[key] = value if method_params[key].blank? end [:required] ||= {} [:required].each do |key| raise LVS::JsonService::Error.new("Required field #{key} wasn't supplied", internal_service, '0', method_params) if method_params[key].blank? end url = @site + prefix + internal_service if [:interpolate_url] method_params.each do |key, value| url.gsub!(":#{key.to_s}", value.to_s) end end if block self.run_remote_request(url, method_params, ) do |result| if (flags && flags[:raw]) || [:raw] yield(result) else block.call(self.parse_result(result)) end end else result = self.run_remote_request(url, method_params, ) if (flags && flags[:raw]) || [:raw] result else self.parse_result(result) end end end add_service(name) end |
.encrypted=(value) ⇒ Object
25 26 27 |
# File 'lib/lvs/json_service/base.rb', line 25 def encrypted=(value) @encrypted = value end |
.fake_service(name, json, options = {}) ⇒ Object
144 145 146 147 148 149 |
# File 'lib/lvs/json_service/base.rb', line 144 def fake_service(name, json, = {}) (class<<self;self;end).send :define_method, name do |*args| self.parse_result(JSON.parse(json)) end add_service(name) end |
.field_prefix=(value) ⇒ Object
54 55 56 |
# File 'lib/lvs/json_service/base.rb', line 54 def field_prefix=(value) @field_prefix = value end |
.ignore_missing ⇒ Object
75 76 77 |
# File 'lib/lvs/json_service/base.rb', line 75 def ignore_missing @ignore_missing end |
.ignore_missing=(value) ⇒ Object
71 72 73 |
# File 'lib/lvs/json_service/base.rb', line 71 def ignore_missing=(value) @ignore_missing = value end |
.parse_result(response) ⇒ Object
155 156 157 158 159 160 161 162 163 |
# File 'lib/lvs/json_service/base.rb', line 155 def parse_result(response) if response.is_a?(LVS::JsonService::Error) response elsif response.is_a?(Array) array = response.map { |x| self.new(x) } else self.new(response) end end |
.require_ssl? ⇒ Boolean
83 84 85 |
# File 'lib/lvs/json_service/base.rb', line 83 def require_ssl? (Module.const_defined?(:SSL_ENABLED) && SSL_ENABLED) || (Module.const_defined?(:SSL_DISABLED) && !SSL_DISABLED) end |
.service_prefix=(value) ⇒ Object
50 51 52 |
# File 'lib/lvs/json_service/base.rb', line 50 def service_prefix=(value) @service_prefix = value end |
.services ⇒ Object
151 152 153 |
# File 'lib/lvs/json_service/base.rb', line 151 def services @services end |
.site=(value) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/lvs/json_service/base.rb', line 62 def site=(value) # value is containing AGP_LOCATION already sometimes: value.gsub!(/^#{AGP_LOCATION}/, '') if defined?(AGP_LOCATION) && value.match(/#{AGP_LOCATION}/) agp = @agp_location ? @agp_location : AGP_LOCATION agp.gsub!(/\/$/, '') value.gsub!(/^\//, '') @site = (agp + '/' + value) end |