restly 0.0.1.alpha.12 → 0.0.1.alpha.16

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,10 @@
1
1
  module Restly::Base::Includes
2
2
  extend ActiveSupport::Concern
3
3
 
4
+ included do
5
+ class_attribute :current_specification, instance_writer: false
6
+ end
7
+
4
8
  module ClassMethods
5
9
 
6
10
  # Delegate stuff to client
@@ -16,13 +20,17 @@ module Restly::Base::Includes
16
20
  end
17
21
 
18
22
  def has_specification
19
- self.fields = Restly::Base::Resource::Specification.new(self).fields
23
+ self.current_specification = Restly::Base::Resource::Specification.new(self)
24
+
25
+ self.fields = current_specification.fields
20
26
 
21
27
  self._accessible_attributes = accessible_attributes_configs.dup
28
+ self._protected_attributes = protected_attributes_configs.dup
22
29
 
23
- (self._accessible_attributes ||= {})[:default] = Restly::Base::Resource::Specification.new(self).accessible_attributes
30
+ (self._accessible_attributes ||= {})[:default] = current_specification.accessible_attributes
31
+ (self._protected_attributes ||= {})[:default] = current_specification.protected_attributes
24
32
 
25
- self._active_authorizer = self._accessible_attributes
33
+ self._active_authorizer = current_specification.active_authorizer
26
34
  end
27
35
 
28
36
  def connection
@@ -26,7 +26,7 @@ module Restly::Base::Instance::Persistence
26
26
 
27
27
  def reload!
28
28
  raise Restly::Error::MissingId, "Cannot reload #{resource_name}, either it hasn't been created or it is missing an ID." unless id
29
- set_attributes_from_response connection.get(path, force: true)
29
+ set_attributes_from_response connection.get(path_with_format, force: true)
30
30
  @loaded = true
31
31
  self
32
32
  end
@@ -1,103 +1,60 @@
1
- class Restly::Base::Resource::Specification < HashWithIndifferentAccess
1
+ class Restly::Base::Resource::Specification
2
+ extend ActiveSupport::Autoload
2
3
 
3
- attr_reader :model, :fields, :accessible_attributes
4
+ autoload :Fields
5
+ autoload :MassAssignmentSecurity
6
+
7
+ attr_reader :model
8
+
9
+ delegate :authorize, :client_token, :path, to: :model
4
10
 
5
11
  def initialize(model)
6
12
  @model = model
7
- @fields = Fields.new(self)
8
- @accessible_attributes = AccessibleAttributes.new(self)
13
+ @specification = HashWithIndifferentAccess.new
14
+ @retry_count = 0
9
15
  end
10
16
 
11
- def [](key)
12
- reload! if super.nil?
13
- super
17
+ def accessible_attributes
18
+ @accessible_attributes ||= MassAssignmentSecurity::AccessibleAttributes.new(self)
14
19
  end
15
20
 
16
- def reload!
17
- parsed_response = authorize(client_token).connection.request(:options, path).parsed
18
- self.replace parsed_response if parsed_response.present?
19
- rescue OAuth2::Error
20
- false
21
+ def protected_attributes
22
+ @protected_attributes ||= MassAssignmentSecurity::ProtectedAttributes.new(self)
21
23
  end
22
24
 
23
- def method_missing(method, *args, &block)
24
- return model.send(method, *args, &block) if model.respond_to?(method)
25
- super
25
+ def active_authorizer
26
+ @active_authorizer ||= MassAssignmentSecurity::DynamicAuthorizer.new(self)
26
27
  end
27
28
 
28
- def respond_to_missing?(method, include_private=false)
29
- model.respond_to?(method)
29
+ def fields
30
+ @fields ||= Fields.new(self)
30
31
  end
31
32
 
32
- module ReloadableSet
33
-
34
- def include?(field)
35
- reload! unless super
36
- super
37
- end
38
-
39
- def inspect
40
- reload! if empty?
41
- super
42
- end
43
-
44
- def each(*args, &block)
45
- reload! if empty?
46
- super
47
- end
48
-
49
- def map(*args, &block)
50
- reload! if empty?
51
- super
52
- end
53
-
54
- def map!(*args, &block)
55
- reload! if empty?
56
- super
57
- end
58
-
59
- def reduce(*args, &block)
60
- reload! if empty?
61
- super
62
- end
63
-
64
- def reload!
65
- replace []
66
- self
67
- end
68
-
33
+ def [](key)
34
+ reload_specification! unless @specification[key].present?
35
+ @specification[key]
69
36
  end
