Class: LVS::JsonService::Base

Inherits:
Object
  • Object
show all
Includes:
Request
Defined in:
lib/lvs/json_service/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Methods included from Request

included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (protected)



232
233
234
235
236
237
238
239
240
241
242
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
# File 'lib/lvs/json_service/base.rb', line 232

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

#fieldsObject

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

.debug(message) ⇒ Object



75
76
77
# File 'lib/lvs/json_service/base.rb', line 75

def debug(message)
  LVS::JsonService::Logger.debug " \033[1;4;32mLVS::JsonService\033[0m #{message}"
end

.define_service(name, service, options = {}, &block) ⇒ Object



83
84
85
86
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
# File 'lib/lvs/json_service/base.rb', line 83

def define_service(name, service, options = {}, &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

  options[:encrypted]          = @encrypted if @encrypted
  options[:auth_cert]          = @auth_cert if @auth_cert
  options[:auth_key]           = @auth_key if @auth_key
  options[: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 ||= {}
    options[:defaults] ||= {}
    options[:defaults].each_pair do |key, value|
      method_params[key] = value if method_params[key].blank?
    end
    options[:required] ||= {}
    options[: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
    if block
      self.run_remote_request(@site + prefix + internal_service, method_params, options) do |result|
        if flags && flags[:raw]
          yield(result)
        else
          block.call(self.parse_result(result))
        end
      end
    else
      result = self.run_remote_request(@site + prefix + internal_service, method_params, options)
      if flags && flags[: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



133
134
135
136
137
138
# File 'lib/lvs/json_service/base.rb', line 133

def fake_service(name, json, options = {})
  (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_missingObject



71
72
73
# File 'lib/lvs/json_service/base.rb', line 71

def ignore_missing
  @ignore_missing
end

.ignore_missing=(value) ⇒ Object



67
68
69
# File 'lib/lvs/json_service/base.rb', line 67

def ignore_missing=(value)
  @ignore_missing = value
end

.parse_result(response) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/lvs/json_service/base.rb', line 144

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

Returns:

  • (Boolean)


79
80
81
# File 'lib/lvs/json_service/base.rb', line 79

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

.servicesObject



140
141
142
# File 'lib/lvs/json_service/base.rb', line 140

def services
  @services
end

.site=(value) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/lvs/json_service/base.rb', line 58

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