Class: Quintype::API::Client
- Inherits:
-
Object
- Object
- Quintype::API::Client
- Defined in:
- lib/quintype/api/client.rb
Class Method Summary collapse
Instance Method Summary collapse
- #get_author(author_id) ⇒ Object
- #get_config ⇒ Object
- #get_search(params) ⇒ Object
- #get_stories(params) ⇒ Object
- #get_story_by_slug(slug) ⇒ Object
- #get_tag_by_name(name) ⇒ Object
-
#initialize(conn) ⇒ Client
constructor
A new instance of Client.
- #post_bulk(requests) ⇒ Object
Constructor Details
#initialize(conn) ⇒ Client
Returns a new instance of Client.
27 28 29 |
# File 'lib/quintype/api/client.rb', line 27 def initialize(conn) @conn = conn end |
Class Method Details
.establish_connection(host, adapter = nil) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/quintype/api/client.rb', line 13 def establish_connection(host, adapter = nil) connection = Faraday.new(host) do |conn| conn.request :json conn.response :json, :content_type => /\bjson$/ conn.adapter(adapter || Faraday.default_adapter) end @client = new(connection) end |
.instance ⇒ Object
22 23 24 |
# File 'lib/quintype/api/client.rb', line 22 def instance @client end |
Instance Method Details
#get_author(author_id) ⇒ Object
63 64 65 66 67 |
# File 'lib/quintype/api/client.rb', line 63 def () response = get("/api/v1/authors/#{}") raise ClientException.new("Could not get authors", response) unless response.status == 200 response.body["author"] end |
#get_config ⇒ Object
31 32 33 34 35 |
# File 'lib/quintype/api/client.rb', line 31 def get_config response = get("/api/v1/config") raise ClientException.new("Could not get config", response) unless response.status == 200 response.body end |
#get_search(params) ⇒ Object
57 58 59 60 61 |
# File 'lib/quintype/api/client.rb', line 57 def get_search(params) response = get("/api/v1/search", params) raise ClientException.new("Could not search stories", response) unless response.status == 200 response.body["results"] end |
#get_stories(params) ⇒ Object
51 52 53 54 55 |
# File 'lib/quintype/api/client.rb', line 51 def get_stories(params) response = get("/api/v1/stories", params) raise ClientException.new("Could not get stories", response) unless response.status == 200 response.body["stories"] end |
#get_story_by_slug(slug) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/quintype/api/client.rb', line 37 def get_story_by_slug(slug) response = get("/api/v1/stories-by-slug", slug: slug) return nil if response.status == 404 raise ClientException.new("Could not fetch story", response) unless response.status == 200 response.body["story"] end |
#get_tag_by_name(name) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/quintype/api/client.rb', line 44 def get_tag_by_name(name) response = get("/api/tag/#{name}") return nil if response.status == 404 raise ClientException.new("Could not fetch tag", response) unless response.status == 200 response.body["tag"] end |
#post_bulk(requests) ⇒ Object
69 70 71 72 73 |
# File 'lib/quintype/api/client.rb', line 69 def post_bulk(requests) response = post("/api/v1/bulk", requests: requests) raise ClientException.new("Could not bulk fetch", response) unless response.status == 200 response.body["results"] end |