nourl 0.0.1
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/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +6 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/lib/nourl.rb +5 -0
- data/lib/nourl/core.rb +48 -0
- data/lib/nourl/rpcable.rb +20 -0
- data/lib/nourl/version.rb +3 -0
- data/nourl.gemspec +17 -0
- data/spec/core_spec.rb +56 -0
- data/spec/rpcable_spec.rb +7 -0
- data/spec/spec_helper.rb +27 -0
- metadata +62 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Artem Yankov
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Nourl
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'nourl'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install nourl
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/nourl.rb
ADDED
data/lib/nourl/core.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
module Nourl
|
2
|
+
|
3
|
+
class << self
|
4
|
+
def rpc_white_list_for(class_list)
|
5
|
+
class_list = [class_list] unless class_list.is_a?(Array)
|
6
|
+
|
7
|
+
class_list.inject({}) do |list, class_name|
|
8
|
+
klass = Object.const_get(class_name.capitalize)
|
9
|
+
|
10
|
+
list[class_name] = klass.send(:rpc_white_list)
|
11
|
+
list
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def can_call?(klass, method)
|
16
|
+
return true if klass == Nourl && method == 'rpc_white_list_for'
|
17
|
+
return true if klass.send(:rpc_white_list).include?(method.to_sym)
|
18
|
+
false
|
19
|
+
end
|
20
|
+
|
21
|
+
def exec(params)
|
22
|
+
rpc_string = JSON.parse(params['rpc_string'])
|
23
|
+
class_name, method = rpc_string['method'].split(".")
|
24
|
+
klass = Object.const_get(class_name.capitalize)
|
25
|
+
|
26
|
+
params = rpc_string['params']
|
27
|
+
|
28
|
+
raise "No access to call #{klass.to_s}.#{method}." unless Nourl.can_call?(klass, method)
|
29
|
+
|
30
|
+
#TODO: find a sexy way to do this
|
31
|
+
result = if params.is_a?(Array)
|
32
|
+
klass.send(method, *params.compact)
|
33
|
+
else
|
34
|
+
klass.send(method, params)
|
35
|
+
end
|
36
|
+
|
37
|
+
json_rpc_format(result, nil, rpc_string['id'])
|
38
|
+
rescue => e
|
39
|
+
json_rpc_format(nil, e.message, rpc_string['id'])
|
40
|
+
end
|
41
|
+
|
42
|
+
def json_rpc_format(result, error, id)
|
43
|
+
{ "result" => result, "error" => error, "id" => id }
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Nourl
|
2
|
+
module RPCable
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
attr_reader :rpc_white_list
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def allow_rpc_for(*methods)
|
14
|
+
@rpc_white_list ||= []
|
15
|
+
@rpc_white_list |= methods
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
data/nourl.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/nourl/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Artem Yankov"]
|
6
|
+
gem.email = ["artem.yankov@gmail.com"]
|
7
|
+
gem.description = %q{Simple lib to provide RPC interface to models}
|
8
|
+
gem.summary = %q{Simple lib to provide RPC interface to models}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "nourl"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Nourl::VERSION
|
17
|
+
end
|
data/spec/core_spec.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
describe Nourl do
|
2
|
+
|
3
|
+
describe Nourl, ".rpc_white_list_for" do
|
4
|
+
|
5
|
+
it "should return a list of accessible methods" do
|
6
|
+
Nourl.rpc_white_list_for("User").should == {"User" => [:read]}
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should handle array of classes" do
|
10
|
+
Nourl.rpc_white_list_for(["User", "Post"]).should ==
|
11
|
+
{"User" => [:read],
|
12
|
+
"Post" => [:create, :edit]}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe Nourl, ".can_call?" do
|
17
|
+
|
18
|
+
it "should return TRUE for allowed methods" do
|
19
|
+
Nourl.can_call?(User, "read")
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return FALSE for not-allowed methods " do
|
23
|
+
Nourl.can_call?(User, "edit")
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
describe Nourl, ".exec" do
|
29
|
+
|
30
|
+
it "should call a passed method" do
|
31
|
+
rpc_string = {"rpc_string" => {"method" => "User.read", "params" => ["John"]}.to_json}
|
32
|
+
Nourl.exec(rpc_string).should == {"result"=>"user John", "error"=>nil, "id"=>nil}
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should handle errors" do
|
36
|
+
rpc_string = {"rpc_string" => {"method" => "User.read", "params" => ["John", 2]}.to_json}
|
37
|
+
Nourl.exec(rpc_string).should == {"result"=>nil, "error"=>"wrong number of arguments (2 for 1)", "id"=>nil}
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should handle not-allowed calls" do
|
41
|
+
rpc_string = {"rpc_string" => {"method" => "User.edit", "params" => ["John"]}.to_json}
|
42
|
+
Nourl.exec(rpc_string).should == {"result"=>nil, "error"=>"No access to call User.edit.", "id"=>nil}
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
describe Nourl, ".json_rpc_format" do
|
48
|
+
|
49
|
+
it "return a JSON-RPC formatted string" do
|
50
|
+
Nourl.json_rpc_format("some_result", nil, 3).should == {"result" => "some_result", "error" => nil, "id" => 3}
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
$:.unshift File.expand_path('../lib', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require "nourl"
|
4
|
+
|
5
|
+
#mocks
|
6
|
+
class User
|
7
|
+
include Nourl::RPCable
|
8
|
+
|
9
|
+
allow_rpc_for :read
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def read(name); "user #{name}" end;
|
13
|
+
def edit; true; end;
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Post
|
18
|
+
include Nourl::RPCable
|
19
|
+
|
20
|
+
allow_rpc_for :create, :edit
|
21
|
+
|
22
|
+
class << self
|
23
|
+
def read; true; end;
|
24
|
+
def edit; true; end;
|
25
|
+
def destroy; true; end;
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nourl
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Artem Yankov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-16 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Simple lib to provide RPC interface to models
|
15
|
+
email:
|
16
|
+
- artem.yankov@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .rspec
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/nourl.rb
|
28
|
+
- lib/nourl/core.rb
|
29
|
+
- lib/nourl/rpcable.rb
|
30
|
+
- lib/nourl/version.rb
|
31
|
+
- nourl.gemspec
|
32
|
+
- spec/core_spec.rb
|
33
|
+
- spec/rpcable_spec.rb
|
34
|
+
- spec/spec_helper.rb
|
35
|
+
homepage: ''
|
36
|
+
licenses: []
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.8.17
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: Simple lib to provide RPC interface to models
|
59
|
+
test_files:
|
60
|
+
- spec/core_spec.rb
|
61
|
+
- spec/rpcable_spec.rb
|
62
|
+
- spec/spec_helper.rb
|