Class: OpenX::Services::Base
- Inherits:
-
Object
- Object
- OpenX::Services::Base
show all
- Includes:
- Comparable
- Defined in:
- lib/openx/services/base.rb
Constant Summary
collapse
- CONFIGURATION_YAML =
File.join(ENV['HOME'], '.openx', 'credentials.yml')
- @@connection =
nil
- @@configuration =
nil
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(params = {}) ⇒ Base
Returns a new instance of Base.
89
90
91
92
93
94
|
# File 'lib/openx/services/base.rb', line 89
def initialize(params = {})
@id = nil
params.each { |k,v| send(:"#{k}=", v) }
@server = XMLRPC::Client.new2("#{self.class.connection.url}#{self.class.endpoint}")
end
|
Class Attribute Details
.create ⇒ Object
Returns the value of attribute create.
15
16
17
|
# File 'lib/openx/services/base.rb', line 15
def create
@create
end
|
.delete ⇒ Object
Returns the value of attribute delete.
15
16
17
|
# File 'lib/openx/services/base.rb', line 15
def delete
@delete
end
|
.endpoint ⇒ Object
Returns the value of attribute endpoint.
14
15
16
|
# File 'lib/openx/services/base.rb', line 14
def endpoint
@endpoint
end
|
.find_all ⇒ Object
Returns the value of attribute find_all.
15
16
17
|
# File 'lib/openx/services/base.rb', line 15
def find_all
@find_all
end
|
.find_one ⇒ Object
Returns the value of attribute find_one.
15
16
17
|
# File 'lib/openx/services/base.rb', line 15
def find_one
@find_one
end
|
.translations ⇒ Object
Returns the value of attribute translations.
14
15
16
|
# File 'lib/openx/services/base.rb', line 14
def translations
@translations
end
|
.update ⇒ Object
Returns the value of attribute update.
15
16
17
|
# File 'lib/openx/services/base.rb', line 15
def update
@update
end
|
Class Method Details
.configuration ⇒ Object
17
18
19
20
|
# File 'lib/openx/services/base.rb', line 17
def configuration
@@configuration ||=
YAML.load_file(CONFIGURATION_YAML)[ENV['OPENX_ENV'] || 'production']
end
|
.configuration=(c) ⇒ Object
22
|
# File 'lib/openx/services/base.rb', line 22
def configuration=(c); @@configuration = c; end
|
.connection ⇒ Object
28
29
30
31
32
33
34
35
36
|
# File 'lib/openx/services/base.rb', line 28
def connection
unless @@connection
@@connection = Session.new(configuration['url'])
@@connection.create( configuration['username'],
configuration['password']
)
end
@@connection
end
|
.connection=(c) ⇒ Object
24
25
26
|
# File 'lib/openx/services/base.rb', line 24
def connection= c
@@connection = c
end
|
.create!(params = {}) ⇒ Object
38
39
40
|
# File 'lib/openx/services/base.rb', line 38
def create!(params = {})
new(params).save!
end
|
.destroy(id) ⇒ Object
75
76
77
|
# File 'lib/openx/services/base.rb', line 75
def destroy(id)
new(:id => id).destroy
end
|
.find(id, *args) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/openx/services/base.rb', line 61
def find(id, *args)
session = self.connection
server = XMLRPC::Client.new2("#{session.url}#{endpoint}")
if id == :all
responses = server.call(find_all(), session.id, *args)
responses.map { |response|
new(translate(response))
}
else
response = server.call(find_one(), session.id, id)
new(translate(response))
end
end
|
.has_one(*things) ⇒ Object
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/openx/services/base.rb', line 50
def has_one(*things)
things.each do |thing|
attr_writer :"#{thing}"
define_method(:"#{thing}") do
klass = thing.to_s.capitalize.gsub(/_[a-z]/) { |m| m[1].chr.upcase }
klass = OpenX::Services.const_get(:"#{klass}")
klass.find(send("#{thing}_id"))
end
end
end
|
.openx_accessor(accessor_map) ⇒ Object
42
43
44
45
46
47
48
|
# File 'lib/openx/services/base.rb', line 42
def openx_accessor(accessor_map)
@translations ||= {}
@translations = accessor_map.merge(@translations)
accessor_map.each do |ruby,openx|
attr_accessor :"#{ruby}"
end
end
|
Instance Method Details
#<=>(other) ⇒ Object
120
121
122
|
# File 'lib/openx/services/base.rb', line 120
def <=>(other)
self.id <=> other.id
end
|
#destroy ⇒ Object
114
115
116
117
118
|
# File 'lib/openx/services/base.rb', line 114
def destroy
session = self.class.connection
@server.call(self.class.delete, session.id, id)
@id = nil
end
|
#new_record? ⇒ Boolean
96
|
# File 'lib/openx/services/base.rb', line 96
def new_record?; @id.nil?; end
|
#save! ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/openx/services/base.rb', line 98
def save!
params = {}
session = self.class.connection
self.class.translations.keys.each { |k|
value = send(:"#{k}")
params[self.class.translations[k].to_s] = value if value
}
if new_record?
@id = @server.call(self.class.create, session.id, params)
else
@server.call(self.class.update, session.id, params)
end
self
end
|