Class: WCC::Blogs::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/wcc/blogs/client/response.rb,
lib/wcc/blogs/client.rb

Defined Under Namespace

Classes: ApiError, NotFoundError, Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/wcc/blogs/client.rb', line 11

def initialize(**options)
  if options[:publishing_target]
    raise ArgumentError, 'publishing_target has been renamed to default_property'
  end

  @default_property = options[:default_property]
  base_url = URI.parse(options[:base_url] || 'https://di0v2frwtdqnv.cloudfront.net')
  @base_path = base_url.path == '' ? '/api/v1' : base_url.path
  @base_url = base_url.to_s
  @connection = options[:connection] || default_connection
  @query_defaults = options[:query_defaults] || {}
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



9
10
11
# File 'lib/wcc/blogs/client.rb', line 9

def base_path
  @base_path
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



9
10
11
# File 'lib/wcc/blogs/client.rb', line 9

def base_url
  @base_url
end

#default_propertyObject (readonly)

Returns the value of attribute default_property.



9
10
11
# File 'lib/wcc/blogs/client.rb', line 9

def default_property
  @default_property
end

Instance Method Details

#blog_list(property: nil, year: nil) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
# File 'lib/wcc/blogs/client.rb', line 48

def blog_list(property: nil, year: nil)
  property ||= default_property
  raise ArgumentError, 'No Property given' unless property

  path = base_path + '/property/' + property + '/blog'
  path += '/' + year if year
  get(path).assert_ok!
end

#blog_show(slug, digest: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/wcc/blogs/client.rb', line 57

def blog_show(slug, digest: nil)
  path = base_path + '/blog/' + slug.sub(%r{^/}, '')
  path += '/' + digest + '.json' if digest

  get(path).tap do |resp|
    resp.assert_ok!
    check_default_property(resp)
  end
rescue WCC::Blogs::Client::NotFoundError => e
  raise WCC::Blogs::NotFoundException, e.message
end

#collection_show(key) ⇒ Object



43
44
45
46
# File 'lib/wcc/blogs/client.rb', line 43

def collection_show(key)
  path = base_path + '/collection/' + key
  get(path).assert_ok!
end

#get(path, query = {}) ⇒ Object

performs an HTTP GET request to the specified path within the configured space and environment. Query parameters are merged with the defaults and appended to the request.



27
28
29
30
31
32
33
# File 'lib/wcc/blogs/client.rb', line 27

def get(path, query = {})
  url = URI.join(@base_url, path)

  Response.new(self,
               { url: url, query: query },
               get_http(url, query))
end

#property_show(property = nil) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
# File 'lib/wcc/blogs/client.rb', line 35

def property_show(property = nil)
  property ||= default_property
  raise ArgumentError, 'No Property given' unless property

  path = base_path + '/property/' + property
  get(path).assert_ok!
end