Class: Hubspot::Blog

Inherits:
Object
  • Object
show all
Defined in:
lib/hubspot/blog.rb

Overview

HubSpot Contacts API

Constant Summary collapse

BLOG_LIST_PATH =
"/content/api/v2/blogs"
BLOG_POSTS_PATH =
"/content/api/v2/blog-posts"
GET_BLOG_BY_ID_PATH =
"/content/api/v2/blogs/:blog_id"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response_hash) ⇒ Blog

Returns a new instance of Blog.



31
32
33
# File 'lib/hubspot/blog.rb', line 31

def initialize(response_hash)
  @properties = response_hash #no need to parse anything, we have properties
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



29
30
31
# File 'lib/hubspot/blog.rb', line 29

def properties
  @properties
end

Class Method Details

.find_by_id(id) ⇒ Object

Returns:

  • Hubspot::Blog



23
24
25
26
# File 'lib/hubspot/blog.rb', line 23

def find_by_id(id)
  response = Hubspot::Connection.get_json(GET_BLOG_BY_ID_PATH, { blog_id: id })
  new(response)
end

.listHubspot::Blog

Lists the blogs https://developers.hubspot.com/docs/methods/blogv2/get_blogs No param filtering is currently implemented

Returns:



15
16
17
18
# File 'lib/hubspot/blog.rb', line 15

def list
  response = Hubspot::Connection.get_json(BLOG_LIST_PATH, {})
  response['objects'].map { |b| new(b) }
end

Instance Method Details

#[](property) ⇒ Object



35
36
37
# File 'lib/hubspot/blog.rb', line 35

def [](property)
  @properties[property]
end

#posts(params = {}) ⇒ Hubspot::BlogPost

Returns the posts for this blog instance.

defaults to returning the last 2 months worth of published blog posts
in date descending order (i.e. most recent first)

https://developers.hubspot.com/docs/methods/blogv2/get_blog_posts



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hubspot/blog.rb', line 45

def posts(params = {})
  default_params = {
    content_group_id: self["id"],
    order_by: '-created',
    created__gt: Time.now - 2.month,
    state: 'PUBLISHED'
  }
  raise Hubspot::InvalidParams.new('params must be passed as a hash') unless params.is_a?(Hash)
  params = default_params.merge(params)

  raise Hubspot::InvalidParams.new('State parameter was invalid') unless [false, 'PUBLISHED', 'DRAFT'].include?(params[:state])
  params.each { |k, v| params.delete(k) if v == false }

  response = Hubspot::Connection.get_json(BLOG_POSTS_PATH, params)
  response['objects'].map { |p| BlogPost.new(p) }
end