Class: Rho::RhoContact

Inherits:
Object show all
Defined in:
lib/framework/rho/rhocontact.rb,
lib/framework/autocomplete/Rho.rb

Constant Summary collapse

AUTHORIZATION_STATUS_NOT_DETERMINED =
'Not Determined'
AUTHORIZATION_STATUS_RESTRICTED =
'Restricted'
AUTHORIZATION_STATUS_DENIED =
'Denied'
AUTHORIZATION_STATUS_AUTHORIZED =
'Authorized'

Class Method Summary collapse

Class Method Details

.close_phonebook(phonebook) ⇒ Object



50
51
52
# File 'lib/framework/rho/rhocontact.rb', line 50

def close_phonebook(phonebook)
  Phonebook::closePhonebook(phonebook)
end

.create!(properties, phonebook) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/framework/rho/rhocontact.rb', line 83

def create!(properties, phonebook = nil)
  pb = phonebook
  if phonebook == nil
    pb = Phonebook::openPhonebook
  end
  unless pb.nil?
    record = Phonebook::createRecord(pb)
    if record.nil?
      puts "Can't find record " + properties['id']
    else
      properties.each do |key,value|
        Phonebook::setRecordValue(record,key,value)
      end
      Phonebook::addRecord(pb,record)
    end
    if phonebook == nil
      Phonebook::closePhonebook(pb)
    end
  end
end

.destroy(recordId, phonebook) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/framework/rho/rhocontact.rb', line 104

def destroy(recordId, phonebook = nil)
  pb = phonebook
  if phonebook == nil
    pb = Phonebook::openPhonebook
  end
  unless pb.nil?
    record = Phonebook::openRecord(pb,recordId)
    if record.nil?
      puts "Can't find record " + recordId
    else
      Phonebook::deleteRecord(pb,record)
    end
    if phonebook == nil
      Phonebook::closePhonebook(pb)
    end
  end
  return record
end

.find(args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/framework/rho/rhocontact.rb', line 54

def find(*args)
  result = nil
  phonebook = args[1][:phonebook] if args[1]
  pb = Phonebook::openPhonebook if phonebook.nil?

  if pb.nil?
    puts "Can't open phonebook"
  elsif args.first.nil? or args.length == 0
    puts "There are no arguments to find contacts"
  elsif args.first == :all or args.first == :first
    if System::get_property('platform') == "ANDROID" or System::get_property('platform') == "APPLE"
      result = Phonebook::getRecords(pb, args[1])
    else
      result = Phonebook::allRecords(pb)
    end
  elsif args.first == :count
    if System::get_property('platform') == "ANDROID" or System::get_property('platform') == "APPLE"
      result = Phonebook::countRecords(pb, args[1])
    else
      result = Phonebook::countRecords(pb);
    end
  else
    result = Phonebook::getRecord(pb, args.first)
  end

  Phonebook::closePhonebook(pb) if phonebook.nil?
  result
end

.get_authorization_statusObject



37
38
39
40
41
42
43
# File 'lib/framework/rho/rhocontact.rb', line 37

def self.get_authorization_status
  result = AUTHORIZATION_STATUS_AUTHORIZED
  if System::get_property('platform') == "ANDROID" or System::get_property('platform') == "APPLE"
    result = Phonebook::get_authorization_status()
  end
  result
end

.open_phonebookObject



46
47
48
# File 'lib/framework/rho/rhocontact.rb', line 46

def open_phonebook()
  return Phonebook::openPhonebook
end

.select(index, block) ⇒ Object

Select reads all contacts with keys provided in index hash If value of corresponding key is not nil then only matching contacts would be returned

Examples of how to use select method:

selected = Rho::RhoContact.select(‘first_name’ => ‘Kate’)

> Returns all records of Kate

selected = Rho::RhoContact.select(‘first_name’ => ‘David’, ‘last_name’ => ‘Taylor’, ‘email_address’ => nil)

> returns record(s) of the David Taylor with his email address

selected = Rho::RhoContact.select(‘first_name’ => ‘Test’, ‘last_name’ => ‘User’, ‘company_name’ => “rhomobile”)

> returns all records of the Test User from the company rhomobile



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/framework/rho/rhocontact.rb', line 158

def select(index, &block)
  working = find(:all, :select => index.keys)
  index.each do |k, v|
    unless v.nil?
      working.keep_if {|key, value| value[k] == v }
    end
  end
  found = working.to_a
  unless found.nil? or block.nil?
    return found.select(&block)
  end
  return found
end

.select_by_name(first_last_name, block) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/framework/rho/rhocontact.rb', line 172

def select_by_name(first_last_name, &block)
  if @contacts.nil?
    @contacts = find(:all).to_a.sort! do |x,y|
      xname = (x[1]['first_name'] or "") + " " + (x[1]['last_name'] or "")
      yname = (y[1]['first_name'] or "") + " " + (y[1]['last_name'] or "")
      xname <=> yname
    end
  end

  range = @contacts.bsearch_range do |x|
    (x[1]['first_name'] or "") + " " + (x[1]['last_name'] or "") <=> first_last_name
  end
  found = @contacts[range]
  unless found.nil? or block.nil?
    return found.select(&block)
  end
  return found
end

.update_attributes(properties, phonebook) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/framework/rho/rhocontact.rb', line 123

def update_attributes(properties, phonebook = nil)
  pb = phonebook
  if phonebook == nil
    pb = Phonebook::openPhonebook
  end
  unless pb.nil?
    record = Phonebook::openRecord(pb, properties['id'])
    if record.nil?
      puts "Can't find record " + properties['id']
    else
      properties.each do |key,value|
        Phonebook::setRecordValue(record,key,value)
      end
      Phonebook::saveRecord(pb,record)
    end
    if phonebook == nil
      Phonebook::closePhonebook(pb)
    end
  end
end