sk_api_schema 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  A more detailed view of the changes can be found in the {commit messages}[https://github.com/salesking/sk_api_schema/commits/]
4
4
 
5
+ 2011-05
6
+ * new hash_clean method for ruby schema reader class
7
+
5
8
  2011-04
6
9
  * added tags
7
10
  * added documents
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/lib/sk_api_schema.rb CHANGED
@@ -149,6 +149,34 @@ module SK
149
149
  links.empty? ? nil : links
150
150
  end
151
151
 
152
+ # Clean a hash before a new object is created from it. Can be used in
153
+ # your ruby controllers where new objects are created from a params-hash
154
+ # Directly operates and CHANGES incoming properties-hash!
155
+ #
156
+ # === Parameter
157
+ # obj_name<String>:: name of the object/schema
158
+ # props<Hash{String|Symbol=>Mixed}>:: properties of the object as hash
159
+ # version<String>:: api version
160
+ # opts<Hash{Symbol=>Mixed}>:: additional options
161
+ # === Param opts
162
+ # :keep<Array[String]>:: properties not being kicked even if defined as
163
+ # readonly
164
+ def clean_hash!(obj_name, props, version, opts={})
165
+ # gather allowed properties
166
+ schema = SK::Api::Schema.read(obj_name, version)
167
+ setters = []
168
+ schema['properties'].each{ |k,v| setters << k if !v['readonly'] }
169
+ setters += opts[:keep] if opts[:keep] && opts[:keep].is_a?(Array)
170
+ # kick readonly
171
+ props.delete_if { |k,v| !setters.include?("#{k}") }
172
+ #convert to type in schema
173
+ props.each do |k,v|
174
+ if schema['properties']["#{k}"]['type'] == 'string' && !v.is_a?(String)
175
+ props[k] = "#{v}"
176
+ end
177
+ end
178
+ end
179
+
152
180
  end # class methods
153
181
  end
154
182
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sk_api_schema}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Georg Leciejewski"]
12
- s.date = %q{2011-04-28}
12
+ s.date = %q{2011-05-02}
13
13
  s.description = %q{SalesKing API JSON schema and utility methods}
14
14
  s.email = %q{gl@salesking.eu}
15
15
  s.extra_rdoc_files = [
@@ -1,6 +1,6 @@
1
1
  require 'spec/spec_helper'
2
2
 
3
- describe SK::Api::Schema do
3
+ describe SK::Api::Schema, 'reading' do
4
4
 
5
5
  before :each do
6
6
  SK::Api::Schema.registry_reset
@@ -111,6 +111,33 @@ describe SK::Api::Schema, 'object parsing' do
111
111
  end
112
112
  end
113
113
 
114
+ describe SK::Api::Schema, 'hash cleaning' do
115
+
116
+ it "should kick readonly properties" do
117
+ props = {'id'=>'some id', 'archived_pdf'=>'some val', 'number'=>'1234' }
118
+ SK::Api::Schema.clean_hash!(:invoice, props, 'v1.0')
119
+ props.keys.should == ['number']
120
+
121
+ props1 = {:id =>'some id', :archived_pdf =>'some val', :number =>'1234' }
122
+ SK::Api::Schema.clean_hash!(:invoice, props1, 'v1.0')
123
+ props1.keys.should == [:number]
124
+ end
125
+
126
+ it "should convert string properties" do
127
+ props = {'title'=>4711, 'number'=>1234 }
128
+ SK::Api::Schema.clean_hash!(:invoice, props, 'v1.0')
129
+ props['title'].should == "4711"
130
+ props['number'].should == "1234"
131
+ end
132
+
133
+ it "should keep some properties" do
134
+ props = {'id'=>'some id', 'number'=>1234 }
135
+ SK::Api::Schema.clean_hash!(:invoice, props, 'v1.0', :keep=>['id'])
136
+ props['id'].should == "some id"
137
+ end
138
+
139
+ end
140
+
114
141
  ################################################################################
115
142
  # virtual classes used in test
116
143
  class Invoice
@@ -120,6 +147,7 @@ end
120
147
  class LineItem
121
148
  attr_accessor :id, :name, :description, :position, :price_single
122
149
  end
150
+
123
151
  class Client
124
152
  attr_accessor :id, :organisation, :last_name, :number, :addresses
125
153
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sk_api_schema
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Georg Leciejewski
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-28 00:00:00 +02:00
18
+ date: 2011-05-02 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency