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)



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/lvs/json_service/base.rb', line 165

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
  key = name_to_key(name)
  value = @data[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} called on #{self.class} but is non-existant, returned nil")
      return nil
    else
      raise NoMethodError.new("Method #{name} 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 = {}) ⇒ 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
# File 'lib/lvs/json_service/base.rb', line 83

def define_service(name, service, options = {})
  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|
    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
    result = self.run_remote_request(@site + prefix + internal_service, method_params, options)
    if flags && flags[:raw]
      result
    else
      self.parse_result(result)
    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



123
124
125
126
127
128
# File 'lib/lvs/json_service/base.rb', line 123

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



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

def parse_result(response)
  if 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



130
131
132
# File 'lib/lvs/json_service/base.rb', line 130

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