activerequest 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -0
- data/examples/README.md +35 -0
- data/examples/sinatra/app.rb +14 -7
- data/lib/active_request/base.rb +6 -2
- data/lib/active_request/belongs_to.rb +18 -7
- data/lib/active_request/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac83cebb3fb2d47f4364bbf316cf217b79bb238f
|
4
|
+
data.tar.gz: 508dfb2390af865cdbd9619ca7bdd7800073b9ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bfb9e47749ba3d5753ea0f3b4b77c5f90e1af9781a4f495e8eb09ca8912caa0a50ee1276a37c29ddd5bcfcdc717913bdfad28227fde77992102fe9614c74cc02
|
7
|
+
data.tar.gz: de8ba12c46bbab1aa4c17926d63dd51a227b173332a72514e650fd4a135397bb685eef1975edd733ca9597034f6262f633bfcd74fff2b882c1feb8c964ac928b
|
data/README.md
CHANGED
@@ -28,6 +28,15 @@ ActiveRequest.configure do |config|
|
|
28
28
|
config.api_version = 'v1'
|
29
29
|
end
|
30
30
|
```
|
31
|
+
|
32
|
+
You can use headers for token.
|
33
|
+
```ruby
|
34
|
+
ActiveRequest.configure do |config|
|
35
|
+
config.uri = 'localhost:4567'
|
36
|
+
config.api_version = 'v1'
|
37
|
+
config.headers = { "token" => "*" }
|
38
|
+
end
|
39
|
+
```
|
31
40
|
## Usage
|
32
41
|
|
33
42
|
[Examples](https://github.com/xptavares/activerequest/blob/master/examples/README.md)
|
data/examples/README.md
CHANGED
@@ -50,3 +50,38 @@ blog.save # "PUT /v1/blogs/1.json?blog[id]=1&blog[title]=new%20title&blog[posts_
|
|
50
50
|
blog = Blog.find(1)
|
51
51
|
blog.delete # "DELETE /v1/blogs/1.json HTTP/1.1" 200
|
52
52
|
```
|
53
|
+
|
54
|
+
## Association
|
55
|
+
|
56
|
+
#### classes
|
57
|
+
```ruby
|
58
|
+
class Blog < ActiveRequest::Base
|
59
|
+
attr_accessor :id, :title
|
60
|
+
has_many :posts
|
61
|
+
end
|
62
|
+
|
63
|
+
class Post < ActiveRequest::Base
|
64
|
+
attr_accessor :id, :title, :body
|
65
|
+
belongs_to :blog
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
#### has many
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
blog = Blog.first
|
73
|
+
=> #<Blog:0x0056334e564ed8 @id=1, @title="title 1", @posts=[]>
|
74
|
+
blog.posts # "GET /v1/blogs/1/posts.json HTTP/1.1" 200
|
75
|
+
=> [#<Post:0x0056334e554240 @id=1, @title="title 1", @body="body 1", @blog=nil>, #<Post:0x0056334e5537a0 @id=2, @title="title 2", @body="body 2", @blog=nil>]
|
76
|
+
```
|
77
|
+
|
78
|
+
#### belongs to
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
post = Post.find 1
|
82
|
+
=> #<Post:0x007fb2c4112578 @blog_id=1, @blog=nil, @id=1, @title="title 1", @body="body 1">
|
83
|
+
post.blog # "GET /v1/blogs/1.json HTTP/1.1" 200
|
84
|
+
=> #<Blog:0x007fb2c40862d0 @posts=[], @id=1, @title="title 1">
|
85
|
+
post
|
86
|
+
=> #<Post:0x007fb2c4112578 @blog_id=1, @blog=#<Blog:0x007fb2c40862d0 @posts=[], @id=1, @title="title 1">, @id=1, @title="title 1", @body="body 1">
|
87
|
+
```
|
data/examples/sinatra/app.rb
CHANGED
@@ -3,14 +3,19 @@ require 'rubygems'
|
|
3
3
|
require "sinatra"
|
4
4
|
require "sinatra/json"
|
5
5
|
|
6
|
+
b1 = { id: 1, title: 'title 1' }
|
7
|
+
b2 = { id: 2, title: 'title 2' }
|
8
|
+
blogs = [b1, b2]
|
9
|
+
t1 = { id: 1, title: 'title 1', body: 'body 1', blog_id: 1 }
|
10
|
+
t2 = { id: 2, title: 'title 2', body: 'body 2', blog_id: 1 }
|
11
|
+
t3 = { id: 3, title: 'title 3', body: 'body 3', blog_id: 2 }
|
12
|
+
posts = [t1, t2, t3]
|
13
|
+
|
6
14
|
get '/' do
|
7
15
|
json foo: 'bar'
|
8
16
|
end
|
9
17
|
|
10
18
|
get '/v1/blogs.json' do
|
11
|
-
b1 = { id: 1, title: 'title 1' }
|
12
|
-
b2 = { id: 2, title: 'title 2' }
|
13
|
-
blogs = [b1, b2]
|
14
19
|
blogs = blogs.select { |b| b[:id].to_s == params[:id] } if params[:id]
|
15
20
|
blogs = blogs.select { |b| b[:title] == params[:title] } if params[:title]
|
16
21
|
json blogs: blogs
|
@@ -36,11 +41,13 @@ delete '/v1/blogs/:id.json' do
|
|
36
41
|
json blog: { id: id.to_i }
|
37
42
|
end
|
38
43
|
|
44
|
+
get '/v1/blogs/:blog_id/posts.json' do
|
45
|
+
blog_id = params[:blog_id].to_i
|
46
|
+
posts = posts.select { |p| p[:blog_id] == blog_id }
|
47
|
+
json "blog/posts" => posts
|
48
|
+
end
|
49
|
+
|
39
50
|
get '/v1/posts.json' do
|
40
|
-
t1 = { id: 1, title: 'title 1', body: 'body 1', blog_id: 1 }
|
41
|
-
t2 = { id: 2, title: 'title 2', body: 'body 2', blog_id: 1 }
|
42
|
-
t3 = { id: 3, title: 'title 3', body: 'body 3', blog_id: 2 }
|
43
|
-
posts = [t1, t2, t3]
|
44
51
|
posts = posts.select { |b| b[:id].to_s == params[:id] } if params[:id]
|
45
52
|
posts = posts.select { |b| b[:title] == params[:title] } if params[:title]
|
46
53
|
posts = posts.select { |b| b[:body] == params[:body] } if params[:body]
|
data/lib/active_request/base.rb
CHANGED
@@ -14,6 +14,10 @@ module ActiveRequest
|
|
14
14
|
attributes.each do |att|
|
15
15
|
send("#{att}=", options[att]) if options[att].present?
|
16
16
|
end
|
17
|
+
belongs_tos.each do |att|
|
18
|
+
simble = att[:foreign_key].to_sym
|
19
|
+
send("#{att[:foreign_key]}=", options[simble]) if options[simble].present?
|
20
|
+
end if belongs_tos
|
17
21
|
end
|
18
22
|
self.class.base_uri("#{ActiveRequest.configuration.uri}/#{ActiveRequest.configuration.api_version}/")
|
19
23
|
end
|
@@ -22,11 +26,11 @@ module ActiveRequest
|
|
22
26
|
name.underscore.pluralize
|
23
27
|
end
|
24
28
|
|
25
|
-
def self.new(
|
29
|
+
def self.new(*args, &blk)
|
26
30
|
alloc = allocate
|
27
|
-
alloc.instance_eval { initialize(*args, &blk) }
|
28
31
|
build_has_manys(alloc)
|
29
32
|
build_belongs_tos(alloc)
|
33
|
+
alloc.instance_eval { initialize(*args, &blk) }
|
30
34
|
alloc
|
31
35
|
end
|
32
36
|
end
|
@@ -8,10 +8,8 @@ module ActiveRequest
|
|
8
8
|
def belongs_to(association, options = nil)
|
9
9
|
@belongs_tos ||= []
|
10
10
|
class_name = !options.nil? && !options[:class_name].nil? ? options[:class_name] : (association.to_s.split.map(&:capitalize)*' ')
|
11
|
-
|
12
|
-
|
13
|
-
# set(association)
|
14
|
-
# end
|
11
|
+
foreign_key = !options.nil? && !options[:foreign_key].nil? ? options[:foreign_key] : "#{association}_id"
|
12
|
+
@belongs_tos << { association: association, class_name: class_name, foreign_key: foreign_key }
|
15
13
|
end
|
16
14
|
|
17
15
|
def belongs_tos
|
@@ -20,12 +18,25 @@ module ActiveRequest
|
|
20
18
|
|
21
19
|
def build_belongs_tos(alloc)
|
22
20
|
alloc.belongs_tos.each do |many|
|
21
|
+
alloc.instance_variable_set("@#{many[:foreign_key]}", nil)
|
23
22
|
alloc.instance_variable_set("@#{many[:association]}", nil)
|
24
23
|
define_method(many[:association]) do
|
25
|
-
alloc.instance_variable_get("@#{many[:association]}")
|
24
|
+
variable = alloc.instance_variable_get("@#{many[:association]}")
|
25
|
+
if id && variable.blank?
|
26
|
+
father_ojb = Object.const_get(many[:class_name])
|
27
|
+
variable = father_ojb.find send(many[:foreign_key])
|
28
|
+
send("#{many[:association]}=", variable) if variable
|
29
|
+
end
|
30
|
+
variable
|
26
31
|
end
|
27
|
-
define_method(
|
28
|
-
alloc.
|
32
|
+
define_method(many[:foreign_key]) do
|
33
|
+
alloc.instance_variable_get("@#{many[:foreign_key]}")
|
34
|
+
end
|
35
|
+
define_method("#{many[:association]}=") do |association_setter|
|
36
|
+
alloc.instance_variable_set("@#{many[:association]}", association_setter)
|
37
|
+
end
|
38
|
+
define_method("#{many[:foreign_key]}=") do |foreign_key_setter|
|
39
|
+
alloc.instance_variable_set("@#{many[:foreign_key]}", foreign_key_setter)
|
29
40
|
end
|
30
41
|
end if alloc.belongs_tos
|
31
42
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerequest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexandre Tavares
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-09-
|
11
|
+
date: 2016-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|