Class: HashBlue::Contact

Inherits:
Client
  • Object
show all
Defined in:
lib/hash_blue/contact.rb

Overview

This class models the Contact entity, providing an easy way to CRUD operations using ActiveRecord as design model

Examples of use:

# Initialize client with a valid access token HashBlue::Client.user = <valid_access_token>

# Retrieve all contacts messages = HashBlue::Contact.find(:all)

# Retrieve a specific contact message = HashBlue::Contact.find(<valid_contact_id>)

# Create a contact HashBlue::Contact.create!(<phone_number>, <name>, <email>)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Client

client, delete, get, post, put

Constructor Details

#initialize(phone_number = nil, name = nil, email = nil) ⇒ Contact

Returns a new instance of Contact.



37
38
39
40
41
# File 'lib/hash_blue/contact.rb', line 37

def initialize(phone_number = nil, name = nil, email = nil)
  @phone_number = phone_number
  @name = name
  @email = email
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



35
36
37
# File 'lib/hash_blue/contact.rb', line 35

def email
  @email
end

#idObject

Returns the value of attribute id.



32
33
34
# File 'lib/hash_blue/contact.rb', line 32

def id
  @id
end

#nameObject

Returns the value of attribute name.



34
35
36
# File 'lib/hash_blue/contact.rb', line 34

def name
  @name
end

#phone_numberObject

Returns the value of attribute phone_number.



33
34
35
# File 'lib/hash_blue/contact.rb', line 33

def phone_number
  @phone_number
end

Class Method Details

.create!(phone_number, name, email) ⇒ Object

Create a new contact



76
77
78
79
# File 'lib/hash_blue/contact.rb', line 76

def create!(phone_number, name, email)
  contact = {:phone_number => phone_number, :name => name, :email => email}
  post "/contacts", {:contact => contact}
end

.find(arg = nil) ⇒ Object

Retrieve a specific contact or a set of contacts

> nil => retrieve all messages

> :all => retrieve all messages

> => <contact_id> => retrieve a specific contact messages

> id => retrieve a specific message using a valid unique identifier

Parameters:

  • arg:


61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/hash_blue/contact.rb', line 61

def find(arg = nil)
  if arg.nil?
    parse_response(get "/contacts")
  elsif arg.is_a? Symbol
    if arg.eql?(:all)
      parse_response(get "/contacts")
    else
      raise ArgumentError, "Invalid argument #{arg}"
    end
  elsif arg.is_a? String or arg.is_a? Fixnum
    parse_response(get "/contacts/#{arg}")
  end
end

Instance Method Details

#save!Object



43
44
45
46
# File 'lib/hash_blue/contact.rb', line 43

def save!
  contact = {:phone_number => phone_number, :name => name, :email => email}
  self.class.post "/contacts", {:contact => contact}
end

#to_sObject



48
49
50
# File 'lib/hash_blue/contact.rb', line 48

def to_s
  "#{self.class.name} [#{id}] => {:phone_number => #{phone_number}, :name => #{name}, :email => #{email}}"
end