Class: Spooky::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/spooky/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_keyObject

Returns the value of attribute api_key.



7
8
9
# File 'lib/spooky/client.rb', line 7

def api_key
  @api_key
end

#api_urlObject

Returns the value of attribute api_url.



7
8
9
# File 'lib/spooky/client.rb', line 7

def api_url
  @api_url
end

#endpointObject

Returns the value of attribute endpoint.



7
8
9
# File 'lib/spooky/client.rb', line 7

def endpoint
  @endpoint
end

#errorObject

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, options = {})
  resource_name = resource.split("/").first
  response, pagination = fetch_json(resource, options)

  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, options = {})
  options.merge!(key: @api_key)
  url = "#{@endpoint}/#{resource}/"
  response = HTTP.get(url, params: options).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 tags
  inc << "authors" if authors

  options = {}
  options[:include] = inc if inc.present?

  if id.present?
    response, _ = fetch("posts/#{id}", options)
    response.present? && response.first
  elsif slug.present?
    response, _ = fetch("posts/slug/#{slug}", options)
    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 tags
  inc << "authors" if authors

  options = {}
  options[:include] = inc if inc.present?

  options = apply_filter(options, filter)
  options = apply_pagination(options, { page: page, limit: limit })

  fetch("posts", options)
end