spooky 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fb5305d1096b3ab7ae0add591dad425ae012cb2105b3a7a0d64691352c28d7c5
4
- data.tar.gz: 576d6d09c71cdc9cd6aba580d3386b80e750f314a9a2509cd49e60401dea76f3
3
+ metadata.gz: a7b33039d9c8c5fc23d9cd5bd0d9f4b378b587695ebe3af8f0f5e23308f71bc7
4
+ data.tar.gz: ec4b539ae09724a1a95b7f42a8e2662bc6246172173827c27bfb8c75805f762c
5
5
  SHA512:
6
- metadata.gz: a8eaf6a4cabc7d44952a84eefbb7d821451d8ac9863c52240de3c10f2da1424ae3065001855b3a4df37ac110144cc71a5f468b46d1d600465c3560f8cbb89ee9
7
- data.tar.gz: 32621bc47059575a6f909b2b3ac6e2be2332aa7218a234243db9a615d08e0d5ea33038458234f47816a0a71bf2fe016e65d30f312138582afab157d3bcfd8603
6
+ metadata.gz: '08bb92cc697e8ad5d80851658d595df20fb3844ca62eba13fcb3f99f8237054166fd114954f6d2b2c47fababd5922c6d24a6bed001f68da578a895590b74d7ac'
7
+ data.tar.gz: 5b2161ec5915e1c9f71796c1b362f290bf9742813c7a444dfb1e90eb73339315d5d01dc655f5a9454a638b9f136abbae8e812a727ecc6897b0a731dbdeadf846
data/README.md CHANGED
@@ -47,33 +47,66 @@ client = Spooky::Client.new
47
47
  ```
48
48
  ### Fetching Posts
49
49
 
50
- Spooky operates primarily on posts.
50
+ Spooky operates primarily on posts. Methods that return a collection also return a pagination object, if there is one.
51
51
 
52
52
  #### Get all posts
53
- `posts = client.posts`
53
+ ```ruby
54
+ posts, pagination = client.posts
55
+ ```
54
56
 
55
- Returns an array of `Spooky::Post`s.
57
+ Returns an array of `Spooky::Post`s and a pagination meta hash.
56
58
 
57
59
  #### Get all posts and include tags and authors
58
- `posts = client.posts(tags: true, authors: true)`
60
+ ```ruby
61
+ posts, pagination = client.posts(tags: true, authors: true)
62
+ ```
59
63
 
60
- Returns an array of `Spooky::Post`s with `Spooky::Tag`s and `Spooky::Author`s where appropriate.
64
+ Returns an array of `Spooky::Post`s with `Spooky::Tag`s and `Spooky::Author`s where appropriate and a pagination meta hash.
61
65
 
62
66
  #### Get a post by ID or slug
63
67
 
64
- `post = client.post_by(id: 'abc123')`
68
+ ```ruby
69
+ post = client.post_by(id: 'abc123')
70
+ ```
65
71
 
66
- `post = client.post_by(slug: 'this-is-a-slug')`
72
+ ```ruby
73
+ post = client.post_by(slug: 'this-is-a-slug')
74
+ ```
67
75
 
68
76
  Both return a `Spooky::Post`.
69
77
 
70
78
  #### Get posts with a filter applied
71
79
 
72
- Filtering accepts simple hashes as conditions or NQL strings for more complex filters.
80
+ Filtering accepts simple hashes as conditions or [NQL strings for more complex filters](https://ghost.org/docs/api/v3/content/#syntax-reference).
73
81
 
74
- `featured_posts = client.posts(filter: { featured: true })`
82
+ ```ruby
83
+ featured_posts, pagination = client.posts(filter: { featured: true })
84
+ ```
75
85
 
76
- `welcome_posts = client.posts(filter: "title:Welcome")`
86
+ ```ruby
87
+ welcome_posts, pagination = client.posts(filter: "title:Welcome")
88
+ ```
89
+
90
+ You can exclude a post using NQL operators, note the `-`:
91
+
92
+ ```ruby
93
+ posts_without_welcome, pagination = client.posts(tags: true, filter: "title:-Welcome")
94
+ ```
95
+
96
+ ### Pagination
97
+
98
+ If applicable, Spooky will return the pagination meta hash directly from Ghost. It looks like this:
99
+
100
+ ```ruby
101
+ {
102
+ "page" => 1,
103
+ "limit" => 3,
104
+ "pages" => 3,
105
+ "total" => 7,
106
+ "next" => 2,
107
+ "prev" => nil
108
+ }
109
+ ```
77
110
 
78
111
  ## Development
79
112
 
@@ -21,20 +21,23 @@ module Spooky
21
21
  @error = response["errors"]
22
22
  false
23
23
  else
24
- response[resource.split("/").first]
24
+ collection = response[resource.split("/").first]
25
+ pagination = response.dig("meta", "pagination")
26
+
27
+ [collection, pagination]
25
28
  end
26
29
  end
27
30
 
28
31
  def fetch(resource, options = {})
29
32
  resource_name = resource.split("/").first
30
- response = fetch_json(resource, options)
33
+ response, pagination = fetch_json(resource, options)
31
34
 
32
35
  resource_class = "Spooky::#{resource_name.singularize.classify}".constantize
33
36
 
34
- response.present? && response.map { |attrs| resource_class.send(:new, attrs) }
37
+ response.present? && [response.map { |attrs| resource_class.send(:new, attrs) }, pagination]
35
38
  end
36
39
 
37
- def posts(tags: false, authors: false, filter: false)
40
+ def posts(tags: false, authors: false, filter: false, page: false, limit: false)
38
41
  inc = []
39
42
  inc << "tags" if tags
40
43
  inc << "authors" if authors
@@ -42,13 +45,8 @@ module Spooky
42
45
  options = {}
43
46
  options[:include] = inc if inc.present?
44
47
 
45
- if filter.present?
46
- if filter.is_a?(Hash)
47
- options[:filter] = filter.map { |k, v| "#{k}:#{v}" }.join
48
- else
49
- options[:filter] = filter
50
- end
51
- end
48
+ options = apply_filter(options, filter)
49
+ options = apply_pagination(options, { page: page, limit: limit })
52
50
 
53
51
  fetch("posts", options)
54
52
  end
@@ -62,14 +60,35 @@ module Spooky
62
60
  options[:include] = inc if inc.present?
63
61
 
64
62
  if id.present?
65
- response = fetch("posts/#{id}", options)
63
+ response, _ = fetch("posts/#{id}", options)
66
64
  response.present? && response.first
67
65
  elsif slug.present?
68
- response = fetch("posts/slug/#{slug}", options)
66
+ response, _ = fetch("posts/slug/#{slug}", options)
69
67
  response.present? && response.first
70
68
  else
71
69
  false
72
70
  end
73
71
  end
72
+
73
+ private
74
+
75
+ def apply_filter(options, filter)
76
+ if filter.present?
77
+ if filter.is_a?(Hash)
78
+ options[:filter] = filter.map { |k, v| "#{k}:#{v}" }.join("+")
79
+ else
80
+ options[:filter] = filter
81
+ end
82
+ end
83
+
84
+ options
85
+ end
86
+
87
+ def apply_pagination(options, pagination)
88
+ options[:page] = pagination[:page] if pagination[:page]
89
+ options[:limit] = pagination[:limit] if pagination[:limit]
90
+
91
+ options
92
+ end
74
93
  end
75
94
  end
@@ -1,3 +1,3 @@
1
1
  module Spooky
2
- VERSION = "1.0.0".freeze
2
+ VERSION = "1.1.0".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spooky
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Georges Gabereau