plaza 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +137 -2
- data/lib/plaza.rb +6 -13
- data/lib/plaza/configuration.rb +22 -13
- data/lib/plaza/connection.rb +22 -0
- data/lib/plaza/inflector.rb +24 -2
- data/lib/plaza/models/restful_model.rb +32 -3
- data/lib/plaza/request.rb +3 -15
- data/lib/plaza/version.rb +1 -1
- data/plaza.gemspec +1 -3
- data/spec/plaza/configuration_spec.rb +27 -0
- data/spec/plaza/connection_spec.rb +29 -0
- data/spec/plaza/inflector_spec.rb +44 -14
- data/spec/plaza/models/restful_model_spec.rb +61 -12
- data/spec/plaza/plaza_spec.rb +7 -0
- data/spec/plaza/request_spec.rb +7 -12
- data/spec/plaza/support/middleware.rb +24 -0
- metadata +14 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7b1ce8fd7612e0e70f87446757ae861a500800d4
|
4
|
+
data.tar.gz: 509e260bbdf978f2c5624a19c5419ae59e9c542d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4915af07edad5d501e7b4aac700907d109838ff991a44a7d366c2ad45250b8436f570369caf4f3efb5d0fdf9cbc2ab3fc3b6dc61b611906177d1f0eac77ff345
|
7
|
+
data.tar.gz: 69efd7d83468172a51310e3f0ee3220bcf259dfaba2c51d63105fdeb231e08c0104785e553e6093f48d0d3451f091504a3205f7beb6f4aab8699855b42d3f87f
|
data/README.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# Plaza
|
2
2
|
|
3
|
-
|
3
|
+
_Because rest areas are more fun with a service plaza_
|
4
|
+
|
5
|
+
Plaza is a client side gem that works with [RestArea][1] to provide an ActiveRecord like experience
|
6
|
+
in dealing with a JSON rest API. Plaza uses [Virtus][2] and [Faraday][3] under the hood to access a
|
7
|
+
json api, so most things that work with those projects will also work with Plaza.
|
4
8
|
|
5
9
|
## Installation
|
6
10
|
|
@@ -20,12 +24,143 @@ Or install it yourself as:
|
|
20
24
|
|
21
25
|
## Usage
|
22
26
|
|
23
|
-
|
27
|
+
### Creating a Plaza Model
|
28
|
+
|
29
|
+
Rest API backed plaza models can be created like any other class, here's an example
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
require 'plaza'
|
33
|
+
|
34
|
+
class Thing
|
35
|
+
include Plaza::RestfulModel
|
36
|
+
|
37
|
+
attribute :name, String
|
38
|
+
attribute :amajig_id, Integer
|
39
|
+
|
40
|
+
has_many :amabobs, 'Amajing' # 1
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
**Note 1**: Valid: plural symbols: `:amabobs`, classes: `MyModule::Amabob`, strings: `'MyModule::Amabob'`
|
45
|
+
|
46
|
+
See [Virtus][2] for more information on configuring attributes.
|
47
|
+
|
48
|
+
### Configuring Plaza
|
49
|
+
|
50
|
+
At some point before you use a Plaza model, you need to configure Plaza, at the bare minimum you
|
51
|
+
need to tell plaza the base url to use for the rest_area api. You can optionally configure the
|
52
|
+
logger, cache store, and faraday middleware that plaza models will use.
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
Plaza.configure do
|
56
|
+
base_url 'http://www.example.com/rest' # Required
|
57
|
+
logger Logger.new(STDOUT) # Default
|
58
|
+
cache_store MemoryStore.new # Default, recommend `Rails.cache` for rails apps
|
59
|
+
use Faraday::SomeMiddleware # Add faraday middleware useing use
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
#### Cache Store
|
64
|
+
|
65
|
+
The store where cached responses are stored, for rails apps we recommend that you just set this
|
66
|
+
to `Rails.cache`. Plaza uses [Faraday Http Cache][4] for caching, refer to their documentation for
|
67
|
+
more information on what the store can be.
|
68
|
+
|
69
|
+
#### Faraday Middleware
|
70
|
+
|
71
|
+
Becasue Plaza works on top of [Faraday][3], you can optionaly add additional middleware to Plaza using the
|
72
|
+
`use` keyword inside the `Plaza.configure` block
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
Plaza.configure do
|
76
|
+
base_url 'http://my_domain.com/rest'
|
77
|
+
use Foobar::SomeMiddleware, Goobar::MoreMiddleware
|
78
|
+
use Moobar::MoreMiddleware
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
Middleware is applied in the same order that Faraday would apply it; the first middleware listed
|
83
|
+
wraps the second middleware which wraps the thrid middleware and so forth until the request is made.
|
84
|
+
|
85
|
+
#### Multiple Services
|
86
|
+
|
87
|
+
If your plaza models need to connect to multiple services with different base urls, they can be
|
88
|
+
configured as such:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
Plaza.configure :my_first_service do
|
92
|
+
base_url 'http://www.first_example.com/rest'
|
93
|
+
end
|
94
|
+
|
95
|
+
Plaza.configure :my_second_service do
|
96
|
+
base_url 'http://www.later_example.com/rest'
|
97
|
+
end
|
98
|
+
|
99
|
+
class Amabob
|
100
|
+
include Plaza::RestfulModel
|
101
|
+
plaza_config :my_first_service # <-- This tells the model to use the :my_first_service configuration
|
102
|
+
|
103
|
+
attribute :thing_id, Integer
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
### Using Plaza Models
|
108
|
+
|
109
|
+
You can:
|
110
|
+
|
111
|
+
Create new models: `my_thing = Thing.new(:name => 'Fred')` (not persisted to api)
|
112
|
+
|
113
|
+
Create models: `my_thing = Thing.create(:name => 'Bob')`. This results in an POST api call like
|
114
|
+
`POST http://example.com/rest/things`
|
115
|
+
|
116
|
+
Find existing models: `my_thing = Thing.find(10)`. This results in a GET API call like `GET
|
117
|
+
http://example.com/things/10`
|
118
|
+
|
119
|
+
Pass query string to API: `Thing.were(:name = 'bob')`. This results in an api call like `GET
|
120
|
+
http://example.com/rest/things?name=bob`. Returns an array.
|
121
|
+
|
122
|
+
Update model attributes: `my_thing.name = 'Kevin'`
|
123
|
+
|
124
|
+
Save new or update existing models: `my_thing.save`. This results in either a PUT or POST to the api
|
125
|
+
depending on if `my_thing.id` is `nil` or not.
|
126
|
+
|
127
|
+
Delete Models: `my_thing.delete`
|
128
|
+
|
129
|
+
Get associated objects: `amabobs_array = my_thing.amabobs`. This requires that you define the
|
130
|
+
`has_many` relationship in the class definition. (See Thing definition above)
|
131
|
+
|
132
|
+
If you need make associations as part of a module, you will need to specify the `has_many` association as follows
|
133
|
+
|
134
|
+
module Foobar
|
135
|
+
class Thing
|
136
|
+
include Plaza::RestfulModel
|
137
|
+
has_many Foobar::Amajig # Perfered Method, but you may run into issues with load order
|
138
|
+
has_many 'Foobar::Amajig' # Alternative you can specify the class as a string.
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
Get related models for which you have the foreign key: `my_thing = a_mabob.thing`
|
143
|
+
Note on this: If you ask a rest model for a attribute and it doesn't have it, but it has the
|
144
|
+
same attribute with an underscore id, it's smart enough to know thats a foreign key and go off and
|
145
|
+
fetch the related rest model.
|
146
|
+
|
147
|
+
Want to know more, go checkout the code, the guts of it are located at
|
148
|
+
`lib/plaza/models/restful_model.rb`
|
24
149
|
|
25
150
|
## Contributing
|
26
151
|
|
27
152
|
1. Fork it ( https://github.com/[my-github-username]/plaza/fork )
|
28
153
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
154
|
+
3. Add Specs!
|
29
155
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
156
|
4. Push to the branch (`git push origin my-new-feature`)
|
31
157
|
5. Create a new Pull Request
|
158
|
+
|
159
|
+
## STD (Stuff To Do) before 1.0.0
|
160
|
+
|
161
|
+
4. Add Support for messages (see rest_area)
|
162
|
+
|
163
|
+
[1]:https://github.com/bguest/rest_area
|
164
|
+
[2]:https://github.com/solnic/virtus
|
165
|
+
[3]:https://github.com/lostisland/faraday
|
166
|
+
[4]:https://github.com/plataformatec/faraday-http-cache
|
data/lib/plaza.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'virtus'
|
2
|
-
require "rack/cache"
|
3
2
|
|
4
3
|
require 'plaza/configuration'
|
5
4
|
require "plaza/version"
|
@@ -8,21 +7,10 @@ require 'plaza/request'
|
|
8
7
|
require 'plaza/response'
|
9
8
|
require 'plaza/adapters'
|
10
9
|
require 'plaza/inflector'
|
11
|
-
require '
|
10
|
+
require 'plaza/connection'
|
12
11
|
|
13
12
|
module Plaza
|
14
13
|
|
15
|
-
class << self
|
16
|
-
def enable_cache
|
17
|
-
#this makes it so that we adhere to http cache headers when issuing
|
18
|
-
#requests
|
19
|
-
require 'rack/cache'
|
20
|
-
RestClient.enable Rack::Cache,
|
21
|
-
:metastore => self.configuration.meta_store,
|
22
|
-
:entitystore => self.configuration.entity_store
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
14
|
def self.configuration(component_name = :default)
|
27
15
|
@configurations ||= {}
|
28
16
|
@configurations[component_name] ||= Plaza::Configuration.new
|
@@ -32,6 +20,11 @@ module Plaza
|
|
32
20
|
self.configuration(component_name).instance_eval(&block) if block_given?
|
33
21
|
end
|
34
22
|
|
23
|
+
def self.connection(component_name = :default)
|
24
|
+
@connections ||= {}
|
25
|
+
@connections[component_name] ||= Plaza::Connection.for(component_name)
|
26
|
+
end
|
27
|
+
|
35
28
|
def self.adapter(class_name)
|
36
29
|
Plaza.const_get("#{class_name}Adapter").new
|
37
30
|
end
|
data/lib/plaza/configuration.rb
CHANGED
@@ -3,31 +3,40 @@ require 'logger'
|
|
3
3
|
module Plaza
|
4
4
|
class Configuration
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
attr_accessor :middleware
|
7
|
+
attr_accessor :default_middleware
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@default_middleware = [
|
11
|
+
Plaza::Middleware::Exceptions,
|
12
|
+
Plaza::Middleware::UserId
|
13
|
+
]
|
14
|
+
@middleware = []
|
15
|
+
end
|
16
|
+
|
17
|
+
def middleware
|
18
|
+
@middleware + default_middleware
|
19
|
+
end
|
8
20
|
|
9
21
|
def base_url(url = nil)
|
10
22
|
url ? @url = url : @url
|
11
23
|
end
|
12
24
|
alias_method :base_url=, :base_url
|
13
25
|
|
26
|
+
def cache_store(store = nil)
|
27
|
+
store ? @cache_store = store : @cache_store
|
28
|
+
end
|
29
|
+
alias_method :cache_store=, :cache_store
|
30
|
+
|
14
31
|
def logger(logger = nil)
|
15
32
|
@logger ||= Logger.new(STDOUT)
|
16
33
|
logger ? @logger = logger : @logger
|
17
34
|
end
|
18
35
|
alias_method :logger=, :logger
|
19
36
|
|
20
|
-
def
|
21
|
-
@
|
37
|
+
def use(*ware)
|
38
|
+
@middleware << ware
|
39
|
+
@middleware.flatten!
|
22
40
|
end
|
23
|
-
|
24
|
-
def cache_meta_store
|
25
|
-
@cache_meta_store ||= 'file:/tmp/cache/meta'
|
26
|
-
end
|
27
|
-
|
28
|
-
def cache_entity_store
|
29
|
-
@cache_meta_store ||= 'file:/tmp/cache/body'
|
30
|
-
end
|
31
|
-
|
32
41
|
end
|
33
42
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Plaza
|
2
|
+
module Connection
|
3
|
+
|
4
|
+
def self.for(config_sym= :default)
|
5
|
+
config = Plaza.configuration(config_sym)
|
6
|
+
Faraday.new(config.base_url) do |conn|
|
7
|
+
conn.request :json
|
8
|
+
conn.response :json, :content_type => /\bjson$/
|
9
|
+
|
10
|
+
config.middleware.each do |middleware|
|
11
|
+
conn.use middleware
|
12
|
+
end
|
13
|
+
conn.use :http_cache, store: config.cache_store, logger: config.logger
|
14
|
+
|
15
|
+
conn.headers[:accept] = 'application/json'
|
16
|
+
|
17
|
+
conn.adapter Faraday.default_adapter
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
data/lib/plaza/inflector.rb
CHANGED
@@ -2,14 +2,36 @@ module Plaza
|
|
2
2
|
module Inflector
|
3
3
|
extend self
|
4
4
|
|
5
|
+
def classify(table_name)
|
6
|
+
singularize(table_name.split('_').map(&:capitalize).join)
|
7
|
+
end
|
8
|
+
|
9
|
+
def pluralize(str)
|
10
|
+
str.strip!
|
11
|
+
str.gsub!(/y$/,'ies')
|
12
|
+
str << 's' unless str[-1] == 's'
|
13
|
+
str
|
14
|
+
end
|
15
|
+
|
5
16
|
def singularize(str)
|
6
17
|
str.strip!
|
7
18
|
str.gsub!(/ies$/,'y')
|
8
19
|
str.chomp('s')
|
9
20
|
end
|
10
21
|
|
11
|
-
def
|
12
|
-
|
22
|
+
def tableize(str)
|
23
|
+
pluralize(underscore(str))
|
13
24
|
end
|
25
|
+
|
26
|
+
def underscore(str)
|
27
|
+
return str unless str =~ /[A-Z-]|::/
|
28
|
+
word = str.to_s.gsub(/::/, '/')
|
29
|
+
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
|
30
|
+
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
31
|
+
word.tr!("-", "_")
|
32
|
+
word.downcase!
|
33
|
+
word
|
34
|
+
end
|
35
|
+
|
14
36
|
end
|
15
37
|
end
|
@@ -59,9 +59,8 @@ module Plaza
|
|
59
59
|
#
|
60
60
|
def has_many(*relations)
|
61
61
|
relations.each do |r|
|
62
|
-
define_method(r) do
|
63
|
-
|
64
|
-
Plaza.const_get(class_name).collection(adapter.has_many(self.id,r))
|
62
|
+
define_method(sym = has_many_symbol_for(r)) do
|
63
|
+
class_for(r).collection(adapter.has_many(self.id,sym))
|
65
64
|
end
|
66
65
|
end
|
67
66
|
end
|
@@ -72,6 +71,22 @@ module Plaza
|
|
72
71
|
end
|
73
72
|
|
74
73
|
alias_method :plaza_config=, :plaza_config
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def has_many_symbol_for(identifier)
|
78
|
+
if identifier.kind_of? Class
|
79
|
+
identifier.plural_name
|
80
|
+
elsif identifier.kind_of? String
|
81
|
+
Inflector.tableize(identifier.split('::').last)
|
82
|
+
elsif identifier.kind_of? Symbol
|
83
|
+
identifier
|
84
|
+
else
|
85
|
+
raise TypeError.new("Can't convert to has_many symbol")
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
|
75
90
|
end
|
76
91
|
|
77
92
|
def plaza_config
|
@@ -135,5 +150,19 @@ module Plaza
|
|
135
150
|
end
|
136
151
|
end
|
137
152
|
|
153
|
+
protected
|
154
|
+
|
155
|
+
def class_for(identifier)
|
156
|
+
if identifier.kind_of?(Symbol)
|
157
|
+
klass = Plaza.const_get(Plaza::Inflector.classify(identifier.to_s))
|
158
|
+
elsif identifier.kind_of?(String)
|
159
|
+
klass = Object.const_get(identifier)
|
160
|
+
elsif identifier.kind_of?(Class)
|
161
|
+
klass = identifier
|
162
|
+
else
|
163
|
+
raise TypeError.new("Can't convert to has_many relation")
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
138
167
|
end
|
139
168
|
end
|
data/lib/plaza/request.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'faraday'
|
2
|
+
require 'faraday/http_cache'
|
2
3
|
require 'faraday_middleware'
|
3
4
|
require_relative 'middleware/user_id'
|
4
5
|
require_relative 'middleware/exceptions'
|
@@ -9,23 +10,10 @@ module Plaza
|
|
9
10
|
attr_reader :logger
|
10
11
|
|
11
12
|
def initialize(config_sym= :default)
|
12
|
-
|
13
|
-
@
|
14
|
-
conn.request :json
|
15
|
-
conn.response :json, :content_type => /\bjson$/
|
16
|
-
|
17
|
-
conn.use Plaza::Middleware::Exceptions
|
18
|
-
conn.use Plaza::Middleware::UserId
|
19
|
-
|
20
|
-
conn.headers[:accept] = 'application/json'
|
21
|
-
yield(conn) if block_given?
|
22
|
-
|
23
|
-
conn.adapter Faraday.default_adapter
|
24
|
-
end
|
25
|
-
@logger = config.logger
|
13
|
+
@connection = Plaza.connection(config_sym)
|
14
|
+
@logger = Plaza.configuration(config_sym).logger
|
26
15
|
end
|
27
16
|
|
28
|
-
|
29
17
|
def get(*args)
|
30
18
|
Response.new(connection.get *args)
|
31
19
|
end
|
data/lib/plaza/version.rb
CHANGED
data/plaza.gemspec
CHANGED
@@ -23,10 +23,8 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.add_development_dependency 'rspec', '~> 2.99'
|
24
24
|
spec.add_development_dependency 'mocha', '~> 1.1'
|
25
25
|
|
26
|
-
spec.add_runtime_dependency "rest-client", "~> 1.6"
|
27
|
-
spec.add_runtime_dependency "rest-client-components"
|
28
26
|
spec.add_runtime_dependency 'faraday', '~> 0.9.0'
|
29
27
|
spec.add_runtime_dependency 'faraday_middleware', '~>0.9.1'
|
30
|
-
spec.add_runtime_dependency
|
28
|
+
spec.add_runtime_dependency 'faraday-http-cache', '~>0.4.2'
|
31
29
|
spec.add_runtime_dependency "virtus", '~> 1.0'
|
32
30
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require_relative 'support/middleware'
|
3
|
+
|
4
|
+
describe Plaza::Configuration do
|
5
|
+
|
6
|
+
let(:config){Plaza::Configuration.new}
|
7
|
+
|
8
|
+
describe '#use' do
|
9
|
+
expected_stack= [
|
10
|
+
Example::HelloMiddleware,
|
11
|
+
Example::GoodbyeMiddleware,
|
12
|
+
Plaza::Middleware::Exceptions,
|
13
|
+
Plaza::Middleware::UserId
|
14
|
+
]
|
15
|
+
|
16
|
+
it 'should add middleware to the faraday stack' do
|
17
|
+
config.use Example::HelloMiddleware
|
18
|
+
config.use Example::GoodbyeMiddleware
|
19
|
+
config.middleware.should == expected_stack
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should add middleware if added as a list' do
|
23
|
+
config.use Example::HelloMiddleware, Example::GoodbyeMiddleware
|
24
|
+
config.middleware.should == expected_stack
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require_relative 'support/middleware'
|
3
|
+
|
4
|
+
Plaza.configure :connection_spec do
|
5
|
+
logger NullLogger.new
|
6
|
+
use Example::HelloMiddleware
|
7
|
+
use Example::GoodbyeMiddleware
|
8
|
+
end
|
9
|
+
|
10
|
+
describe Plaza::Connection do
|
11
|
+
|
12
|
+
context 'with custom middeware' do
|
13
|
+
it 'should run middleware in correct order' do
|
14
|
+
stub_request(:get, "http://example.com/").
|
15
|
+
with(:headers => {
|
16
|
+
'Accept'=>'application/json',
|
17
|
+
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
|
18
|
+
'Greetings'=>'HelloGoodbye',
|
19
|
+
'User-Agent'=>'Faraday v0.9.0',
|
20
|
+
'X-User-Id'=>''}
|
21
|
+
).to_return(:status => 200, :body => "", :headers => {})
|
22
|
+
connection = Plaza::Connection.for(:connection_spec)
|
23
|
+
response = connection.get('http://example.com')
|
24
|
+
expect(response.headers).to eq( {'Greetings' => 'GoodbyeHello'} )
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -3,33 +3,63 @@ include Plaza
|
|
3
3
|
|
4
4
|
describe Plaza::Inflector do
|
5
5
|
|
6
|
+
describe '#classify' do
|
7
|
+
it 'should classify strings with and s at the end' do
|
8
|
+
expect(Inflector.classify('foobars ')).to eq 'Foobar'
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should classify strings that end in ies' do
|
12
|
+
expect(Inflector.classify('entries')).to eq('Entry')
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should work with underscores' do
|
16
|
+
expect(Inflector.classify('targeting_entries')).to eq('TargetingEntry')
|
17
|
+
expect(Inflector.classify('events_collections')).to eq('EventsCollection')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#pluralize' do
|
22
|
+
it 'should add s to end of thing' do
|
23
|
+
expect(Inflector.pluralize('thing')).to eq('things')
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should change y to ies' do
|
27
|
+
expect(Inflector.pluralize('entry')).to eq('entries')
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should not add another s' do
|
31
|
+
expect(Inflector.pluralize('things')).to eq('things')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
6
35
|
describe '#singularize' do
|
7
36
|
it 'should return singularized random word with s at the end' do
|
8
|
-
Inflector.singularize('foobars').
|
9
|
-
Inflector.singularize('intentss').
|
37
|
+
expect(Inflector.singularize('foobars')).to eq 'foobar'
|
38
|
+
expect(Inflector.singularize('intentss')).to eq 'intents'
|
10
39
|
end
|
11
40
|
|
12
41
|
it 'should change ies to y' do
|
13
|
-
Inflector.singularize('entries').
|
42
|
+
expect(Inflector.singularize('entries')).to eq 'entry'
|
14
43
|
end
|
15
44
|
|
16
45
|
it 'should not change ies in middle of word' do
|
17
|
-
Inflector.singularize('fiesties ').
|
46
|
+
expect(Inflector.singularize('fiesties ')).to eq 'fiesty'
|
18
47
|
end
|
19
48
|
end
|
20
49
|
|
21
|
-
describe '#
|
22
|
-
it '
|
23
|
-
Inflector.
|
24
|
-
|
25
|
-
|
26
|
-
it 'should classify strings that end in ies' do
|
27
|
-
Inflector.classify('entries').should == 'Entry'
|
50
|
+
describe '#tableize' do
|
51
|
+
it 'convert rails examples' do
|
52
|
+
expect(Inflector.tableize('RawScaledScorer')).to eq 'raw_scaled_scorers'
|
53
|
+
expect(Inflector.tableize('egg_and_ham')).to eq 'egg_and_hams'
|
54
|
+
expect(Inflector.tableize('fancyCategory')).to eq 'fancy_categories'
|
28
55
|
end
|
56
|
+
end
|
29
57
|
|
30
|
-
|
31
|
-
|
32
|
-
Inflector.
|
58
|
+
describe '#underscore' do
|
59
|
+
it 'convert rails examples' do
|
60
|
+
expect( Inflector.underscore('ActiveModel') ).to eq 'active_model'
|
61
|
+
expect( Inflector.underscore('ActiveModel::Errors')).to eq 'active_model/errors'
|
33
62
|
end
|
34
63
|
end
|
64
|
+
|
35
65
|
end
|
@@ -8,23 +8,31 @@ class Thing
|
|
8
8
|
attribute :amajig_id, Integer
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
include Plaza::RestfulModel
|
13
|
-
|
14
|
-
has_many :things, :amabobs
|
15
|
-
attribute :name
|
16
|
-
end
|
17
|
-
|
18
|
-
Plaza.configure :amabob do
|
11
|
+
Plaza.configure :foobar do
|
19
12
|
base_url 'http://www.example.com/rest'
|
20
13
|
logger NullLogger.new # Specs should STFU
|
21
14
|
end
|
22
15
|
|
23
|
-
|
16
|
+
module Foobar
|
17
|
+
class Amabob
|
18
|
+
include Plaza::RestfulModel
|
19
|
+
plaza_config :foobar
|
20
|
+
|
21
|
+
has_many 'Foobar::Amajing'
|
22
|
+
attribute :amajig_id, Integer
|
23
|
+
end
|
24
|
+
|
25
|
+
class Amajing
|
26
|
+
include Plaza::RestfulModel
|
27
|
+
plaza_config :foobar
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class Amajig
|
24
32
|
include Plaza::RestfulModel
|
25
|
-
plaza_config :amabob
|
26
33
|
|
27
|
-
|
34
|
+
has_many :things, Foobar::Amabob
|
35
|
+
attribute :name
|
28
36
|
end
|
29
37
|
|
30
38
|
describe Thing do
|
@@ -63,6 +71,15 @@ describe Thing do
|
|
63
71
|
}
|
64
72
|
}
|
65
73
|
|
74
|
+
let(:amabob_hash){
|
75
|
+
{
|
76
|
+
'amabob' => {
|
77
|
+
'id' => 3,
|
78
|
+
'amajig_id' => 2
|
79
|
+
}
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
66
83
|
let(:amabobs_hash){
|
67
84
|
{
|
68
85
|
'amabobs' =>[
|
@@ -78,6 +95,21 @@ describe Thing do
|
|
78
95
|
}
|
79
96
|
}
|
80
97
|
|
98
|
+
let(:amajings_hash){
|
99
|
+
{
|
100
|
+
'amajings'=>[
|
101
|
+
{
|
102
|
+
'id'=> 5,
|
103
|
+
'amabob_id'=> 4
|
104
|
+
},
|
105
|
+
{
|
106
|
+
'id'=> 6,
|
107
|
+
'amabob_id'=> 4
|
108
|
+
}
|
109
|
+
]
|
110
|
+
}
|
111
|
+
}
|
112
|
+
|
81
113
|
describe 'attributes' do
|
82
114
|
it{ should respond_to :errors }
|
83
115
|
|
@@ -151,6 +183,17 @@ describe Thing do
|
|
151
183
|
expect{Thing.find(1)}.to raise_error(Plaza::ConnectionError)
|
152
184
|
end
|
153
185
|
end
|
186
|
+
|
187
|
+
context 'with caching' do
|
188
|
+
it 'second request should be cached and not hit server' do
|
189
|
+
stub_request(:get, 'http://www.example.com/rest/amabobs/3.json').to_return(
|
190
|
+
headers: {'Cache-Control' => 'max-age=200'},
|
191
|
+
body:amabob_hash.to_json
|
192
|
+
).times(1).then.to_raise('Cache Not Working')
|
193
|
+
Foobar::Amabob.find(3)
|
194
|
+
expect{ Foobar::Amabob.find(3) }.not_to raise_error
|
195
|
+
end
|
196
|
+
end
|
154
197
|
end
|
155
198
|
|
156
199
|
describe '.where()' do
|
@@ -219,7 +262,13 @@ describe Thing do
|
|
219
262
|
it 'gets second has_many relationships' do
|
220
263
|
amajig = Amajig.new(amajig_hash['amajig'])
|
221
264
|
stub_request(:get, 'http://example.com/rest/amajigs/2/amabobs.json').to_return(body:amabobs_hash.to_json)
|
222
|
-
expect(amajig.amabobs.first.class).to be Amabob
|
265
|
+
expect(amajig.amabobs.first.class).to be Foobar::Amabob
|
266
|
+
end
|
267
|
+
|
268
|
+
it 'gets string has_many relationships' do
|
269
|
+
amabob = Foobar::Amabob.new(id:4)
|
270
|
+
stub_request(:get, 'http://www.example.com/rest/amabobs/4/amajings.json').to_return(body:amajings_hash.to_json)
|
271
|
+
expect(amabob.amajings.first.class).to be Foobar::Amajing
|
223
272
|
end
|
224
273
|
end
|
225
274
|
|
data/spec/plaza/request_spec.rb
CHANGED
@@ -6,6 +6,7 @@ describe Request do
|
|
6
6
|
context 'when Thread.current[:x_user_id] is set' do
|
7
7
|
|
8
8
|
before do Thread.current[:x_user_id] = 4242 end
|
9
|
+
after do Thread.current[:x_user_id] = nil end
|
9
10
|
|
10
11
|
%i(get post put delete).each do |method|
|
11
12
|
it "##{method} should add X-User-Id to headers" do
|
@@ -38,20 +39,14 @@ describe Request do
|
|
38
39
|
end
|
39
40
|
|
40
41
|
context "when trucker service is down" do
|
41
|
-
let(:request){
|
42
|
-
exception = Faraday::Adapter::Test::Stubs.new do |stub|
|
43
|
-
%i(get put post delete).each do |method|
|
44
|
-
stub.send(method, '/failblog') {raise Faraday::Error::ConnectionFailed.new('Connection Failed')}
|
45
|
-
end
|
46
|
-
end
|
47
|
-
request = Request.new do |conn|
|
48
|
-
conn.adapter :test, exception
|
49
|
-
end
|
50
|
-
request
|
51
|
-
}
|
52
42
|
%i(get put post delete).each do |method|
|
53
43
|
describe "#{method}" do
|
54
|
-
|
44
|
+
before do
|
45
|
+
stub_request(method, "http://example.com/failblog").
|
46
|
+
to_raise(Faraday::Error::ConnectionFailed.new('Connection Failed'))
|
47
|
+
end
|
48
|
+
|
49
|
+
let(:response){Request.new.send(method, '/failblog')}
|
55
50
|
|
56
51
|
it 'response code should be 503' do
|
57
52
|
expect{response}.to raise_error(Plaza::ConnectionError)
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Example
|
2
|
+
class HelloMiddleware < Faraday::Middleware
|
3
|
+
def call(request_env)
|
4
|
+
request_env.request_headers['Greetings'] ||= ""
|
5
|
+
request_env.request_headers['Greetings'] << 'Hello'
|
6
|
+
@app.call(request_env).on_complete do |response_env|
|
7
|
+
response_env.response_headers['Greetings'] ||= ""
|
8
|
+
response_env.response_headers['Greetings'] << 'Hello'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class GoodbyeMiddleware < Faraday::Middleware
|
14
|
+
def call(request_env)
|
15
|
+
request_env.request_headers['Greetings'] ||= ""
|
16
|
+
request_env.request_headers['Greetings'] << 'Goodbye'
|
17
|
+
@app.call(request_env).on_complete do |response_env|
|
18
|
+
response_env.response_headers['Greetings'] ||= ""
|
19
|
+
response_env.response_headers['Greetings'] << 'Goodbye'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: plaza
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- VMC Engineering
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,34 +66,6 @@ dependencies:
|
|
66
66
|
- - ~>
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '1.1'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: rest-client
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ~>
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '1.6'
|
76
|
-
type: :runtime
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ~>
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '1.6'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: rest-client-components
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - '>='
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '0'
|
90
|
-
type: :runtime
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - '>='
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: '0'
|
97
69
|
- !ruby/object:Gem::Dependency
|
98
70
|
name: faraday
|
99
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -123,19 +95,19 @@ dependencies:
|
|
123
95
|
- !ruby/object:Gem::Version
|
124
96
|
version: 0.9.1
|
125
97
|
- !ruby/object:Gem::Dependency
|
126
|
-
name:
|
98
|
+
name: faraday-http-cache
|
127
99
|
requirement: !ruby/object:Gem::Requirement
|
128
100
|
requirements:
|
129
101
|
- - ~>
|
130
102
|
- !ruby/object:Gem::Version
|
131
|
-
version:
|
103
|
+
version: 0.4.2
|
132
104
|
type: :runtime
|
133
105
|
prerelease: false
|
134
106
|
version_requirements: !ruby/object:Gem::Requirement
|
135
107
|
requirements:
|
136
108
|
- - ~>
|
137
109
|
- !ruby/object:Gem::Version
|
138
|
-
version:
|
110
|
+
version: 0.4.2
|
139
111
|
- !ruby/object:Gem::Dependency
|
140
112
|
name: virtus
|
141
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -168,6 +140,7 @@ files:
|
|
168
140
|
- lib/plaza/adapters/base_adapter.rb
|
169
141
|
- lib/plaza/adapters/restful_adapter.rb
|
170
142
|
- lib/plaza/configuration.rb
|
143
|
+
- lib/plaza/connection.rb
|
171
144
|
- lib/plaza/inflector.rb
|
172
145
|
- lib/plaza/middleware/exceptions.rb
|
173
146
|
- lib/plaza/middleware/user_id.rb
|
@@ -180,9 +153,13 @@ files:
|
|
180
153
|
- lib/plaza/version.rb
|
181
154
|
- plaza.gemspec
|
182
155
|
- spec/plaza/adapters/restful_adapter_spec.rb
|
156
|
+
- spec/plaza/configuration_spec.rb
|
157
|
+
- spec/plaza/connection_spec.rb
|
183
158
|
- spec/plaza/inflector_spec.rb
|
184
159
|
- spec/plaza/models/restful_model_spec.rb
|
160
|
+
- spec/plaza/plaza_spec.rb
|
185
161
|
- spec/plaza/request_spec.rb
|
162
|
+
- spec/plaza/support/middleware.rb
|
186
163
|
- spec/spec_helper.rb
|
187
164
|
homepage: ''
|
188
165
|
licenses:
|
@@ -210,7 +187,11 @@ specification_version: 4
|
|
210
187
|
summary: Client for rest_area gem
|
211
188
|
test_files:
|
212
189
|
- spec/plaza/adapters/restful_adapter_spec.rb
|
190
|
+
- spec/plaza/configuration_spec.rb
|
191
|
+
- spec/plaza/connection_spec.rb
|
213
192
|
- spec/plaza/inflector_spec.rb
|
214
193
|
- spec/plaza/models/restful_model_spec.rb
|
194
|
+
- spec/plaza/plaza_spec.rb
|
215
195
|
- spec/plaza/request_spec.rb
|
196
|
+
- spec/plaza/support/middleware.rb
|
216
197
|
- spec/spec_helper.rb
|