arrest 0.0.12 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,7 +1,8 @@
1
- *.gem
2
1
  .bundle
3
- Gemfile.lock
4
- pkg/*
2
+ .project
3
+ *.gem
5
4
  *.swp
6
5
  coverage
6
+ Gemfile.lock
7
7
  log/*
8
+ pkg/*
@@ -6,6 +6,7 @@ require 'arrest/helper/logger'
6
6
  require "arrest/attributes/belongs_to"
7
7
  require "arrest/attributes/has_attributes"
8
8
  require "arrest/attributes/converter"
9
+ require "arrest/handler"
9
10
  require "arrest/source"
10
11
  require "arrest/helper/filter"
11
12
  require "arrest/helper/child_collection"
@@ -25,7 +25,12 @@ module Arrest
25
25
 
26
26
  def to_hash value
27
27
  return nil unless value != nil
28
- value
28
+ converter = CONVERTER[@clazz]
29
+ if converter == nil
30
+ puts "No converter for: #{@clazz.name}"
31
+ converter = IdentConv
32
+ end
33
+ converter.mk_json value
29
34
  end
30
35
  end
31
36
 
@@ -84,6 +89,10 @@ module Arrest
84
89
  end
85
90
  end
86
91
 
92
+ def mk_json obj
93
+ obj
94
+ end
95
+
87
96
  def target clazz
88
97
  @clazz = clazz
89
98
  CONVERTER[clazz] = self
@@ -119,5 +128,9 @@ module Arrest
119
128
  def self.parse value
120
129
  Time.parse(value)
121
130
  end
131
+
132
+ def self.mk_json time
133
+ time.strftime "%FT%T%z"
134
+ end
122
135
  end
123
136
  end
@@ -0,0 +1,45 @@
1
+ module Arrest
2
+
3
+ # The classes in this module supply default behaviour
4
+ # for certain processing steps in the consumttion ot the
5
+ # rest api
6
+ module Handlers
7
+
8
+ class HeaderDecorator
9
+ # must return a hash from header name to value
10
+ def self.headers
11
+ {}
12
+ end
13
+ end
14
+
15
+
16
+ # a converter to transform between the name of the field in
17
+ # the json object and the name of the field in ruby code.
18
+ # Default behaviour is that for an underscored name in ruby
19
+ # a camel cased version in json expected:
20
+ # ruby -> json
21
+ # started_at startedAt
22
+ class KeyConverter
23
+ class << self
24
+ def key_from_json name
25
+ StringUtils.underscore(name.to_s)
26
+ end
27
+
28
+ def key_to_json name
29
+ StringUtils.classify(name.to_s,false)
30
+ end
31
+ end
32
+ end
33
+
34
+ class ErrorHandler
35
+ # a function to convert the body of an http response
36
+ # to a meaningful error message
37
+ def self.convert body, statuscode
38
+ body
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+
@@ -80,6 +80,10 @@ module Arrest
80
80
  rest_resource.id= id
81
81
  true
82
82
  else
83
+ if response.env[:status] != 201
84
+ err = Arrest::Source.error_handler.convert(response.body, response.env[:status])
85
+ rest_resource.errors.add(:base, err)
86
+ end
83
87
  puts "unable to create: #{response.env[:response_headers]} body: #{response.body} "
84
88
  false
85
89
  end
@@ -1,3 +1,4 @@
1
+ require "arrest/handler"
1
2
  module Arrest
2
3
  def self.debug s
3
4
  if Arrest::Source.debug
@@ -13,6 +14,7 @@ module Arrest
13
14
  attr_reader :header_decorator
14
15
  attr_accessor :json_key_converter
15
16
  attr_accessor :skip_validations
17
+ attr_accessor :error_handler
16
18
 
17
19
  def source=(host=nil)
18
20
  if [nil, ""].include?(host)
@@ -38,32 +40,20 @@ module Arrest
38
40
  def header_decorator=(hd=nil)
39
41
  Arrest::debug "Setting headerd to #{hd}"
40
42
  if hd == nil
41
- @header_decorator = self
43
+ @header_decorator = Handlers::Header_decorator
42
44
  elsif hd.respond_to?(:headers)
43
45
  @header_decorator = hd
44
46
  else
45
47
  raise "Header_decorator must be an object that returns an hash for the method headers"
46
48
  end
47
49
  end
48
-
49
- def headers
50
- {}
51
- end
52
-
53
- def key_from_json name
54
- StringUtils.underscore(name.to_s)
55
- end
56
-
57
- def key_to_json name
58
- StringUtils.classify(name.to_s,false)
59
- end
60
-
61
50
  end
62
51
  end
63
52
  Source.mod = nil
64
- Source.header_decorator = Source
53
+ Source.header_decorator = Handlers::HeaderDecorator
65
54
  Source.debug = false
66
- Source.json_key_converter = Source
55
+ Source.json_key_converter = Handlers::KeyConverter
56
+ Source.error_handler = Handlers::ErrorHandler
67
57
  Source.skip_validations = false
68
58
 
69
59
  end
@@ -1,3 +1,3 @@
1
1
  module Arrest
2
- VERSION = "0.0.12"
2
+ VERSION = "0.0.13"
3
3
  end
@@ -41,6 +41,10 @@ class SpecialZoo < Zoo
41
41
 
42
42
  end
43
43
 
44
+ class TimeClass < Arrest::RootResource
45
+ attribute :time, Time
46
+ end
47
+
44
48
  class ANestedClass < Arrest::NestedResource
45
49
  attribute :name, String
46
50
  attribute :underscore_name, String
@@ -391,5 +391,13 @@ class FirstTest < Test::Unit::TestCase
391
391
  assert_equal "Foo1", v1_reloaded.zoos.open_filter.first.name
392
392
  assert_equal 3, Zoo.all.length
393
393
  end
394
+
395
+ def test_time
396
+ now = Time.now
397
+ expected = now.strftime "%FT%T%z"
398
+ t = TimeClass.new(:time => now)
399
+ assert_equal expected, t.to_jhash[:time], "This is the expected default format"
400
+
401
+ end
394
402
  end
395
403
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-10 00:00:00.000000000Z
12
+ date: 2012-01-11 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &15777380 !ruby/object:Gem::Requirement
16
+ requirement: &13647940 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *15777380
24
+ version_requirements: *13647940
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: faraday
27
- requirement: &15776240 !ruby/object:Gem::Requirement
27
+ requirement: &13647020 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - =
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.7.5
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *15776240
35
+ version_requirements: *13647020
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activemodel
38
- requirement: &15775480 !ruby/object:Gem::Requirement
38
+ requirement: &13646240 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '3'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *15775480
46
+ version_requirements: *13646240
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &15749340 !ruby/object:Gem::Requirement
49
+ requirement: &13645500 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.0.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *15749340
57
+ version_requirements: *13645500
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &15748420 !ruby/object:Gem::Requirement
60
+ requirement: &13645080 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *15748420
68
+ version_requirements: *13645080
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rdoc
71
- requirement: &15747940 !ruby/object:Gem::Requirement
71
+ requirement: &13644600 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *15747940
79
+ version_requirements: *13644600
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
- requirement: &15747400 !ruby/object:Gem::Requirement
82
+ requirement: &13644080 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '2'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *15747400
90
+ version_requirements: *13644080
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rr
93
- requirement: &15746680 !ruby/object:Gem::Requirement
93
+ requirement: &13643640 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *15746680
101
+ version_requirements: *13643640
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: simplecov
104
- requirement: &15746220 !ruby/object:Gem::Requirement
104
+ requirement: &13643160 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *15746220
112
+ version_requirements: *13643160
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: rack
115
- requirement: &15745800 !ruby/object:Gem::Requirement
115
+ requirement: &13642740 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,7 +120,7 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *15745800
123
+ version_requirements: *13642740
124
124
  description: Consume a rest API in a AR like fashion
125
125
  email:
126
126
  - axel.tetzlaff@fortytools.com
@@ -140,6 +140,7 @@ files:
140
140
  - lib/arrest/attributes/converter.rb
141
141
  - lib/arrest/attributes/has_attributes.rb
142
142
  - lib/arrest/exceptions.rb
143
+ - lib/arrest/handler.rb
143
144
  - lib/arrest/helper/child_collection.rb
144
145
  - lib/arrest/helper/filter.rb
145
146
  - lib/arrest/helper/has_many_collection.rb