oxmlk 0.3.3 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/examples/fs_cdr.rb +53 -0
- data/examples/xml/fs_cdr.xml +26 -0
- data/lib/oxmlk.rb +30 -8
- data/lib/oxmlk/attr.rb +3 -3
- data/lib/oxmlk/elem.rb +2 -3
- data/oxmlk.gemspec +5 -2
- metadata +5 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/examples/fs_cdr.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec/spec_helper')
|
3
|
+
require 'sqlite3'
|
4
|
+
require 'activerecord'
|
5
|
+
|
6
|
+
DB_PATH = File.join(File.dirname(__FILE__), 'active_record.sqlite3')
|
7
|
+
ActiveRecord::Base.establish_connection(
|
8
|
+
:adapter => 'sqlite3',
|
9
|
+
:database => DB_PATH
|
10
|
+
)
|
11
|
+
|
12
|
+
class Cdr < ActiveRecord::Base
|
13
|
+
include OxMlk
|
14
|
+
|
15
|
+
ox_tag :cdr
|
16
|
+
|
17
|
+
ox_elem :hangup_cause, :in => 'variables'
|
18
|
+
ox_elem :start_stamp, :in => 'variables'
|
19
|
+
ox_elem :answer_stamp, :in => 'variables'
|
20
|
+
ox_elem :end_stamp, :in => 'variables'
|
21
|
+
ox_elem :caller_id, :in => 'variables'
|
22
|
+
ox_elem :duration, :in => 'variables'
|
23
|
+
ox_elem :billsec, :in => 'variables'
|
24
|
+
end
|
25
|
+
|
26
|
+
# do a quick pseudo migration. This should only get executed on the first run
|
27
|
+
unless Cdr.table_exists?
|
28
|
+
ActiveRecord::Base.connection.create_table(:cdrs) do |t|
|
29
|
+
t.string :hangup_cause
|
30
|
+
t.string :start_stamp
|
31
|
+
t.string :answer_stamp
|
32
|
+
t.string :end_stamp
|
33
|
+
t.string :caller_id
|
34
|
+
t.string :duration
|
35
|
+
t.string :billsec
|
36
|
+
t.timestamps
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
new_cdr = Cdr.from_xml(xml_for(:fs_cdr))
|
41
|
+
|
42
|
+
new_cdr.save
|
43
|
+
cdr = Cdr.find new_cdr.id
|
44
|
+
|
45
|
+
puts cdr.hangup_cause,
|
46
|
+
cdr.start_stamp,
|
47
|
+
cdr.answer_stamp,
|
48
|
+
cdr.end_stamp,
|
49
|
+
cdr.caller_id,
|
50
|
+
cdr.duration,
|
51
|
+
cdr.billsec,
|
52
|
+
'',
|
53
|
+
cdr.to_xml
|
@@ -0,0 +1,26 @@
|
|
1
|
+
<?xml version="1.0"?>
|
2
|
+
<cdr>
|
3
|
+
<variables>
|
4
|
+
|
5
|
+
<!-- extra xml tags 'n such i don't need -->
|
6
|
+
|
7
|
+
<hangup_cause>NORMAL_CLEARING</hangup_cause>
|
8
|
+
<start_stamp>2008-07-31 2011</start_stamp>
|
9
|
+
<answer_stamp>2008-07-31 2011</answer_stamp>
|
10
|
+
<end_stamp>2008-07-31 2011</end_stamp>
|
11
|
+
|
12
|
+
<caller_id>Mikey</caller_id>
|
13
|
+
<duration>39</duration>
|
14
|
+
<billsec>36</billsec>
|
15
|
+
|
16
|
+
<!-- extra xml tags 'n such i don't need -->
|
17
|
+
|
18
|
+
</variables>
|
19
|
+
</cdr>
|
20
|
+
|
21
|
+
<!--
|
22
|
+
these are the exact tag names and such i will recieve from Mod_xml_cdr,
|
23
|
+
there's ALOT more, but these are the only ones i need.
|
24
|
+
From the xml_cdr wiki: 'Date/time stamp variables are URI encoded
|
25
|
+
so be sure to "URI decode" them prior to inserting them into a database.'
|
26
|
+
-->
|
data/lib/oxmlk.rb
CHANGED
@@ -25,7 +25,6 @@ private
|
|
25
25
|
end
|
26
26
|
|
27
27
|
module InstanceMethods
|
28
|
-
|
29
28
|
# Returns a LibXML::XML::Node representing this object
|
30
29
|
def to_xml
|
31
30
|
self.class.to_xml(self)
|
@@ -124,7 +123,7 @@ private
|
|
124
123
|
def ox_attr(name,o={},&block)
|
125
124
|
new_attr = Attr.new(name, o.reverse_merge(:tag_proc => @tag_proc),&block)
|
126
125
|
@ox_attrs << new_attr
|
127
|
-
|
126
|
+
ox_accessor new_attr.accessor
|
128
127
|
end
|
129
128
|
|
130
129
|
# Declares a reference to a certain xml element or a typed collection of elements.
|
@@ -197,7 +196,7 @@ private
|
|
197
196
|
# The array will include all elements already manipulated by anything passed to
|
198
197
|
# the :as option. If the :as option is an Array and no block is passed then the
|
199
198
|
# Array is returned unmodified. If the :as option is nil or is not an Array then
|
200
|
-
# only the first element is
|
199
|
+
# only the first element is
|
201
200
|
#
|
202
201
|
# == Options
|
203
202
|
# === :as
|
@@ -336,7 +335,7 @@ private
|
|
336
335
|
def ox_elem(name,o={},&block)
|
337
336
|
new_elem = Elem.new(name, o.reverse_merge(:tag_proc => @tag_proc),&block)
|
338
337
|
@ox_elems << new_elem
|
339
|
-
|
338
|
+
ox_accessor new_elem.accessor
|
340
339
|
end
|
341
340
|
|
342
341
|
# Sets the name of the XML element that represents this class. Use this
|
@@ -407,9 +406,21 @@ private
|
|
407
406
|
xml = XML::Node.from(data)
|
408
407
|
raise 'invalid XML' unless xml.name == ox_tag
|
409
408
|
|
410
|
-
|
411
|
-
|
412
|
-
|
409
|
+
returning new do |ox|
|
410
|
+
(ox_attrs + ox_elems).each {|e| ox.send(e.setter,e.from_xml(xml))}
|
411
|
+
ox.send(:after_parse) if ox.respond_to?(:after_parse)
|
412
|
+
end
|
413
|
+
end
|
414
|
+
|
415
|
+
def ox_accessor(sym)
|
416
|
+
define_method(sym) do
|
417
|
+
@attributes ||= {}
|
418
|
+
@attributes[sym.to_s]
|
419
|
+
end
|
420
|
+
define_method("#{sym}=") do |val|
|
421
|
+
@attributes ||= {}
|
422
|
+
@attributes[sym.to_s] = val
|
423
|
+
end
|
413
424
|
end
|
414
425
|
|
415
426
|
# Returns XML generated from an instance based on Attr & Elem definitions.
|
@@ -417,13 +428,24 @@ private
|
|
417
428
|
# @return [XML::Node] Generated XML::Node
|
418
429
|
def to_xml(data)
|
419
430
|
ox = XML::Node.new(ox_tag)
|
431
|
+
wrappers = {}
|
432
|
+
|
420
433
|
ox_elems.each do |elem|
|
421
|
-
elem.
|
434
|
+
if elem.in
|
435
|
+
wrappers[elem.in] ||= XML::Node.new elem.in
|
436
|
+
elem.to_xml(data).each {|e| wrappers[elem.in] << e}
|
437
|
+
else
|
438
|
+
elem.to_xml(data).each {|e| ox << e}
|
439
|
+
end
|
422
440
|
end
|
441
|
+
|
442
|
+
wrappers.each {|k,v| ox << v}
|
443
|
+
|
423
444
|
ox_attrs.each do |a|
|
424
445
|
val = data.send(a.accessor).to_s
|
425
446
|
ox[a.tag]= val if val.present?
|
426
447
|
end
|
448
|
+
|
427
449
|
ox
|
428
450
|
end
|
429
451
|
end
|
data/lib/oxmlk/attr.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module OxMlk
|
2
2
|
class Attr
|
3
3
|
|
4
|
-
attr_reader :accessor, :setter
|
4
|
+
attr_reader :accessor, :setter, :from, :as, :procs, :tag_proc, :tag
|
5
5
|
|
6
6
|
# Named Procs for use in :as option.
|
7
7
|
PROCS = (Hash.new {|h,k| k.to_proc rescue nil}).merge(
|
@@ -44,13 +44,13 @@ module OxMlk
|
|
44
44
|
@procs = ([*as].map {|k| PROCS[k]} + [block]).compact
|
45
45
|
end
|
46
46
|
|
47
|
-
private
|
48
|
-
|
49
47
|
# Finds @tag in data and applies procs.
|
50
48
|
def from_xml(data)
|
51
49
|
procs.inject(XML::Node.from(data)[tag]) {|d,p| p.call(d) rescue d}
|
52
50
|
end
|
53
51
|
|
52
|
+
private
|
53
|
+
|
54
54
|
# Converts a value to a Boolean.
|
55
55
|
# @param [Symbol,String,Integer] value Value to convert
|
56
56
|
# @return [Boolean] Returns true if value is 'true', 'yes', 't' or 1.
|
data/lib/oxmlk/elem.rb
CHANGED
@@ -24,7 +24,7 @@ module OxMlk
|
|
24
24
|
@from = o[:from]
|
25
25
|
@as = [*o[:as]].compact
|
26
26
|
@as = [:bool] if as.empty? && name.to_s.end_with?('?')
|
27
|
-
@in = o[:in]
|
27
|
+
@in = o[:in].to_s
|
28
28
|
|
29
29
|
@procs = as.map {|k| PROCS[k]}
|
30
30
|
@procs = [PROCS[:value]] + procs unless [:raw,:name,:value].include?(as.first) || ox?
|
@@ -63,8 +63,7 @@ module OxMlk
|
|
63
63
|
XML::Node.new(tag, node.to_s)
|
64
64
|
end
|
65
65
|
end
|
66
|
-
|
67
|
-
@in ? [nodes.inject(XML::Node.new(@in)) {|o,n| o << n}] : nodes
|
66
|
+
nodes.empty? ? [] : nodes
|
68
67
|
end
|
69
68
|
|
70
69
|
def xpath
|
data/oxmlk.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{oxmlk}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Josh Robinson"]
|
12
|
-
s.date = %q{2009-09-
|
12
|
+
s.date = %q{2009-09-10}
|
13
13
|
s.description = %q{OxMlk gives you a simple way to map you ruby objects to XML and then convert one to the other.}
|
14
14
|
s.email = %q{hexorx@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -24,11 +24,13 @@ Gem::Specification.new do |s|
|
|
24
24
|
"VERSION",
|
25
25
|
"examples/amazon.rb",
|
26
26
|
"examples/example.rb",
|
27
|
+
"examples/fs_cdr.rb",
|
27
28
|
"examples/phrk.rb",
|
28
29
|
"examples/posts.rb",
|
29
30
|
"examples/twitter.rb",
|
30
31
|
"examples/xml/amazon.xml",
|
31
32
|
"examples/xml/example.xml",
|
33
|
+
"examples/xml/fs_cdr.xml",
|
32
34
|
"examples/xml/phrk.xml",
|
33
35
|
"examples/xml/posts.xml",
|
34
36
|
"examples/xml/twitter.xml",
|
@@ -58,6 +60,7 @@ Gem::Specification.new do |s|
|
|
58
60
|
"spec/xml_spec.rb",
|
59
61
|
"examples/amazon.rb",
|
60
62
|
"examples/example.rb",
|
63
|
+
"examples/fs_cdr.rb",
|
61
64
|
"examples/phrk.rb",
|
62
65
|
"examples/posts.rb",
|
63
66
|
"examples/twitter.rb"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: oxmlk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Robinson
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-09-
|
12
|
+
date: 2009-09-10 00:00:00 -06:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -59,11 +59,13 @@ files:
|
|
59
59
|
- VERSION
|
60
60
|
- examples/amazon.rb
|
61
61
|
- examples/example.rb
|
62
|
+
- examples/fs_cdr.rb
|
62
63
|
- examples/phrk.rb
|
63
64
|
- examples/posts.rb
|
64
65
|
- examples/twitter.rb
|
65
66
|
- examples/xml/amazon.xml
|
66
67
|
- examples/xml/example.xml
|
68
|
+
- examples/xml/fs_cdr.xml
|
67
69
|
- examples/xml/phrk.xml
|
68
70
|
- examples/xml/posts.xml
|
69
71
|
- examples/xml/twitter.xml
|
@@ -115,6 +117,7 @@ test_files:
|
|
115
117
|
- spec/xml_spec.rb
|
116
118
|
- examples/amazon.rb
|
117
119
|
- examples/example.rb
|
120
|
+
- examples/fs_cdr.rb
|
118
121
|
- examples/phrk.rb
|
119
122
|
- examples/posts.rb
|
120
123
|
- examples/twitter.rb
|