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) ⇒ 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
28 29 30 31 32 33 34 35 |
# File 'lib/spooky/client.rb', line 28 def fetch(resource, = {}) resource_name = resource.split("/").first response = fetch_json(resource, ) 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, = {}) .merge!(key: @api_key) url = "#{@endpoint}/#{resource}/" response = HTTP.get(url, params: ).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 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) ⇒ 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 inc << "authors" if = {} [:include] = inc if inc.present? if filter.present? if filter.is_a?(Hash) [:filter] = filter.map { |k, v| "#{k}:#{v}" }.join else [:filter] = filter end end fetch("posts", ) end |