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)



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
271
272
273
274
275
276
277
# File 'lib/lvs/json_service/base.rb', line 239

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



46
47
48
49
# File 'lib/lvs/json_service/base.rb', line 46

def add_service(service)
  @services ||= []
  @services = @services << service
end

.agp_location=(value) ⇒ Object



42
43
44
# File 'lib/lvs/json_service/base.rb', line 42

def agp_location=(value)
  @agp_location = value
end

.auth_cert=(value) ⇒ Object



30
31
32
# File 'lib/lvs/json_service/base.rb', line 30

def auth_cert=(value)
  @auth_cert = value
end

.auth_key=(value) ⇒ Object



34
35
36
# File 'lib/lvs/json_service/base.rb', line 34

def auth_key=(value)
  @auth_key = value
end

.auth_key_pass=(value) ⇒ Object



38
39
40
# File 'lib/lvs/json_service/base.rb', line 38

def auth_key_pass=(value)
  @auth_key_pass = value
end

.debug(message) ⇒ Object



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

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

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



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

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[:eventmachine_async] = @eventmachine_async if @eventmachine_async
  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)
      puts "In service call non-block"
      if flags && flags[:raw]
        result
      else
        self.parse_result(result)
      end
    end
  end

  add_service(name)
end

.encrypted=(value) ⇒ Object



26
27
28
# File 'lib/lvs/json_service/base.rb', line 26

def encrypted=(value)
  @encrypted = value
end

.fake_service(name, json, options = {}) ⇒ Object



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

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



55
56
57
# File 'lib/lvs/json_service/base.rb', line 55

def field_prefix=(value)
  @field_prefix = value
end

.ignore_missingObject



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

def ignore_missing
  @ignore_missing
end

.ignore_missing=(value) ⇒ Object



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

def ignore_missing=(value)
  @ignore_missing = value
end

.is_eventmachine_async!Object



59
60
61
# File 'lib/lvs/json_service/base.rb', line 59

def is_eventmachine_async!
  @eventmachine_async = true
end

.parse_result(response) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/lvs/json_service/base.rb', line 151

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)


84
85
86
# File 'lib/lvs/json_service/base.rb', line 84

def require_ssl?
  (Module.const_defined?(:SSL_ENABLED) && SSL_ENABLED) || (Module.const_defined?(:SSL_DISABLED) && !SSL_DISABLED)
end

.service_prefix=(value) ⇒ Object



51
52
53
# File 'lib/lvs/json_service/base.rb', line 51

def service_prefix=(value)
  @service_prefix = value
end

.servicesObject



147
148
149
# File 'lib/lvs/json_service/base.rb', line 147

def services
  @services
end

.site=(value) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/lvs/json_service/base.rb', line 63

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