forked from AnyPresence/dm-soap-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.rb
124 lines (99 loc) · 3.3 KB
/
example.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
113
114
115
116
117
118
119
120
121
#!/usr/bin/ruby
#
# More of a straight-up example of a script using salesforce-adapter.
# Use script/console to interact with the spec fixtures via IRB.
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
xtra = File.expand_path('../api', __FILE__)
$LOAD_PATH.unshift(xtra) unless $LOAD_PATH.include?(xtra)
require 'rubygems'
require 'dm-core'
require 'dm-soap-adapter'
class Account
include SoapAdapter::Resource
def cast_attrs(attrs)
props = Account.properties
new_attrs = {}
attrs.each_pair do |k,v|
if !v.eql? nil
case props[k.to_sym].primitive.name
when 'Integer'
new_attrs[k.to_sym] = v.to_i
when 'String'
new_attrs[k.to_sym] = v.to_str
else
new_attrs[k.to_sym] = v
end
end
end
attrs = new_attrs
end
def initialize(attrs = {}, options = {})
super({})
attrs = cast_attrs(attrs)
self.assign_attributes(attrs, as: options[:as] )
end
def update_attributes(attrs = {}, options = {})
attrs = cast_attrs(attrs)
self.assign_attributes(attrs, as: options[:as] )
self.save
end
def assign_attributes(values, options = {})
values.each do |k, v|
send("#{k}=", v)
end
end
def self.default_repository_name
:soap
end
property :id, String, :key => true
property :name, String
property :description, String
property :fax, String
property :phone, String
property :type, String
property :website, String
end
class Contact
include SoapAdapter::Resource
def self.default_repository_name
:soap
end
property :id, Serial
property :first_name, String
property :last_name, String
property :email, String
belongs_to :account
end
@adapter = DataMapper.setup(:soap, {:adapter => 'soap',
:path => "http://localhost:3000/accounts/wsdl",
:class => Account,
:methods => {:create => 'createAccount',
:read => 'getAccount',
:update => 'updateAccount',
:delete => 'deleteAccount',
:all => 'allAccounts'}})
first = Account.new( :id => '10004',
:name => 'name',
:description => 'description',
:fax => 'fax',
:phone => 'phone',
:type => 'type',
:website => 'website')
second = Account.new( :id => '10004',
:name => 'name_updated',
:description => 'description_updated',
:fax => 'fax_updated',
:phone => 'phone_updated',
:type => 'type_updated',
:website => 'website_updated')
puts 'testing create...'
first = Account.create(first.attributes)
puts 'testing update...'
puts @adapter.update(second, first)
puts 'testing read'
Account.get(10004)
puts 'testing delete...'
puts @adapter.delete(1005)
puts 'testing all...'
puts @adapter.all()