70
37
 
71
- class Fields < Restly::Base::Fields::FieldSet
72
- include ReloadableSet
73
-
74
- attr_reader :spec
75
-
76
- def initialize(spec)
77
- @spec = spec
78
- super([])
79
- end
80
-
81
- def reload!
82
- replace spec[:attributes]
83
- end
38
+ private
84
39
 
40
+ def reload_specification!
41
+ parsed_response = authorize(client_token).connection.request(:options, path).parsed
42
+ @specification = parsed_response.with_indifferent_access if parsed_response.present?
43
+ rescue OAuth2::Error
44
+ false
85
45
  end
86
46
 
87
- class AccessibleAttributes < ActiveModel::MassAssignmentSecurity::WhiteList
88
- include ReloadableSet
89
-
90
- attr_reader :spec
91
-
92
- def initialize(spec)
93
- @spec = spec
94
- super([])
95
- end
96
-
97
- def reload!
98
- replace spec[:actions].map { |action| action['parameters'] }.flatten if spec[:actions].present?
47
+ def method_missing(method, *args, &block)
48
+ if self[method]
49
+ self[method]
50
+ else
51
+ @retry_count = 0
52
+ super(method, *args, &block)
99
53
  end
54
+ end
100
55
 
56
+ def respond_to_missing?(method, include_private=false)
57
+ model.respond_to?(method)
101
58
  end
102
59
 
103
60
  end
@@ -0,0 +1,40 @@
1
+ class Restly::Base::Resource::Specification::Fields < Restly::Proxies::Base
2
+
3
+ attr_reader :spec
4
+
5
+ def initialize(spec)
6
+ @spec = spec
7
+ @removed = Set.new
8
+ @added = Set.new
9
+ super Restly::Base::Fields::FieldSet.new
10
+ end
11
+
12
+ def -(field)
13
+ @removed << field
14
+ super
15
+ end
16
+
17
+ def +(field)
18
+ @added << field
19
+ super
20
+ end
21
+
22
+ private
23
+
24
+ def method_missing(m, *args, &block)
25
+ reload_specification! unless super.present?
26
+ if (value = super).is_a? self.class
27
+ __setobj__ value
28
+ self
29
+ else
30
+ value
31
+ end
32
+ end
33
+
34
+ def reload_specification!
35
+ from_spec = spec[:attributes] || []
36
+ fields = (from_spec - @removed.to_a) + @added.to_a
37
+ __setobj__ Restly::Base::Fields::FieldSet.new(fields)
38
+ end
39
+
40
+ end
@@ -0,0 +1,76 @@
1
+ module Restly::Base::Resource::Specification::MassAssignmentSecurity
2
+
3
+ class DynamicAuthorizer < Restly::Proxies::Base
4
+
5
+ attr_reader :spec
6
+
7
+ def initialize(spec)
8
+ @spec = spec
9
+ super({ default: ActiveModel::MassAssignmentSecurity::BlackList.new })
10
+ end
11
+
12
+ private
13
+
14
+ def method_missing(m, *args, &block)
15
+
16
+ # Try to set the proper authorizer!
17
+ if spec.accessible_attributes.present?
18
+ __setobj__ ({ default: spec.accessible_attributes })
19
+
20
+ elsif spec.protected_attributes.present?
21
+ __setobj__ ({ default: spec.protected_attributes })
22
+
23
+ end
24
+
25
+ super
26
+ end
27
+
28
+ end
29
+
30
+ class AccessibleAttributes < Restly::Proxies::Base
31
+
32
+ attr_reader :spec
33
+
34
+ def initialize(spec)
35
+ @spec = spec
36
+ super ActiveModel::MassAssignmentSecurity::WhiteList.new
37
+ end
38
+
39
+ private
40
+
41
+ def method_missing(m, *args, &block)
42
+ reload_specification! unless super.present?
43
+ super
44
+ end
45
+
46
+ def reload_specification!
47
+ accepts = spec.actions.map { |action| action['accepts_parameters'] }.flatten if spec.actions.present?
48
+ __setobj__ ActiveModel::MassAssignmentSecurity::BlackList.new accepts
49
+ end
50
+
51
+ end
52
+
53
+ class ProtectedAttributes < Restly::Proxies::Base
54
+
55
+ attr_reader :spec
56
+
57
+ def initialize(spec)
58
+ @spec = spec
59
+ super ActiveModel::MassAssignmentSecurity::BlackList.new
60
+ end
61
+
62
+ private
63
+
64
+ def method_missing(m, *args, &block)
65
+ reload_specification! unless super.present?
66
+ super
67
+ end
68
+
69
+ def reload_specification!
70
+ rejects = spec.actions.map { |action| action['rejects_parameters'] }.flatten if spec.actions.present?
71
+ __setobj__ ActiveModel::MassAssignmentSecurity::BlackList.new rejects
72
+ end
73
+
74
+ end
75
+
76
+ end
@@ -1,3 +1,3 @@
1
1
  module Restly
