faraday_resource 0.1.2 → 0.1.3
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 +4 -4
- data/README.md +155 -1
- data/faraday_resource.gemspec +2 -2
- data/lib/faraday_resource/base.rb +38 -16
- data/lib/faraday_resource/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e2fb02c19a3344fba6d826e7ef018a2968d7185
|
4
|
+
data.tar.gz: d24468b32baa8324b73e0e17026617853a1d7e72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3cb5432c68b1d0095cc048847d334a9edf3c5d2ed2990306c354a4f5b27e3a04d0e43be188d77606f58a4038f06b940f68dcf43d0c4af656cf99d3875bd8a08e
|
7
|
+
data.tar.gz: 8684a7642790cabec7bdc4abf17e6d04b786ed7a267585789691eb53928f46cfb76c811d03dabc6e9eb72423917607f6e60bf4d29dc08f2c7a0348317b0f6ab5
|
data/README.md
CHANGED
@@ -22,7 +22,161 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
|
26
|
+
|
27
|
+
such as:
|
28
|
+
|
29
|
+
```
|
30
|
+
|
31
|
+
class User
|
32
|
+
include FaradayResource::Base
|
33
|
+
|
34
|
+
set_url 'http://localhost:5100'
|
35
|
+
|
36
|
+
set_content_type 'application/json'
|
37
|
+
|
38
|
+
get :load, 'url' => '/users/:id', 'params' => {:test => 'dfdjfk', :id => 1}
|
39
|
+
|
40
|
+
collection do
|
41
|
+
get :get_list, 'url' => '/users', 'is_array' => true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
now you can use User class such as:
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
```
|
51
|
+
user = User.new
|
52
|
+
response = user.load #=> fetch data
|
53
|
+
user.name #=> get name
|
54
|
+
response.status #=> status code faraday response
|
55
|
+
```
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
if is_array is true return response and array[user] or only response
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
```
|
65
|
+
response, users = User.get_list #=> only when is_array is true
|
66
|
+
response.status #=> status code faraday response
|
67
|
+
users.class #=> Array, array[user]
|
68
|
+
|
69
|
+
response = User.get_list #=> when is_array is false
|
70
|
+
```
|
71
|
+
|
72
|
+
overwrite url params
|
73
|
+
|
74
|
+
|
75
|
+
```
|
76
|
+
User.get_list({
|
77
|
+
:url => '/other_users',
|
78
|
+
:params => {
|
79
|
+
:q => 'wp'
|
80
|
+
}
|
81
|
+
})
|
82
|
+
|
83
|
+
#now faraday use '/other_users' and params will merge this params
|
84
|
+
```
|
85
|
+
|
86
|
+
|
87
|
+
you can set custom parse function for instance_methods (default JSON.parse)
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
```
|
92
|
+
|
93
|
+
class User
|
94
|
+
set_parse do |body|
|
95
|
+
JSON.parse(body)['entity']
|
96
|
+
end
|
97
|
+
end
|
98
|
+
```
|
99
|
+
|
100
|
+
|
101
|
+
you can set custom array_parse function for is_array is true (default JSON.parse)
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
```
|
106
|
+
class User
|
107
|
+
set_array_parse do |body|
|
108
|
+
JSON.parse(body)['entities']
|
109
|
+
end
|
110
|
+
end
|
111
|
+
```
|
112
|
+
|
113
|
+
|
114
|
+
you can set global setting (will be overwitten by class set_url..)
|
115
|
+
|
116
|
+
|
117
|
+
```
|
118
|
+
FaradayResource.configure do |settings|
|
119
|
+
settings.url = 'http://localhost:5100'
|
120
|
+
settings.content_type = 'application/json'
|
121
|
+
end
|
122
|
+
|
123
|
+
```
|
124
|
+
|
125
|
+
url params xxx will replaced by params[:xxx] or params[xxx]
|
126
|
+
|
127
|
+
```
|
128
|
+
class User
|
129
|
+
|
130
|
+
get :load, 'url' => '/users/:id', 'params' => {:test => 'dfdjfk', :id => 1} do |params, instance|
|
131
|
+
params['test'] = instance.name
|
132
|
+
params
|
133
|
+
end
|
134
|
+
end
|
135
|
+
```
|
136
|
+
|
137
|
+
|
138
|
+
get/post/put/delete methods (not in collection) can accept block (params, instance then return params)
|
139
|
+
|
140
|
+
|
141
|
+
```
|
142
|
+
class User
|
143
|
+
|
144
|
+
put :update, 'url' => '/users/:id', 'params' => {} do |params, instance|
|
145
|
+
params['user'] = {
|
146
|
+
:name => instance.name,
|
147
|
+
:age => instance.age
|
148
|
+
}
|
149
|
+
params
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
u = User.new({id: 2})
|
154
|
+
u.load
|
155
|
+
u.name = 'zkf'
|
156
|
+
u.update
|
157
|
+
```
|
158
|
+
|
159
|
+
can assign value for attributes, check stale?
|
160
|
+
|
161
|
+
```
|
162
|
+
u = User.new({id: 2})
|
163
|
+
u.load
|
164
|
+
u.name #=> 'wp'
|
165
|
+
u.name = 'zkf'
|
166
|
+
u.name #=> 'zkf'
|
167
|
+
u.stale? #=> true
|
168
|
+
u.stale_attributes #=> {'name': 'wp'}
|
169
|
+
```
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
##other
|
174
|
+
|
175
|
+
I think this gem will help communicating between app servers (soa)
|
176
|
+
|
177
|
+
我水平有限(low),如果有什么好的想法(good tips),可以联系我(concat me) qq:524162910 email:wpcreep@gmail.com
|
178
|
+
|
179
|
+
|
26
180
|
|
27
181
|
## Development
|
28
182
|
|
data/faraday_resource.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["wpzero"]
|
10
10
|
spec.email = ["wangping@galiumsof.com"]
|
11
11
|
|
12
|
-
spec.summary = %q{
|
13
|
-
spec.description = %q{
|
12
|
+
spec.summary = %q{REST resource similar to angular resource based on faraday}
|
13
|
+
spec.description = %q{REST resource similar to angular resource based on faraday}
|
14
14
|
spec.homepage = "https://github.com/wpzero/faraday_resource"
|
15
15
|
|
16
16
|
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
@@ -1,6 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
# 这个是一个给block绑定一个object的方法
|
1
|
+
# 这个是一个给proc绑定一个object的方法
|
4
2
|
class Proc
|
5
3
|
def call_with_obj(obj, *args)
|
6
4
|
m = nil
|
@@ -17,26 +15,44 @@ module FaradayResource
|
|
17
15
|
module Base
|
18
16
|
def self.included(base)
|
19
17
|
base.extend(ClassMethods)
|
20
|
-
|
21
18
|
# 添加一个attributes的proxy
|
22
19
|
base.class_eval do
|
23
20
|
def method_missing(name, *args)
|
24
|
-
# 可以代理到attributes
|
21
|
+
# 可以代理到attributes的取值
|
25
22
|
if self.attributes.keys.include? name
|
26
23
|
return self.attributes[name]
|
27
24
|
elsif self.attributes.keys.include? name.to_s
|
28
25
|
return self.attributes[name.to_s]
|
29
26
|
end
|
27
|
+
# 可以代理到attributes的赋值
|
28
|
+
result = /^(.+)=$/.match(name.to_s)
|
29
|
+
if result && result[1] && self.attributes[result[1]]
|
30
|
+
# 如果第一次修改旧的记录那么就记录老的数据
|
31
|
+
self.stale_attributes[result[1]] = self.attributes[result[1]] unless self.stale_attributes[result[1]]
|
32
|
+
return self.attributes[result[1]] = args[0]
|
33
|
+
elsif result && result[1] && self.attributes[:"#{result[1]}"]
|
34
|
+
# 如果第一次修改旧的记录那么就记录老的数据
|
35
|
+
self.stale_attributes[:"#{result[1]}"] = self.attributes[:"#{result[1]}"] unless self.stale_attributes[:"#{result[1]}"]
|
36
|
+
return self.attributes[:"#{result[1]}"] = args[0]
|
37
|
+
end
|
38
|
+
# 否则不处理
|
30
39
|
super
|
31
40
|
end
|
32
41
|
end
|
33
|
-
|
34
42
|
# 设置一个attributes来存返回的数据
|
35
43
|
base.class_eval do
|
36
|
-
|
44
|
+
# 存储 attributes 和 旧的 attribtues
|
45
|
+
attr_accessor :attributes, :stale_attributes
|
37
46
|
|
47
|
+
# 初始化
|
38
48
|
def initialize attrs={}
|
39
49
|
@attributes = attrs
|
50
|
+
@stale_attributes = {}
|
51
|
+
end
|
52
|
+
|
53
|
+
# 判断是否有修改
|
54
|
+
def stale?
|
55
|
+
self.stale_attributes.length > 0
|
40
56
|
end
|
41
57
|
end
|
42
58
|
|
@@ -121,23 +137,27 @@ module FaradayResource
|
|
121
137
|
end
|
122
138
|
|
123
139
|
# post macro
|
124
|
-
def post name, method_settings={}
|
125
|
-
|
140
|
+
def post name, method_settings={}, &method_block
|
141
|
+
method_block ||= lambda{|params, instance|}
|
142
|
+
inner_create_method name, :post, method_settings, method_block
|
126
143
|
end
|
127
144
|
|
128
145
|
# get macro
|
129
|
-
def get name, method_settings={}
|
130
|
-
|
146
|
+
def get name, method_settings={}, &method_block
|
147
|
+
method_block ||= lambda{|params, instance|}
|
148
|
+
inner_create_method name, :get, method_settings, method_block
|
131
149
|
end
|
132
150
|
|
133
151
|
# put macro
|
134
|
-
def put name, method_settings={}
|
135
|
-
|
152
|
+
def put name, method_settings={}, &method_block
|
153
|
+
method_block ||= lambda{|params, instance|}
|
154
|
+
inner_create_method name, :put, method_settings, method_block
|
136
155
|
end
|
137
156
|
|
138
157
|
# delete macro
|
139
|
-
def delete name, method_settings={}
|
140
|
-
|
158
|
+
def delete name, method_settings={}, &method_block
|
159
|
+
method_block ||= lambda{|params, instance|}
|
160
|
+
inner_create_method name, :delete, method_settings, method_block
|
141
161
|
end
|
142
162
|
|
143
163
|
# array method 返回的是array[resource]
|
@@ -147,7 +167,7 @@ module FaradayResource
|
|
147
167
|
end
|
148
168
|
|
149
169
|
private
|
150
|
-
def inner_create_method name, method, method_settings
|
170
|
+
def inner_create_method name, method, method_settings, method_block
|
151
171
|
# 定义一个方法
|
152
172
|
define_method name do |settings={}|
|
153
173
|
dom_url = self.class.url || FaradayResource.config.url
|
@@ -169,6 +189,7 @@ module FaradayResource
|
|
169
189
|
response = conn.send method do |req|
|
170
190
|
req.url re_url
|
171
191
|
req.headers['Content-Type'] = settings['Content-Type'] || method_settings['Content-Type'] || self.class.content_type || FaradayResource.config.content_type
|
192
|
+
params = method_block.call(params, self)
|
172
193
|
req.params = params
|
173
194
|
end
|
174
195
|
|
@@ -177,6 +198,7 @@ module FaradayResource
|
|
177
198
|
parse = JSON.method(:parse)
|
178
199
|
parse = self.class.parse if self.class.parse
|
179
200
|
self.attributes = parse.call(response.body)
|
201
|
+
self.stale_attributes = {}
|
180
202
|
end
|
181
203
|
end
|
182
204
|
return response
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday_resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- wpzero
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,7 +52,7 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
description:
|
55
|
+
description: REST resource similar to angular resource based on faraday
|
56
56
|
email:
|
57
57
|
- wangping@galiumsof.com
|
58
58
|
executables: []
|
@@ -93,5 +93,5 @@ rubyforge_project:
|
|
93
93
|
rubygems_version: 2.4.7
|
94
94
|
signing_key:
|
95
95
|
specification_version: 4
|
96
|
-
summary:
|
96
|
+
summary: REST resource similar to angular resource based on faraday
|
97
97
|
test_files: []
|