Module: NetSuiteRails::Transformations

Defined in:
lib/netsuite_rails/transformations.rb

Class Method Summary collapse

Class Method Details

.company_name(company_name, direction = :push) ⇒ Object

company_name field has a 83 character limit



27
28
29
30
31
32
33
# File 'lib/netsuite_rails/transformations.rb', line 27

def company_name(company_name, direction = :push)
  if direction == :push
    company_name[0..82]
  else
    company_name
  end
end

.date(date, direction = :push) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/netsuite_rails/transformations.rb', line 117

def date(date, direction = :push)
  if direction == :push
    # setting the hour to noon eliminates the chance that some strange timezone offset
    # shifting would cause the date to drift into the next or previous day
    date.to_datetime.change(offset: "-07:00", hour: 12)
  else
    date.change(offset: Time.zone.formatted_offset)
  end
end

.datetime(datetime, direction = :push) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/netsuite_rails/transformations.rb', line 127

def datetime(datetime, direction = :push)
  if direction == :push
    datetime.change(offset: "-08:00", year: 1970, day: 01, month: 01) - (8 + NetSuiteRails::Configuration.netsuite_instance_time_zone_offset).hours
  else
    datetime = datetime.change(offset: Time.zone.formatted_offset) + (8 + NetSuiteRails::Configuration.netsuite_instance_time_zone_offset).hours
    datetime
  end
end

.email(email, direction = :push) ⇒ Object

NS will throw an error if whitespace bumpers the email string



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/netsuite_rails/transformations.rb', line 85

def email(email, direction = :push)
  if direction == :push
    # any whitespace will cause netsuite to throw a fatal error
    email = email.gsub(' ', '')

    # TODO consider throwing an exception instead of returning nil?
    # netsuite will throw a fatal error if a valid email address is not sent
    # http://stackoverflow.com/questions/742451/what-is-the-simplest-regular-expression-to-validate-emails-to-not-accept-them-bl
    if email !~ /.+@.+\..+/
      return nil
    end

    email = email.
      # an error will be thrown if period is on the end of a sentence
      gsub(/[^A-Za-z]+$/, '').
      # any commas in the email with throw an error
      gsub(',', '').
      # double periods anywhere in the email cause issues
      gsub('..', '.').
      # a period before the @ seems to cause issues
      gsub('.@', '@')

    email
  else
    email
  end
end

.firstname(firstname, direction = :push) ⇒ Object

NS limits firstname fields to 33 characters



36
37
38
39
40
41
42
# File 'lib/netsuite_rails/transformations.rb', line 36

def firstname(firstname, direction = :push)
  if direction == :push
    firstname[0..33]
  else
    firstname
  end
end

.float(number, direction = :push) ⇒ Object

TODO consider adding precision?



54
55
56
# File 'lib/netsuite_rails/transformations.rb', line 54

def float(number, direction = :push)
  number.to_f
end

.gift_card_code(code, direction = :push) ⇒ Object

gift certificate codes have a maximum of 9 characters



18
19
20
21
22
23
24
# File 'lib/netsuite_rails/transformations.rb', line 18

def gift_card_code(code, direction = :push)
  if direction == :push
    code[0..8]
  else
    code
  end
end

.integer(number, direction = :push) ⇒ Object



49
50
51
# File 'lib/netsuite_rails/transformations.rb', line 49

def integer(number, direction = :push)
  number.to_i
end

.lastname(lastname, direction = :push) ⇒ Object

same limitations as firstname



45
46
47
# File 'lib/netsuite_rails/transformations.rb', line 45

def lastname(lastname, direction = :push)
  firstname(lastname, direction)
end

.memo(memo, direction = :push) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/netsuite_rails/transformations.rb', line 9

def memo(memo, direction = :push)
  if direction == :push
    memo[0..999]
  else
    memo
  end
end

.phone(phone, direction = :push) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/netsuite_rails/transformations.rb', line 58

def phone(phone, direction = :push)
  if direction == :push
    return nil if phone.nil?

    formatted_phone = phone.
      strip.
      gsub(/ext(ension)?/, 'x').
      # remove anything that isn't a extension indicator or a number
      gsub(/[^0-9x]/, '').
      # if the first part of the phone # is 10 characters long and starts with a 1 the 22 char error is thrown
      gsub(/^1([0-9]{10})/, '\1')

    # eliminate the extension if the number is still too long
    formatted_phone.gsub!(/x.*$/, '') if formatted_phone.size > 22

    # phone numbers less than 7 digits throw a fatal error
    if formatted_phone.size < 7
      return nil
    end

    formatted_phone
  else
    phone
  end
end

.transform(type, value, direction) ⇒ Object



5
6
7
# File 'lib/netsuite_rails/transformations.rb', line 5

def transform(type, value, direction)
  self.send(type, value, direction)
end