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(*args) ⇒ Object (protected)



180
181
182
183
# File 'lib/lvs/json_service/base.rb', line 180

def method_missing(*args)
  self.class.debug("Method #{args[0]} called on #{self.class} but is non-existant, returned default FALSE")
  super
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



44
45
46
47
# File 'lib/lvs/json_service/base.rb', line 44

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

.agp_location=(value) ⇒ Object



40
41
42
# File 'lib/lvs/json_service/base.rb', line 40

def agp_location=(value)
  @agp_location = value
end

.auth_cert=(value) ⇒ Object



28
29
30
# File 'lib/lvs/json_service/base.rb', line 28

def auth_cert=(value)
  @auth_cert = value
end

.auth_key=(value) ⇒ Object



32
33
34
# File 'lib/lvs/json_service/base.rb', line 32

def auth_key=(value)
  @auth_key = value
end

.auth_key_pass=(value) ⇒ Object



36
37
38
# File 'lib/lvs/json_service/base.rb', line 36

def auth_key_pass=(value)
  @auth_key_pass = value
end

.debug(message) ⇒ Object



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

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

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



74
75
76
77
78
79
80
81
82
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
# File 'lib/lvs/json_service/base.rb', line 74

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



24
25
26
# File 'lib/lvs/json_service/base.rb', line 24

def encrypted=(value)
  @encrypted = value
end

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



114
115
116
117
118
119
# File 'lib/lvs/json_service/base.rb', line 114

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



53
54
55
# File 'lib/lvs/json_service/base.rb', line 53

def field_prefix=(value)
  @field_prefix = value
end

.parse_result(response) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/lvs/json_service/base.rb', line 125

def parse_result(response)
  if response.is_a?(Array)
    response.map { |x| self.new(x) }
  else
    self.new(response)
  end
end

.require_ssl?Boolean

Returns:

  • (Boolean)


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

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

.service_prefix=(value) ⇒ Object



49
50
51
# File 'lib/lvs/json_service/base.rb', line 49

def service_prefix=(value)
  @service_prefix = value
end

.servicesObject



121
122
123
# File 'lib/lvs/json_service/base.rb', line 121

def services
  @services
end

.site=(value) ⇒ Object



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

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