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.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour -r ./spec/spec_helper.rb
2
+
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nourl.gemspec
4
+ gem 'rspec'
5
+ gem 'json'
6
+ gemspec
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.
@@ -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
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))
2
+
3
+ require "json"
4
+ require "nourl/rpcable.rb"
5
+ require "nourl/core.rb"
@@ -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
@@ -0,0 +1,3 @@
1
+ module Nourl
2
+ VERSION = "0.0.1"
3
+ end
@@ -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
@@ -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
@@ -0,0 +1,7 @@
1
+ describe Nourl::RPCable do
2
+
3
+ it "should return an array of accessible methods" do
4
+ User.rpc_white_list.should == [:read]
5
+ end
6
+
7
+ end
@@ -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