barrister-rails 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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/LICENSE.txt +22 -0
- data/README.md +87 -0
- data/Rakefile +1 -0
- data/barrister-rails.gemspec +25 -0
- data/lib/barrister-rails/version.rb +9 -0
- data/lib/barrister-rails.rb +131 -0
- data/spec/spec_helper.rb +4 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bcf3199f32088494e519192f36e76ada5ebda938
|
4
|
+
data.tar.gz: 7f05224550b54e45507b659fa3e70601b8bc5687
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 31b96e510cbff54265d994588638e6b206814d705f859b8fc2f4d75b7816017de354da0be2422cbaac368dace70b0c61224dc081776ccf670106ba805c4ebbfe
|
7
|
+
data.tar.gz: 3e9b469248bea6c49f149ec484bc6e71c5d9cdcde6f919693d46b8001181994aef7530850643ee1cfe5bf8800b591d5743f6cdca50271d7bcbbff2527291e5ae
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Erin Swenson-Healey
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Erin Swenson-Healey
|
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,87 @@
|
|
1
|
+
# barrister-rails
|
2
|
+
|
3
|
+
A wrapper for the Barrister Ruby client, for Rails. Transmutes the hashes
|
4
|
+
representing custom IDL structs to instances of runtime-generated psuedo-models
|
5
|
+
that implement the ActiveModel interface - allowing them to be consumed easily
|
6
|
+
by your Rails views.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'barrister-rails'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install barrister-rails
|
21
|
+
|
22
|
+
## A note on the hash-to-model transmute
|
23
|
+
|
24
|
+
By default, the Barrister Rails client transmutes hashes (representing structs
|
25
|
+
in the IDL) to instances of runtime-defined classes that implement the
|
26
|
+
ActiveModel interface. This allows data from the server to be consumed easily
|
27
|
+
by Rails' view helpers. Note that these classes will only be created if a
|
28
|
+
class of the same name cannot be found. If you want to implement your own class
|
29
|
+
that the Rails client will instantiate, simply define it in app/models using
|
30
|
+
the name of the struct from the IDL.
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
### Setup
|
35
|
+
|
36
|
+
By default, the barrister-rails client assumes your server is reachable via http:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
|
40
|
+
c = Barrister::Rails::Client.new 'http://localhost:3001/api'
|
41
|
+
|
42
|
+
```
|
43
|
+
|
44
|
+
If an alternative transport is desired, simply instantiate it and pass it to the
|
45
|
+
client's constructor:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
|
49
|
+
redis_transport = Barrister::Transports::Redis.new 'some_channel_name'
|
50
|
+
c = Barrister::Rails::Client.new redis_transport
|
51
|
+
|
52
|
+
```
|
53
|
+
|
54
|
+
### Usage
|
55
|
+
|
56
|
+
Upon instantiation, the Barrister client will attempt to pull down the JSON
|
57
|
+
representation of the IDL file used by the Barrister server. Assuming an
|
58
|
+
interface has been defined in the IDL, you can call methods on it like so:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
|
62
|
+
users = c.UserService.get_all_users
|
63
|
+
puts users
|
64
|
+
=> [#<User email: "smasd", full_name: "Joe", id: 9, phone_number: "2069990811">,
|
65
|
+
#<User email: "smurf", full_name: "Bob", id: 10, phone_number: "234234234">]
|
66
|
+
|
67
|
+
```
|
68
|
+
|
69
|
+
### Disabling hash-to-model transmute
|
70
|
+
|
71
|
+
To disable the runtime creation of classes and conversion of Ruby hashes,
|
72
|
+
simply pass a false-value on the (optional) configuration hash at the time
|
73
|
+
of instantiation:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
|
77
|
+
c = Barrister::Rails::Client.new 'http://localhost:3001/api', transmute_to_model: false
|
78
|
+
|
79
|
+
```
|
80
|
+
|
81
|
+
## Contributing
|
82
|
+
|
83
|
+
1. Fork it ( http://github.com/<my-github-username>/barrister-rails/fork )
|
84
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
85
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
86
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
87
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'barrister-rails/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "barrister-rails"
|
8
|
+
spec.version = Barrister::Rails::VERSION
|
9
|
+
spec.authors = ["Erin Swenson-Healey"]
|
10
|
+
spec.email = ["erin.swenson.healey@gmail.com"]
|
11
|
+
spec.summary = %q{A Rails-oriented wrapper for the Barrister client.}
|
12
|
+
spec.description = %q{More to come!}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "barrister", "~> 0"
|
22
|
+
spec.add_dependency "active_attr", "~> 0"
|
23
|
+
spec.add_development_dependency "pry", "~> 0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 2.0"
|
25
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require "barrister"
|
2
|
+
require 'active_attr'
|
3
|
+
require "barrister-rails/version"
|
4
|
+
|
5
|
+
module Barrister
|
6
|
+
|
7
|
+
module Rails
|
8
|
+
|
9
|
+
class Client
|
10
|
+
|
11
|
+
DEFAULT_BARRISTER_TYPES = ['bool', 'int', 'string', 'float']
|
12
|
+
|
13
|
+
class BaseEtherealModel
|
14
|
+
|
15
|
+
include ActiveAttr::Model
|
16
|
+
|
17
|
+
# for building a route
|
18
|
+
def to_param
|
19
|
+
"#{id}"
|
20
|
+
end
|
21
|
+
|
22
|
+
# for determining inflection
|
23
|
+
def persisted?
|
24
|
+
!id.nil?
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
class InterfaceProxy
|
30
|
+
|
31
|
+
attr_reader :name
|
32
|
+
|
33
|
+
def initialize(name, client, fx_metadata, transmute_to_model)
|
34
|
+
@name = name
|
35
|
+
@client = client
|
36
|
+
@fx_metadata = fx_metadata
|
37
|
+
@transmute_to_model = transmute_to_model
|
38
|
+
end
|
39
|
+
|
40
|
+
def method_missing(name, *args)
|
41
|
+
result = @client.send(@name).send(name, *args)
|
42
|
+
|
43
|
+
if @transmute_to_model == true and DEFAULT_BARRISTER_TYPES.include?(@fx_metadata[name][:type]) == false
|
44
|
+
cast result, @fx_metadata[name][:type], @fx_metadata[name][:is_array]
|
45
|
+
else
|
46
|
+
result
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def ensure_const(type)
|
51
|
+
return Object.const_get(type) if Object.const_defined?(type)
|
52
|
+
|
53
|
+
klass = Class.new(BaseEtherealModel)
|
54
|
+
|
55
|
+
a = attributes_for_type(type).map { |name| "attribute :#{name};" }
|
56
|
+
|
57
|
+
klass.class_eval a.join('')
|
58
|
+
|
59
|
+
Object.send(:const_set, type, klass)
|
60
|
+
|
61
|
+
klass
|
62
|
+
end
|
63
|
+
|
64
|
+
def cast(result, type, is_array)
|
65
|
+
klass = ensure_const(type)
|
66
|
+
|
67
|
+
if is_array
|
68
|
+
result.map { |result| klass.new result }
|
69
|
+
else
|
70
|
+
klass.new result
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def attributes_for_type(type)
|
75
|
+
structs = @client
|
76
|
+
.instance_variable_get('@contract')
|
77
|
+
.instance_variable_get('@structs')
|
78
|
+
|
79
|
+
all_struct_fields([], structs[type], structs).map { |f| f['name'] }
|
80
|
+
end
|
81
|
+
|
82
|
+
def all_struct_fields(arr, struct, structs)
|
83
|
+
struct["fields"].each do |f|
|
84
|
+
arr << f
|
85
|
+
end
|
86
|
+
|
87
|
+
if struct["extends"]
|
88
|
+
parent = structs[struct["extends"]]
|
89
|
+
if parent
|
90
|
+
return all_struct_fields(arr, parent, structs)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
return arr
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
def initialize(transport_or_uri, opts={})
|
100
|
+
transport = transport_or_uri.is_a?(String) ? Barrister::HttpTransport.new(transport_or_uri) : transport_or_uri
|
101
|
+
@client = Barrister::Client.new(transport)
|
102
|
+
@custom_types = Hash.new
|
103
|
+
|
104
|
+
interfaces = @client
|
105
|
+
.instance_variable_get('@contract')
|
106
|
+
.interfaces
|
107
|
+
|
108
|
+
unless opts[:transmute_to_model] == false
|
109
|
+
pairs = interfaces
|
110
|
+
.map { |iface| iface.functions }
|
111
|
+
.flatten
|
112
|
+
.map { |fx| [fx.name.to_sym, { type: fx.returns['type'], is_array: fx.returns['is_array'] } ] }
|
113
|
+
|
114
|
+
fx_metadata = Hash[pairs]
|
115
|
+
else
|
116
|
+
fx_metadata = {}
|
117
|
+
end
|
118
|
+
|
119
|
+
@interface_proxies = interfaces.map { |iface| InterfaceProxy.new iface.name, @client, fx_metadata, opts[:transmute_to_model] != false }
|
120
|
+
end
|
121
|
+
|
122
|
+
def method_missing(name, *args)
|
123
|
+
name_as_string = name.to_s
|
124
|
+
@interface_proxies.find { |iface_proxy| iface_proxy.name == name_as_string }
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: barrister-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Erin Swenson-Healey
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: barrister
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: active_attr
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.0'
|
69
|
+
description: More to come!
|
70
|
+
email:
|
71
|
+
- erin.swenson.healey@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- barrister-rails.gemspec
|
83
|
+
- lib/barrister-rails.rb
|
84
|
+
- lib/barrister-rails/version.rb
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
homepage: ''
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.2.2
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: A Rails-oriented wrapper for the Barrister client.
|
110
|
+
test_files:
|
111
|
+
- spec/spec_helper.rb
|