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



28
29
30
31
32
33
34
35
# File 'lib/spooky/client.rb', line 28

def fetch(resource, options = {})
  resource_name = resource.split("/").first
  response = fetch_json(resource, options)

  resource_class = "Spooky::#{resource_name.singularize.classify}".constantize

  response.present? && response.map { |attrs| resource_class.send(:new, attrs) }
end

#fetch_json(resource, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# 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
    response[resource.split("/").first]
  end
end

#post_by(id: nil, slug: nil, tags: false, authors: false) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/spooky/client.rb', line 56

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) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/spooky/client.rb', line 37

def posts(tags: false, authors: false, filter: false)
  inc = []
  inc << "tags" if tags
  inc << "authors" if authors

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

  if filter.present?
    if filter.is_a?(Hash)
      options[:filter] = filter.map { |k, v| "#{k}:#{v}" }.join
    else
      options[:filter] = filter
    end
  end

  fetch("posts", options)
end