Class: Quintype::API::Client

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

Class Method Summary collapse

Instance Method Summary collapse

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

.instanceObject



22
23
24
# File 'lib/quintype/api/client.rb', line 22

def instance
  @client
end

Instance Method Details

#get_author(author_id) ⇒ Object

Raises:



63
64
65
66
67
# File 'lib/quintype/api/client.rb', line 63

def get_author(author_id)
  response = get("/api/v1/authors/#{author_id}")
  raise ClientException.new("Could not get authors", response) unless response.status == 200
  response.body["author"]
end

#get_configObject

Raises:



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

Raises:



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

Raises:



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

Raises:



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

Raises:



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

Raises:



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