Class: Broadlistening::Comment

Inherits:
Object
  • Object
show all
Defined in:
lib/broadlistening/comment.rb

Overview

Represents a normalized comment in the pipeline.

Comments are the input data to the pipeline, containing user opinions that will be processed into arguments through the extraction step.

Examples:

Creating from a hash

comment = Comment.from_hash({ id: "1", body: "I think...", attribute_age: "30代" })
comment.id        # => "1"
comment.body      # => "I think..."
comment.attributes # => { "age" => "30代" }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, body:, proposal_id: nil, source_url: nil, attributes: nil, properties: nil) ⇒ Comment

Returns a new instance of Comment.



17
18
19
20
21
22
23
24
# File 'lib/broadlistening/comment.rb', line 17

def initialize(id:, body:, proposal_id: nil, source_url: nil, attributes: nil, properties: nil)
  @id = id
  @body = body
  @proposal_id = proposal_id
  @source_url = source_url
  @attributes = attributes
  @properties = properties
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



15
16
17
# File 'lib/broadlistening/comment.rb', line 15

def attributes
  @attributes
end

#bodyObject

Returns the value of attribute body.



15
16
17
# File 'lib/broadlistening/comment.rb', line 15

def body
  @body
end

#idObject

Returns the value of attribute id.



15
16
17
# File 'lib/broadlistening/comment.rb', line 15

def id
  @id
end

#propertiesObject

Returns the value of attribute properties.



15
16
17
# File 'lib/broadlistening/comment.rb', line 15

def properties
  @properties
end

#proposal_idObject

Returns the value of attribute proposal_id.



15
16
17
# File 'lib/broadlistening/comment.rb', line 15

def proposal_id
  @proposal_id
end

#source_urlObject

Returns the value of attribute source_url.



15
16
17
# File 'lib/broadlistening/comment.rb', line 15

def source_url
  @source_url
end

Class Method Details

.from_hash(hash, property_names: []) ⇒ Comment

Create a Comment from a hash, normalizing various input formats

Parameters:

  • hash (Hash)

    Input hash with comment data

  • property_names (Array<String>) (defaults to: [])

    Property names to extract (from config)

Returns:



31
32
33
34
35
36
37
38
39
40
# File 'lib/broadlistening/comment.rb', line 31

def self.from_hash(hash, property_names: [])
  new(
    id: hash[:id] || hash["id"],
    body: hash[:body] || hash["body"],
    proposal_id: hash[:proposal_id] || hash["proposal_id"],
    source_url: extract_source_url(hash),
    attributes: extract_attributes(hash),
    properties: extract_properties(hash, property_names)
  )
end

.from_object(obj, property_names: []) ⇒ Comment

Create a Comment from an object (e.g., ActiveRecord model)

Parameters:

  • obj (Object)

    Object responding to id, body, etc.

  • property_names (Array<String>) (defaults to: [])

    Property names to extract

Returns:



47
48
49
50
51
52
53
54
55
56
# File 'lib/broadlistening/comment.rb', line 47

def self.from_object(obj, property_names: [])
  new(
    id: obj.id,
    body: obj.body,
    proposal_id: obj.respond_to?(:proposal_id) ? obj.proposal_id : nil,
    source_url: obj.respond_to?(:source_url) ? obj.source_url : nil,
    attributes: extract_attributes_from_object(obj),
    properties: extract_properties_from_object(obj, property_names)
  )
end

Instance Method Details

#empty?Boolean

Check if comment body is empty or nil

Returns:

  • (Boolean)


75
76
77
# File 'lib/broadlistening/comment.rb', line 75

def empty?
  @body.nil? || @body.strip.empty?
end

#to_hHash

Convert to hash for serialization

Returns:

  • (Hash)


61
62
63
64
65
66
67
68
69
70
# File 'lib/broadlistening/comment.rb', line 61

def to_h
  {
    id: @id,
    body: @body,
    proposal_id: @proposal_id,
    source_url: @source_url,
    attributes: @attributes,
    properties: @properties
  }
end