graft 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@
2
2
 
3
3
  == Description
4
4
 
5
- Graft provides an easy way to map XML data onto your Ruby classes.
5
+ Graft provides an easy way to map XML and JSON data onto your Ruby classes.
6
6
 
7
7
  == Installation
8
8
 
@@ -12,11 +12,105 @@ Stable:
12
12
 
13
13
  Bleeding edge:
14
14
 
15
- $ sudo gem install reagent-graft --source=http://gems.github.com
15
+ $ git clone git://github.com/reagent/graft.git
16
+ $ cd graft && rake gem
17
+ $ sudo gem install pkg/graft-<version>.gem
16
18
 
17
19
  == Usage
18
20
 
19
- TBD
21
+ When interacting with APIs, it's quite often the case that the data returned as
22
+ part of the response is represented either as XML or JSON. The Graft library makes
23
+ turning that data into Ruby objects pretty simple. This code was an extraction from
24
+ my work on both the Fleakr[http://reagent.github.com/fleakr] and
25
+ Etsy[http://github.com/reagent/etsy] gems.
26
+
27
+ === Mapping XML
28
+
29
+ If you want to use Graft in XML mode, you'll need to include the right library:
30
+
31
+ require 'rubygems'
32
+ require 'graft/xml'
33
+
34
+ Once that is set up, you can take an XML string like this:
35
+
36
+ <rsp>
37
+ <user nsid="3">reagent</user>
38
+ <name>Patrick Reagan</name>
39
+ </rsp>
40
+
41
+ And map it onto a Ruby class:
42
+
43
+ class User
44
+ include Graft
45
+
46
+ attribute :name
47
+ attribute :username, :from => 'user'
48
+ attribute :id, :from => 'user@nsid', :type => :integer
49
+ end
50
+
51
+ There are a couple of ways to pull this data into the +User+ object. The simplest
52
+ is from the constructor:
53
+
54
+ user = User.new(xml)
55
+
56
+ There is also a +populate_from+ instance method that will do the same:
57
+
58
+ user = User.new
59
+ user.populate_from(xml)
60
+
61
+ This second method is useful if the data you want in your Ruby object comes from 2
62
+ separate XML files. Accessing it is simple:
63
+
64
+ >> user.name # => "Patrick Reagan"
65
+ >> user.username # => "reagent"
66
+ >> user.id # => 3
67
+
68
+ === Mapping JSON
69
+
70
+ The process of mapping JSON is similar to XML, except:
71
+
72
+ * You don't need to declare the type of the attribute
73
+ * You need to provide a full path to the JSON value
74
+
75
+ To get started, include the correct library:
76
+
77
+ require 'rubygems'
78
+ require 'graft/json'
79
+
80
+ Then for simple JSON data:
81
+
82
+ {
83
+ "rsp": {
84
+ "user_id": 3,
85
+ "username": "reagent",
86
+ "name": "Patrick Reagan"
87
+ }
88
+ }
89
+
90
+ You can map it in a similar way:
91
+
92
+ class User
93
+ include Graft
94
+
95
+ attribute :name, :from => 'rsp/name'
96
+ attribute :username, :from => 'rsp/username'
97
+ attribute :id, :from => 'rsp/user_id'
98
+ end
99
+
100
+ Again, you can initialize the values from both the constructor:
101
+
102
+ user = User.new(json)
103
+
104
+ Or the +populate_from+ method:
105
+
106
+ user = User.new
107
+ user.populate_from(json)
108
+
109
+ The results are the same:
110
+
111
+ >> user.name # => "Patrick Reagan"
112
+ >> user.username # => "reagent"
113
+ >> user.id # => 3
20
114
 
21
115
  == License
22
116
 
data/Rakefile CHANGED
@@ -12,7 +12,7 @@ spec = Gem::Specification.new do |s|
12
12
  s.has_rdoc = true
13
13
  s.extra_rdoc_files = %w(README.rdoc)
14
14
  s.rdoc_options = %w(--main README.rdoc)
15
- s.summary = "Simple XML to attribute mapping for your Ruby classes"
15
+ s.summary = "Graft provides an easy way to map XML and JSON data onto your Ruby classes"
16
16
  s.author = 'Patrick Reagan'
17
17
  s.email = 'reaganpr@gmail.com'
18
18
  s.homepage = 'http://sneaq.net/'
@@ -3,7 +3,7 @@ module Graft
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 2
6
- TINY = 0
6
+ TINY = 1
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
@@ -3,7 +3,15 @@ $:.unshift File.dirname(__FILE__) + '/..'
3
3
  require 'hpricot'
4
4
  require 'builder'
5
5
  require 'tzinfo'
6
- require 'active_support/core_ext/blank'
6
+
7
+ begin
8
+ # ActiveSupport < 2.3.5
9
+ require 'active_support/core_ext/blank'
10
+ rescue NameError
11
+ # ActiveSupport >= 2.3.5 will raise a NameError exception
12
+ require 'active_support/core_ext/object/blank'
13
+ end
14
+
7
15
  require 'active_support/time_with_zone'
8
16
  require 'active_support/inflector'
9
17
 
@@ -41,7 +41,10 @@ module Graft
41
41
  def value_from(document)
42
42
  values = sources.map do |source|
43
43
  node = node_for(document, source)
44
- (node.attributes[attribute(source)] || node.inner_text) unless node.nil?
44
+ if !node.nil?
45
+ possible_values = [node.attributes[attribute(source)], node.inner_text]
46
+ possible_values.detect {|value| !value.blank? }
47
+ end
45
48
  end
46
49
 
47
50
  type_class.new(values.compact.first).value
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graft
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Reagan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-07 00:00:00 -04:00
12
+ date: 2010-01-25 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -115,9 +115,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  requirements: []
116
116
 
117
117
  rubyforge_project:
118
- rubygems_version: 1.3.4
118
+ rubygems_version: 1.3.5
119
119
  signing_key:
120
120
  specification_version: 3
121
- summary: Simple XML to attribute mapping for your Ruby classes
121
+ summary: Graft provides an easy way to map XML and JSON data onto your Ruby classes
122
122
  test_files: []
123
123