grape-client 0.1.0 → 0.2.0
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 +30 -6
- data/grape-client.gemspec +1 -1
- data/lib/grape_client/base.rb +30 -3
- data/lib/grape_client/collection.rb +14 -12
- data/lib/grape_client/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea69852f478ab39fc8bd0a7decc8977957026f1b
|
4
|
+
data.tar.gz: ffd3fd7206257e88a4533ca50e21de9ef0e8cb0c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2afdfe9f38b7836286dcd4760c44b24bbc05a61f13886a3c8662dd7165c149014f7a2d6f8d3f82a5a92f689286c924754ccef74f56846c94f9fdea8b5cdfb44
|
7
|
+
data.tar.gz: 6dec3eaa9399f5304ce403370d1c4483ac89c7a18a4764141d85cf227a0ce23ff498e0cc8a68a23550039a42d6a5329c314a1a8e99b12a1e273aee31726e918d
|
data/README.md
CHANGED
@@ -3,6 +3,9 @@
|
|
3
3
|
[](https://codeclimate.com/github/desofto/grape-client)
|
4
4
|
[](https://codeclimate.com/github/desofto/grape-client/coverage)
|
5
5
|
|
6
|
+
Simple access from your client to Grape APIs.
|
7
|
+
Automatically supports: kaminari pagination, network access, nested objects.
|
8
|
+
|
6
9
|
## Installation
|
7
10
|
|
8
11
|
Add this line to your application's Gemfile:
|
@@ -21,12 +24,33 @@ Or install it yourself as:
|
|
21
24
|
|
22
25
|
## Usage
|
23
26
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
27
|
+
```ruby
|
28
|
+
class User < GrapeClient::Base
|
29
|
+
self.site = 'http://localhost:3000'
|
30
|
+
self.user = 'user'
|
31
|
+
self.password = 'password'
|
32
|
+
self.prefix = '/api/v1/'
|
33
|
+
|
34
|
+
field_accessor :id, :email
|
35
|
+
end
|
36
|
+
|
37
|
+
u = User.create(email: 'test@example.com')
|
38
|
+
u.email = 'another@example.com'
|
39
|
+
u.save!
|
40
|
+
u.reload
|
41
|
+
|
42
|
+
u = User.find(2)
|
43
|
+
u.destroy
|
44
|
+
|
45
|
+
User.all.each do |user|
|
46
|
+
...
|
47
|
+
end
|
48
|
+
```
|
29
49
|
|
30
50
|
## Contributing
|
31
51
|
|
32
|
-
|
52
|
+
1. Fork it ( https://github.com/desofto/grape-client/fork )
|
53
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
54
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
55
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
56
|
+
5. Create a new Pull Request
|
data/grape-client.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ['dmitry@desofto.com']
|
11
11
|
|
12
12
|
spec.summary = 'Simple access from your client to Grape APIs.'
|
13
|
-
spec.description = 'Simple access from your client to Grape APIs.'
|
13
|
+
spec.description = 'Simple access from your client to Grape APIs. Automatically supports: kaminari pagination, network access, nested objects.'
|
14
14
|
spec.homepage = 'https://github.com/desofto/grape-client'
|
15
15
|
spec.license = 'MIT'
|
16
16
|
|
data/lib/grape_client/base.rb
CHANGED
@@ -16,6 +16,25 @@ module GrapeClient
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
+
def belongs_to(property, options = {})
|
20
|
+
field_accessor "#{property}_id"
|
21
|
+
|
22
|
+
define_method(property) do
|
23
|
+
@attributes[property] ||= retrive_object(options[:class_name] || property,
|
24
|
+
self["#{property}_id"])
|
25
|
+
end
|
26
|
+
|
27
|
+
define_method("#{property}=") do |object|
|
28
|
+
self["#{property}_id"] = object.id
|
29
|
+
@attributes[property] = object
|
30
|
+
end
|
31
|
+
|
32
|
+
define_method("#{property}_id=") do |id|
|
33
|
+
@attributes[property] = nil
|
34
|
+
self["#{property}_id"] = id
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
19
38
|
def connection
|
20
39
|
@connection ||= Connection.new(user, password)
|
21
40
|
end
|
@@ -39,14 +58,14 @@ module GrapeClient
|
|
39
58
|
end
|
40
59
|
|
41
60
|
def initialize(attrs = {})
|
42
|
-
@attributes = {}
|
43
61
|
self.attributes = attrs
|
44
62
|
end
|
45
63
|
|
46
64
|
def attributes=(attrs)
|
65
|
+
@attributes = {}
|
47
66
|
attributes = self.class.attributes
|
48
67
|
attrs.each do |name, value|
|
49
|
-
next unless attributes.include? name.to_sym
|
68
|
+
next unless attributes.include?(name.to_sym) || methods.include?(name.to_sym)
|
50
69
|
send("#{name}=", value)
|
51
70
|
end
|
52
71
|
end
|
@@ -75,7 +94,15 @@ module GrapeClient
|
|
75
94
|
|
76
95
|
def to_post
|
77
96
|
entity_name = self.class.entity_name
|
78
|
-
|
97
|
+
list = self.class.attributes
|
98
|
+
filtered_attributes = attributes.select { |key, _value| list.include? key }
|
99
|
+
filtered_attributes.transform_keys { |key| "#{entity_name}[#{key}]" }
|
100
|
+
end
|
101
|
+
|
102
|
+
private
|
103
|
+
|
104
|
+
def retrive_object(name, id)
|
105
|
+
name.to_s.camelcase.constantize.find(id) if id.present?
|
79
106
|
end
|
80
107
|
end
|
81
108
|
end
|
@@ -13,39 +13,41 @@ module GrapeClient
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def each(&_block)
|
16
|
-
|
17
|
-
|
18
|
-
@elements.each do |attrs|
|
16
|
+
per_page do |elements|
|
17
|
+
elements.each do |attrs|
|
19
18
|
yield @clazz.new(attrs)
|
20
19
|
end
|
21
|
-
break unless load_next_page
|
22
20
|
end
|
23
21
|
end
|
24
22
|
|
25
23
|
def map(&_block)
|
26
|
-
prepare_for_iteration
|
27
24
|
result = []
|
28
|
-
|
29
|
-
result +=
|
25
|
+
per_page do |elements|
|
26
|
+
result += elements.map do |attrs|
|
30
27
|
yield @clazz.new(attrs)
|
31
28
|
end
|
32
|
-
break unless load_next_page
|
33
29
|
end
|
34
30
|
result
|
35
31
|
end
|
36
32
|
|
37
33
|
def collect
|
38
|
-
prepare_for_iteration
|
39
34
|
result = []
|
40
|
-
|
41
|
-
result +=
|
42
|
-
break unless load_next_page
|
35
|
+
per_page do |elements|
|
36
|
+
result += elements
|
43
37
|
end
|
44
38
|
result
|
45
39
|
end
|
46
40
|
|
47
41
|
private
|
48
42
|
|
43
|
+
def per_page
|
44
|
+
prepare_for_iteration
|
45
|
+
loop do
|
46
|
+
yield @elements
|
47
|
+
break unless load_next_page
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
49
51
|
def update(elements, headers = nil)
|
50
52
|
@elements = elements
|
51
53
|
@count = headers['total'].first.try(:to_i) if headers
|
data/lib/grape_client/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dmitry Silchenko
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,7 +66,8 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '4.2'
|
69
|
-
description: Simple access from your client to Grape APIs.
|
69
|
+
description: 'Simple access from your client to Grape APIs. Automatically supports:
|
70
|
+
kaminari pagination, network access, nested objects.'
|
70
71
|
email:
|
71
72
|
- dmitry@desofto.com
|
72
73
|
executables: []
|