Class: Resources::ThirdParty

Inherits:
BaseResource show all
Includes:
ThirdPartyPrompter
Defined in:
lib/pvdgm-svc-client/resources/third_party.rb

Constant Summary collapse

EMAIL_REGEX =
/\A[\w\.%\+\-\']+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|jobs|museum)\z/i

Instance Attribute Summary

Attributes inherited from BaseResource

#options, #prompter

Instance Method Summary collapse

Methods included from ThirdPartyPrompter

#third_party_id

Methods inherited from BaseResource

#initialize

Constructor Details

This class inherits a constructor from BaseResource

Instance Method Details

#createObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pvdgm-svc-client/resources/third_party.rb', line 36

def create
  params = { 
    third_party: {
      name: prompter.ask("\nName for new third party: ") { |q| q.validate = /\A.{1,255}\z/; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid Third Party Name" },
      key: prompter.ask("\nUnique key for third party: ") { |q| q.validate = /\A[a-zA-Z0-9_-]{1,255}\z/; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid Third Party key" },
      account_id: prompter.ask("\nAccount ID: ") { |q| q.validate = lambda { | a | a.blank? || is_valid_object?('Account', a) }; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid Account ID" },
      contact_email: prompter.ask("\nContact email address for new third party: ") { |q| q.validate = EMAIL_REGEX; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid email address" }
    }
  }
  result = post("services/third_parties", params)
  puts "\nID of new third party: #{result['id']}"
  puts
end

#listObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/pvdgm-svc-client/resources/third_party.rb', line 8

def list
  result = get("services/third_parties")
  puts "\nThird Parties:"
  table = Terminal::Table.new headings: [ 'Name', 'Key', 'Account ID', 'Contact Email' ] do |t|
    result.each do | third_party |
      t << [ third_party['name'], third_party['key'], third_party['account_id'].present? ? "#{third_party['account_name']} (#{third_party['account_id']})" : '' , third_party['contact_email'] ]
    end
  end

  prompter.say table.to_s
  puts
end

#showObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pvdgm-svc-client/resources/third_party.rb', line 21

def show
  tp_id = third_party_id
  result = get("services/third_parties/#{tp_id}")
  @third_party_name = result['name']
  @third_party_key = result['key']
  @third_party_acct_id = result['account_id']
  @third_party_contact_email = result['contact_email']
  puts "\nThird Party"
  table = Terminal::Table.new headings: [ 'Name', 'Key', 'Account ID', 'Contact Email' ] do |t|
    t << [ result['name'], result['key'], result['account_id'].present? ? "#{result['account_name']} (#{result['account_id']})" : '', result['contact_email'] ]
  end
  puts table
  puts
end

#updateObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pvdgm-svc-client/resources/third_party.rb', line 50

def update
  show
  params = { 
    third_party: {
      name: prompter.ask("\nNew name for third party: ") { |q| q.default = @third_party_name; q.validate = /\A.{1,255}\z/; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid Third Party Name" },
      key: prompter.ask("\nUnique key for third party: ") { |q| q.default = @third_party_key; q.validate = /\A[a-zA-Z0-9_-]{1,255}\z/; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid Third Party key" },
      account_id: prompter.ask("\nAccount ID: ") { |q| q.default = @acct_id; q.validate = lambda { | a | a.blank? || is_valid_object?('Account', a) }; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid Account ID" },
      contact_email: prompter.ask("\nNew contact email address for third party: ") { |q| q.default = @third_party_contact_email; q.validate = EMAIL_REGEX; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid email address" }
    }
  }
  result = put("services/third_parties/#{third_party_id}", params)
  puts "\nID of updated third party: #{result['id']}"
  puts
end