Class: Hubspot::Resource

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

Direct Known Subclasses

Company, Contact

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id_or_properties = nil) ⇒ Resource

Returns a new instance of Resource.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/hubspot/resource.rb', line 54

def initialize(id_or_properties = nil)
  @changes = HashWithIndifferentAccess.new
  @properties = HashWithIndifferentAccess.new

  case id_or_properties
  when Integer, NilClass
    @id = id_or_properties
  when String
    @id = Integer id_or_properties
  when Hash
    @id = id_or_properties.delete(id_field) || id_or_properties.delete(:id)

    add_accessors(id_or_properties.keys)
    id_or_properties.each do |k, v|
      send "#{k}=", v
    end
  else
    raise InvalidParams.new("#{self.class.name} must be initialized with an ID, hash, or nil")
  end

  @persisted = @id.present?
  @deleted = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object (protected)



243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/hubspot/resource.rb', line 243

def method_missing(method_name, *arguments, &block)
  # When assigning a missing attribute define the accessors and set the value
  if method_name.to_s.end_with?("=")
    attr = method_name.to_s[0...-1].to_sym
    add_accessors([attr])

    # Call the new setter
    return send(method_name, arguments[0])
  elsif @properties.key?(method_name)
    return @properties[method_name]['value']
  else
    super
  end
end

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



84
85
86
# File 'lib/hubspot/resource.rb', line 84

def changes
  @changes
end

#idObject

Returns the value of attribute id.



78
79
80
# File 'lib/hubspot/resource.rb', line 78

def id
  @id
end

#metadataObject (readonly)

Returns the value of attribute metadata.



85
86
87
# File 'lib/hubspot/resource.rb', line 85

def 
  @metadata
end

#propertiesObject (readonly)

Returns the value of attribute properties.



86
87
88
# File 'lib/hubspot/resource.rb', line 86

def properties
  @properties
end

Class Method Details

.create(properties = {}) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/hubspot/resource.rb', line 23

def create(properties = {})
  request = {
    properties: Hubspot::Utils.hash_to_properties(properties.stringify_keys, key_name: property_name_field)
  }
  response = Hubspot::Connection.post_json(create_path, params: {}, body: request)
  from_result(response)
end

.create_pathObject



161
162
163
164
165
166
167
# File 'lib/hubspot/resource.rb', line 161

def self.create_path
  begin
    self::CREATE_PATH
  rescue NameError
    raise "CREATE_PATH not defined for #{self.class.name}"
  end
end

.delete_pathObject



197
198
199
200
201
202
203
# File 'lib/hubspot/resource.rb', line 197

def self.delete_path
  begin
    self::DELETE_PATH
  rescue NameError
    raise "CREATE_PATH not defined for #{self.class.name}"
  end
end

.find(id) ⇒ Object



18
19
20
21
# File 'lib/hubspot/resource.rb', line 18

def find(id)
  instance = new(id)
  instance.reload
end

.find_pathObject



173
174
175
176
177
178
179
# File 'lib/hubspot/resource.rb', line 173

def self.find_path
  begin
    self::FIND_PATH
  rescue NameError
    raise "FIND_PATH not defined for #{self.class.name}"
  end
end

.from_result(result, id_field: self.id_field) ⇒ Object



12
13
14
15
16
# File 'lib/hubspot/resource.rb', line 12

def from_result(result, id_field: self.id_field)
  resource = new(result[id_field])
  resource.send(:initialize_from, result.with_indifferent_access)
  resource
end

.update(id, properties = {}) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/hubspot/resource.rb', line 31

def update(id, properties = {})
  begin
    update!(id, properties)
  rescue Hubspot::RequestError => e
    false
  end
end

.update!(id, properties = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hubspot/resource.rb', line 39

def update!(id, properties = {})
  request = {
    properties: Hubspot::Utils.hash_to_properties(properties.stringify_keys, key_name: property_name_field)
  }

  if update_method == "put"
    response = Hubspot::Connection.put_json(update_path, params: { id: id, no_parse: true }, body: request)
  else
    response = Hubspot::Connection.post_json(update_path, params: { id: id, no_parse: true }, body: request)
  end

  response.success?
end

.update_pathObject



185
186
187
188
189
190
191
# File 'lib/hubspot/resource.rb', line 185

def self.update_path
  begin
    self::UPDATE_PATH
  rescue NameError
    raise "UPDATE_PATH not defined for #{self.class.name}"
  end
end

Instance Method Details

#[](name) ⇒ Object



92
93
94
# File 'lib/hubspot/resource.rb', line 92

def [](name)
  @changes[name] || @properties.dig(name, 'value')
end

#changed?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/hubspot/resource.rb', line 88

def changed?
  !@changes.empty?
end

#deleteObject



145
146
147
148
149
150
151
152
153
# File 'lib/hubspot/resource.rb', line 145

def delete
  raise(Hubspot::InvalidParams.new("Resource must have an ID")) if @id.nil?

  Hubspot::Connection.delete_json(delete_path, id: @id)

  @deleted = true
  @changes = HashWithIndifferentAccess.new
  true
end

#deleted?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/hubspot/resource.rb', line 155

def deleted?
  @deleted
end

#persisted?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/hubspot/resource.rb', line 105

def persisted?
  @persisted
end

#reloadObject



96
97
98
99
100
101
102
103
# File 'lib/hubspot/resource.rb', line 96

def reload
  raise(Hubspot::InvalidParams.new("Resource must have an ID")) if @id.nil?

  response = Hubspot::Connection.get_json(find_path, id: @id)
  initialize_from(response.with_indifferent_access)

  self
end

#saveObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/hubspot/resource.rb', line 109

def save
  request = {
    properties: Hubspot::Utils.hash_to_properties(@changes.stringify_keys, key_name: property_name_field)
  }

  if persisted?
    if update_method == "put"
      response = Hubspot::Connection.put_json(update_path, params: { id: @id }, body: request)
    else
      response = Hubspot::Connection.post_json(update_path, params: { id: @id }, body: request)
    end

    update_from_changes
  else
    response = Hubspot::Connection.post_json(create_path, params: {}, body: request)

    # Grab the new ID from the response
    @id = response[id_field]

    # Update the fields with the response
    initialize_from(response.with_indifferent_access)
  end

  @persisted = true
  true
end

#to_iObject



80
81
82
# File 'lib/hubspot/resource.rb', line 80

def to_i
  @id
end

#update(properties) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/hubspot/resource.rb', line 136

def update(properties)
  if properties && !properties.is_a?(Hash)
    raise ArgumentError, "When assigning properties, you must pass a hash as an argument."
  end

  @changes = @changes.merge(properties)
  save
end