2
- VERSION = "0.0.1.alpha.12"
2
+ VERSION = "0.0.1.alpha.16"
3
3
  end
data/restly.gemspec CHANGED
@@ -8,23 +8,23 @@ Gem::Specification.new do |gem|
8
8
  gem.version = Restly::VERSION
9
9
  gem.authors = ["Jason Waldrip"]
10
10
  gem.email = ["jason@waldrip.net"]
11
- gem.description = %q{ Allows your app to authenticate a resource with oauth}
12
- gem.summary = %q{ Allows your app to authenticate a resource with oauth}
11
+ gem.description = %q{ An ODM for a RESTful service }
12
+ gem.summary = %q{ Restly incorporates a ActiveModel based ODM for your REST based service. Think of Restly as a Re-imagination of Rails' ActiveResource }
13
13
  gem.homepage = "http://github.com/jwaldrip/restly"
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
- gem.add_dependency "oauth2"
20
- gem.add_dependency "activesupport"
21
- gem.add_dependency "activemodel"
22
- gem.add_dependency "colorize"
19
+ gem.add_dependency "oauth2", "~> 0.8.0"
20
+ gem.add_dependency "activesupport", "~> 3.2"
21
+ gem.add_dependency "activemodel", "~> 3.2"
22
+ gem.add_dependency "colorize", "~> 0.5.8"
23
23
 
24
- gem.add_development_dependency "rspec"
25
- gem.add_development_dependency "pry"
26
- gem.add_development_dependency "faraday_simulation"
27
- gem.add_development_dependency "faraday_middleware"
28
- gem.add_development_dependency "multi_xml"
24
+ gem.add_development_dependency "rspec", "~> 2.12"
25
+ gem.add_development_dependency "pry", "~> 9.10"
26
+ gem.add_development_dependency "faraday_simulation", "~> 0.0.2"
27
+ gem.add_development_dependency "faraday_middleware", "~> 0.9"
28
+ gem.add_development_dependency "multi_xml", "~> 0.5"
29
29
 
30
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.alpha.12
4
+ version: 0.0.1.alpha.16
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,153 +9,153 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-15 00:00:00.000000000 Z
12
+ date: 2012-11-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: oauth2
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ! '>='
19
+ - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '0'
21
+ version: 0.8.0
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ! '>='
27
+ - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: '0'
29
+ version: 0.8.0
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: activesupport
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ! '>='
35
+ - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: '0'
37
+ version: '3.2'
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ! '>='
43
+ - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: '0'
45
+ version: '3.2'
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: activemodel
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
- - - ! '>='
51
+ - - ~>
52
52
  - !ruby/object:Gem::Version
53
- version: '0'
53
+ version: '3.2'
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  none: false
58
58
  requirements:
59
- - - ! '>='
59
+ - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: '3.2'
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: colorize
64
64
  requirement: !ruby/object:Gem::Requirement
65
65
  none: false
66
66
  requirements:
67
- - - ! '>='
67
+ - - ~>
68
68
  - !ruby/object:Gem::Version
69
- version: '0'
69
+ version: 0.5.8
70
70
  type: :runtime
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
- - - ! '>='
75
+ - - ~>
76
76
  - !ruby/object:Gem::Version
77
- version: '0'
77
+ version: 0.5.8
78
78
  - !ruby/object:Gem::Dependency
79
79
  name: rspec
80
80
  requirement: !ruby/object:Gem::Requirement
81
81
  none: false
82
82
  requirements:
83
- - - ! '>='
83
+ - - ~>
84
84
  - !ruby/object:Gem::Version
85
- version: '0'
85
+ version: '2.12'
86
86
  type: :development
87
87
  prerelease: false
88
88
  version_requirements: !ruby/object:Gem::Requirement
89
89
  none: false
90
90
  requirements:
91
- - - ! '>='
91
+ - - ~>
92
92
  - !ruby/object:Gem::Version
93
- version: '0'
93
+ version: '2.12'
94
94
  - !ruby/object:Gem::Dependency
95
95
  name: pry
96
96
  requirement: !ruby/object:Gem::Requirement
97
97
  none: false
98
98
  requirements:
99
- - - ! '>='
99
+ - - ~>
100
100
  - !ruby/object:Gem::Version
101
- version: '0'
101
+ version: '9.10'
102
102
  type: :development
103
103
  prerelease: false
104
104
  version_requirements: !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
- - - ! '>='
107
+ - - ~>
108
108
  - !ruby/object:Gem::Version
109
- version: '0'
109
+ version: '9.10'
110
110
  - !ruby/object:Gem::Dependency
111
111
  name: faraday_simulation
112
112
  requirement: !ruby/object:Gem::Requirement
113
113
  none: false
114
114
  requirements:
115
- - - ! '>='
115
+ - - ~>
116
116
  - !ruby/object:Gem::Version
117
- version: '0'
117
+ version: 0.0.2
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  none: false
122
122
  requirements:
123
- - - ! '>='
123
+ - - ~>
124
124
  - !ruby/object:Gem::Version
125
- version: '0'
125
+ version: 0.0.2
126
126
  - !ruby/object:Gem::Dependency
127
127
  name: faraday_middleware
128
128
  requirement: !ruby/object:Gem::Requirement
129
129
  none: false
130
130
  requirements:
131
- - - ! '>='
131
+ - - ~>
132
132
  - !ruby/object:Gem::Version
133
- version: '0'
133
+ version: '0.9'
134
134
  type: :development
135
135
  prerelease: false
136
136
  version_requirements: !ruby/object:Gem::Requirement
137
137
  none: false
138
138
  requirements:
139
- - - ! '>='
139
+ - - ~>
140
140
  - !ruby/object:Gem::Version
141
- version: '0'
141
+ version: '0.9'
142
142
  - !ruby/object:Gem::Dependency
143
143
  name: multi_xml
144
144
  requirement: !ruby/object:Gem::Requirement
145
145
  none: false
146
146
  requirements:
147
- - - ! '>='
147
+ - - ~>
148
148
  - !ruby/object:Gem::Version
149
- version: '0'
149
+ version: '0.5'
150
150
  type: :development
151
151
  prerelease: false
152
152
  version_requirements: !ruby/object:Gem::Requirement
153
153
  none: false
154
154
  requirements:
155
- - - ! '>='
155
+ - - ~>
156
156
  - !ruby/object:Gem::Version
157
- version: '0'
158
- description: ! ' Allows your app to authenticate a resource with oauth'
157
+ version: '0.5'
158
+ description: ! ' An ODM for a RESTful service '
159
159
  email:
160
160
  - jason@waldrip.net
161
161
  executables: []
@@ -197,6 +197,8 @@ files:
197
197
  - lib/restly/base/resource/batch_actions.rb
198
198
  - lib/restly/base/resource/finders.rb
199
199
  - lib/restly/base/resource/specification.rb
200
+ - lib/restly/base/resource/specification/fields.rb
201
+ - lib/restly/base/resource/specification/mass_assignment_security.rb
200
202
  - lib/restly/client.rb
201
203
  - lib/restly/collection.rb
202
204
  - lib/restly/collection/pagination.rb
@@ -354,7 +356,8 @@ rubyforge_project:
354
356
  rubygems_version: 1.8.23
355
357
  signing_key:
356
358
  specification_version: 3
357
- summary: Allows your app to authenticate a resource with oauth
359
+ summary: Restly incorporates a ActiveModel based ODM for your REST based service.
360
+ Think of Restly as a Re-imagination of Rails' ActiveResource
358
361
  test_files:
359
362
  - spec/dummy/.gitignore
360
363
  - spec/dummy/README.rdoc