Class: BlogApi::Client

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

Defined Under Namespace

Classes: RequestError

Constant Summary collapse

DEFAULT_URL =
'https://k-blog0130.herokuapp.com/api/v2/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url = DEFAULT_URL) ⇒ Client

Returns a new instance of Client.



10
11
12
# File 'lib/blog_api/client.rb', line 10

def initialize(base_url = DEFAULT_URL)
  @base_url = base_url
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



8
9
10
# File 'lib/blog_api/client.rb', line 8

def base_url
  @base_url
end

Instance Method Details

#categoriesObject



23
24
25
# File 'lib/blog_api/client.rb', line 23

def categories
  get('categories').parsed_body
end


35
36
37
# File 'lib/blog_api/client.rb', line 35

def featured_posts
  get("featured_posts").parsed_body
end

#get(path, options: { format: :plain }) ⇒ Object

Raises:



14
15
16
17
18
19
20
21
# File 'lib/blog_api/client.rb', line 14

def get(path, options: { format: :plain })
  url = "#{base_url}/#{path}"
  result = HTTParty.get(url, options)

  raise RequestError unless result.response.code == '200'

  BlogApi::Response.new(result)
end

#post(post_id) ⇒ Object



31
32
33
# File 'lib/blog_api/client.rb', line 31

def post(post_id)
  get("posts/#{post_id}").parsed_body
end

#postsObject



27
28
29
# File 'lib/blog_api/client.rb', line 27

def posts
  get('posts').parsed_body
end

#tagsObject



39
40
41
# File 'lib/blog_api/client.rb', line 39

def tags
  get("tags").parsed_body
end