Class: Spooky::Client
- Inherits:
-
Object
- Object
- Spooky::Client
- Defined in:
- lib/spooky/client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#api_url ⇒ Object
Returns the value of attribute api_url.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#error ⇒ Object
Returns the value of attribute error.
Instance Method Summary collapse
- #fetch(resource, options = {}) ⇒ Object
- #fetch_json(resource, options = {}) ⇒ Object
-
#initialize(attrs = {}) ⇒ Client
constructor
A new instance of Client.
- #post_by(id: nil, slug: nil, tags: false, authors: false) ⇒ Object
- #posts(tags: false, authors: false, filter: false, page: false, limit: false) ⇒ Object
Constructor Details
#initialize(attrs = {}) ⇒ Client
Returns a new instance of Client.
9 10 11 12 13 |
# File 'lib/spooky/client.rb', line 9 def initialize(attrs = {}) @api_url = ENV["GHOST_API_URL"] || attrs[:api_url] @api_key = ENV["GHOST_CONTENT_API_KEY"] || attrs[:api_key] @endpoint = "#{@api_url}/ghost/api/v3/content" end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
7 8 9 |
# File 'lib/spooky/client.rb', line 7 def api_key @api_key end |
#api_url ⇒ Object
Returns the value of attribute api_url.
7 8 9 |
# File 'lib/spooky/client.rb', line 7 def api_url @api_url end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
7 8 9 |
# File 'lib/spooky/client.rb', line 7 def endpoint @endpoint end |
#error ⇒ Object
Returns the value of attribute error.
7 8 9 |
# File 'lib/spooky/client.rb', line 7 def error @error end |
Instance Method Details
#fetch(resource, options = {}) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/spooky/client.rb', line 31 def fetch(resource, = {}) resource_name = resource.split("/").first response, pagination = fetch_json(resource, ) resource_class = "Spooky::#{resource_name.singularize.classify}".constantize response.present? && [response.map { |attrs| resource_class.send(:new, attrs) }, pagination] end |
#fetch_json(resource, options = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/spooky/client.rb', line 15 def fetch_json(resource, = {}) .merge!(key: @api_key) url = "#{@endpoint}/#{resource}/" response = HTTP.get(url, params: ).parse if response["errors"].present? @error = response["errors"] false else collection = response[resource.split("/").first] pagination = response.dig("meta", "pagination") [collection, pagination] end end |
#post_by(id: nil, slug: nil, tags: false, authors: false) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/spooky/client.rb', line 54 def post_by(id: nil, slug: nil, tags: false, authors: false) inc = [] inc << "tags" if inc << "authors" if = {} [:include] = inc if inc.present? if id.present? response, _ = fetch("posts/#{id}", ) response.present? && response.first elsif slug.present? response, _ = fetch("posts/slug/#{slug}", ) response.present? && response.first else false end end |
#posts(tags: false, authors: false, filter: false, page: false, limit: false) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/spooky/client.rb', line 40 def posts(tags: false, authors: false, filter: false, page: false, limit: false) inc = [] inc << "tags" if inc << "authors" if = {} [:include] = inc if inc.present? = apply_filter(, filter) = apply_pagination(, { page: page, limit: limit }) fetch("posts", ) end |