fmrest 0.3.3 → 0.4.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/CHANGELOG.md +5 -0
- data/README.md +62 -15
- data/lib/fmrest/spyke/model/orm.rb +11 -3
- data/lib/fmrest/spyke/relation.rb +90 -13
- data/lib/fmrest/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7eb7073bb86836108750364857226e2ce6eaac9c88348c74b0c82ecb446f5d4e
|
4
|
+
data.tar.gz: 7e5d6bee538d00fce9feea4f0666014853fe4feba67ea11ff1841809a7d6c577
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2e59d89d5829d94566be859060c10e88c875b3298bdd15dbc61b289df64b69534f3d294528b6b3a697b1583cf149472030fcc370eee97ddd0af0d03804520bec
|
7
|
+
data.tar.gz: 057a8bca299892de5d4415b2c67bec71a15778c8e80567726e44c9413bd7b75cf7d422bf86626b24a0021b2c5ca9eb7e43cf4eb486217e176133c19d3a509554
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -447,22 +447,27 @@ passing arbitrary parameters to the REST backend. fmrest-ruby however is well
|
|
447
447
|
aware of its backend API, so it extends Spkye models with a bunch of useful
|
448
448
|
querying methods.
|
449
449
|
|
450
|
-
|
451
|
-
class Honeybee < Spyke::Base
|
452
|
-
include FmRest::Spyke
|
450
|
+
#### .limit
|
453
451
|
|
454
|
-
|
452
|
+
`.limit` sets the limit for get and find request:
|
455
453
|
|
456
|
-
|
457
|
-
|
454
|
+
```ruby
|
455
|
+
Honeybee.limit(10)
|
458
456
|
```
|
459
457
|
|
460
|
-
|
458
|
+
NOTE: You can also set a default limit value for a model class, see
|
459
|
+
[Other notes on querying](#other-notes-on-querying).
|
461
460
|
|
462
|
-
|
461
|
+
You can also use `.limit` to set limits on portals:
|
463
462
|
|
464
463
|
```ruby
|
465
|
-
Honeybee.limit(
|
464
|
+
Honeybee.limit(hives: 3, flowers: 2)
|
465
|
+
```
|
466
|
+
|
467
|
+
To remove the limit on a portal set it to `nil`:
|
468
|
+
|
469
|
+
```ruby
|
470
|
+
Honeybee.limit(flowers: nil)
|
466
471
|
```
|
467
472
|
|
468
473
|
#### .offset
|
@@ -473,6 +478,18 @@ Honeybee.limit(10)
|
|
473
478
|
Honeybee.offset(10)
|
474
479
|
```
|
475
480
|
|
481
|
+
You can also use `.offset` to set offsets on portals:
|
482
|
+
|
483
|
+
```ruby
|
484
|
+
Honeybee.offset(hives: 3, flowers: 2)
|
485
|
+
```
|
486
|
+
|
487
|
+
To remove the offset on a portal set it to `nil`:
|
488
|
+
|
489
|
+
```ruby
|
490
|
+
Honeybee.offset(flowers: nil)
|
491
|
+
```
|
492
|
+
|
476
493
|
#### .sort
|
477
494
|
|
478
495
|
`.sort` (or `.order`) sets sorting options for get and find requests:
|
@@ -490,16 +507,44 @@ Honeybee.sort(:name, :age!)
|
|
490
507
|
Honeybee.sort(:name, :age__desc)
|
491
508
|
```
|
492
509
|
|
510
|
+
NOTE: You can also set default sort values for a model class, see
|
511
|
+
[Other notes on querying](#other-notes-on-querying).
|
512
|
+
|
493
513
|
#### .portal
|
494
514
|
|
495
|
-
`.portal` (
|
496
|
-
(this recognizes portals defined with
|
515
|
+
`.portal` (aliased as `.includes` and `.portals`) sets which portals to fetch
|
516
|
+
(if any) for get and find requests (this recognizes portals defined with
|
517
|
+
`has_portal`):
|
497
518
|
|
498
519
|
```ruby
|
499
|
-
Honeybee.portal(:hives)
|
520
|
+
Honeybee.portal(:hives) # include just the :hives portal
|
500
521
|
Honeybee.includes(:hives) # alias method
|
522
|
+
Honeybee.portals(:hives, :flowers) # alias for pluralization fundamentalists
|
523
|
+
```
|
524
|
+
|
525
|
+
Chaining calls to `.portal` will add portals to the existing included list:
|
526
|
+
|
527
|
+
```ruby
|
528
|
+
Honeybee.portal(:flowers).portal(:hives) # include both portals
|
529
|
+
```
|
530
|
+
|
531
|
+
If you want to disable portals for the scope call `.portal(false)`:
|
532
|
+
|
533
|
+
```ruby
|
534
|
+
Honeybee.portal(false) # disable portals for this scope
|
535
|
+
```
|
536
|
+
|
537
|
+
If you want to include all portals call `.portal(true)`:
|
538
|
+
|
539
|
+
```ruby
|
540
|
+
Honeybee.portal(true) # include all portals
|
501
541
|
```
|
502
542
|
|
543
|
+
For convenience you can also use `.with_all_portals` and `.without_portals`,
|
544
|
+
which behave just as calling `.portal(true)` and `portal(false)` respectively.
|
545
|
+
|
546
|
+
NOTE: By default all portals are included.
|
547
|
+
|
503
548
|
#### .query
|
504
549
|
|
505
550
|
`.query` sets query conditions for a find request (and supports attributes as
|
@@ -553,8 +598,10 @@ Honeybee.limit(10).offset(20).sort(:name, :age!).portal(:hives).query(name: "Hut
|
|
553
598
|
You can also set default values for limit and sort on the class:
|
554
599
|
|
555
600
|
```ruby
|
556
|
-
Honeybee
|
557
|
-
|
601
|
+
class Honeybee < FmRest::Spyke::Base
|
602
|
+
self.default_limit = 1000
|
603
|
+
self.default_sort = [:name, :age!]
|
604
|
+
end
|
558
605
|
```
|
559
606
|
|
560
607
|
Calling any `Enumerable` method on the resulting scope object will trigger a
|
@@ -672,7 +719,7 @@ end
|
|
672
719
|
- [ ] Better/simpler-to-use core Ruby API
|
673
720
|
- [ ] Better API documentation and README
|
674
721
|
- [ ] Oauth support
|
675
|
-
- [
|
722
|
+
- [x] Support for portal limit and offset
|
676
723
|
- [x] More options for token storage
|
677
724
|
- [x] Support for container fields
|
678
725
|
- [x] Optional logging
|
@@ -17,7 +17,9 @@ module FmRest
|
|
17
17
|
|
18
18
|
class_methods do
|
19
19
|
# Methods delegated to FmRest::Spyke::Relation
|
20
|
-
delegate :limit, :offset, :sort, :
|
20
|
+
delegate :limit, :offset, :sort, :order, :query, :omit, :portal,
|
21
|
+
:portals, :includes, :with_all_portals, :without_portals,
|
22
|
+
to: :all
|
21
23
|
|
22
24
|
def all
|
23
25
|
# Use FmRest's Relation insdead of Spyke's vanilla one
|
@@ -66,9 +68,15 @@ module FmRest
|
|
66
68
|
prefixed ? scope.sort_params.to_json : scope.sort_params
|
67
69
|
end
|
68
70
|
|
69
|
-
|
71
|
+
unless scope.included_portals.nil?
|
70
72
|
where_options["portal"] =
|
71
|
-
prefixed ? scope.
|
73
|
+
prefixed ? scope.included_portals.to_json : scope.included_portals
|
74
|
+
end
|
75
|
+
|
76
|
+
if scope.portal_params.present?
|
77
|
+
scope.portal_params.each do |portal_param, value|
|
78
|
+
where_options["#{prefix}#{portal_param}"] = value
|
79
|
+
end
|
72
80
|
end
|
73
81
|
|
74
82
|
scope.where(where_options)
|
@@ -12,7 +12,7 @@ module FmRest
|
|
12
12
|
|
13
13
|
|
14
14
|
attr_accessor :limit_value, :offset_value, :sort_params, :query_params,
|
15
|
-
:portal_params
|
15
|
+
:included_portals, :portal_params
|
16
16
|
|
17
17
|
def initialize(*_args)
|
18
18
|
super
|
@@ -24,20 +24,43 @@ module FmRest
|
|
24
24
|
end
|
25
25
|
|
26
26
|
@query_params = []
|
27
|
-
|
27
|
+
|
28
|
+
@included_portals = nil
|
29
|
+
@portal_params = {}
|
28
30
|
end
|
29
31
|
|
30
|
-
# @param
|
31
|
-
#
|
32
|
-
|
33
|
-
|
32
|
+
# @param value_or_hash [Integer, Hash] the limit value for this layout,
|
33
|
+
# or a hash with limits for the layout's portals
|
34
|
+
# @example
|
35
|
+
# Person.limit(10) # Set layout limit
|
36
|
+
# Person.limit(children: 10) # Set portal limit
|
37
|
+
# @return [FmRest::Spyke::Relation] a new relation with the limits
|
38
|
+
# applied
|
39
|
+
def limit(value_or_hash)
|
40
|
+
with_clone do |r|
|
41
|
+
if value_or_hash.respond_to?(:each)
|
42
|
+
r.set_portal_params(value_or_hash, :limit)
|
43
|
+
else
|
44
|
+
r.limit_value = value_or_hash
|
45
|
+
end
|
46
|
+
end
|
34
47
|
end
|
35
48
|
|
36
|
-
# @param
|
37
|
-
#
|
49
|
+
# @param value_or_hash [Integer, Hash] the offset value for this layout,
|
50
|
+
# or a hash with offsets for the layout's portals
|
51
|
+
# @example
|
52
|
+
# Person.offset(10) # Set layout offset
|
53
|
+
# Person.offset(children: 10) # Set portal offset
|
54
|
+
# @return [FmRest::Spyke::Relation] a new relation with the offsets
|
38
55
|
# applied
|
39
|
-
def offset(
|
40
|
-
with_clone
|
56
|
+
def offset(value_or_hash)
|
57
|
+
with_clone do |r|
|
58
|
+
if value_or_hash.respond_to?(:each)
|
59
|
+
r.set_portal_params(value_or_hash, :offset)
|
60
|
+
else
|
61
|
+
r.offset_value = value_or_hash
|
62
|
+
end
|
63
|
+
end
|
41
64
|
end
|
42
65
|
|
43
66
|
# Allows sort params given in either hash format (using FM Data API's
|
@@ -47,7 +70,7 @@ module FmRest
|
|
47
70
|
#
|
48
71
|
# @param args [Array<Symbol, Hash>] the names of attributes to sort by with
|
49
72
|
# optional `!` or `__desc` suffix, or a hash of options as expected by
|
50
|
-
#
|
73
|
+
# the FM Data API
|
51
74
|
# @example
|
52
75
|
# Person.sort(:first_name, :age!)
|
53
76
|
# Person.sort(:first_name, :age__desc)
|
@@ -62,13 +85,45 @@ module FmRest
|
|
62
85
|
end
|
63
86
|
alias order sort
|
64
87
|
|
88
|
+
# Sets the portals to include with each record in the response.
|
89
|
+
#
|
90
|
+
# @param args [Array<Symbol, String>, true, false] the names of portals to
|
91
|
+
# include, or `false` to request no portals
|
92
|
+
# @example
|
93
|
+
# Person.portal(:relatives, :pets)
|
94
|
+
# Person.portal(false) # Disables portals
|
95
|
+
# Person.portal(true) # Enables portals (includes all)
|
96
|
+
# @return [FmRest::Spyke::Relation] a new relation with the portal
|
97
|
+
# options applied
|
65
98
|
def portal(*args)
|
99
|
+
raise ArgumentError, "Call `portal' with at least one argument" if args.empty?
|
100
|
+
|
66
101
|
with_clone do |r|
|
67
|
-
|
68
|
-
|
102
|
+
if args.length == 1 && args.first.eql?(true) || args.first.eql?(false)
|
103
|
+
r.included_portals = args.first ? nil : []
|
104
|
+
else
|
105
|
+
r.included_portals ||= []
|
106
|
+
r.included_portals += args.flatten.map { |p| normalize_portal_param(p) }
|
107
|
+
r.included_portals.uniq!
|
108
|
+
end
|
69
109
|
end
|
70
110
|
end
|
71
111
|
alias includes portal
|
112
|
+
alias portals portal
|
113
|
+
|
114
|
+
# Same as calling `portal(true)`
|
115
|
+
#
|
116
|
+
# @return (see #portal)
|
117
|
+
def with_all_portals
|
118
|
+
portal(true)
|
119
|
+
end
|
120
|
+
|
121
|
+
# Same as calling `portal(false)`
|
122
|
+
#
|
123
|
+
# @return (see #portal)
|
124
|
+
def without_portals
|
125
|
+
portal(false)
|
126
|
+
end
|
72
127
|
|
73
128
|
def query(*params)
|
74
129
|
with_clone do |r|
|
@@ -95,6 +150,28 @@ module FmRest
|
|
95
150
|
fallback_or_reraise(error, default: nil)
|
96
151
|
end
|
97
152
|
|
153
|
+
protected
|
154
|
+
|
155
|
+
def set_portal_params(params_hash, param)
|
156
|
+
# Copy portal_params so we're not modifying the same hash as the parent
|
157
|
+
# scope
|
158
|
+
self.portal_params = portal_params.dup
|
159
|
+
|
160
|
+
params_hash.each do |portal_name, value|
|
161
|
+
# TODO: Use a hash like { portal_name: { param: value } } instead so
|
162
|
+
# we can intelligently avoid including portal params for excluded
|
163
|
+
# portals
|
164
|
+
key = "#{param}.#{normalize_portal_param(portal_name)}"
|
165
|
+
|
166
|
+
# Delete key if value is falsy
|
167
|
+
if !value && portal_params.has_key?(key)
|
168
|
+
portal_params.delete(key)
|
169
|
+
else
|
170
|
+
self.portal_params[key] = value
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
98
175
|
private
|
99
176
|
|
100
177
|
def normalize_sort_param(param)
|
data/lib/fmrest/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fmrest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pedro Carbajal
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-10-
|
11
|
+
date: 2019-10-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|