Class: SugarCRM::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/sugarcrm/connection/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, method, json, debug = false) ⇒ Request

Returns a new instance of Request.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sugarcrm/connection/request.rb', line 8

def initialize(url, method, json, debug=false)
  @url      = url
  @method   = method
  @json     = escape(json)
  @request  = 'method=' << @method.to_s
  @request << '&input_type=JSON'
  @request << '&response_type=JSON'
  @request << '&rest_data=' << @json
  if debug
    puts "#{method}: Request:"
    puts json 
    puts "\n"
  end
  self
end

Instance Attribute Details

#http_methodObject (readonly)

Returns the value of attribute http_method.



6
7
8
# File 'lib/sugarcrm/connection/request.rb', line 6

def http_method
  @http_method
end

#jsonObject

Returns the value of attribute json.



5
6
7
# File 'lib/sugarcrm/connection/request.rb', line 5

def json
  @json
end

#methodObject

Returns the value of attribute method.



4
5
6
# File 'lib/sugarcrm/connection/request.rb', line 4

def method
  @method
end

#requestObject

Returns the value of attribute request.



2
3
4
# File 'lib/sugarcrm/connection/request.rb', line 2

def request
  @request
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/sugarcrm/connection/request.rb', line 3

def url
  @url
end

Instance Method Details

#bytesizeObject



38
39
40
# File 'lib/sugarcrm/connection/request.rb', line 38

def bytesize
  self.to_s.bytesize
end

#convert_reserved_characters(string) ⇒ Object

A tiny helper for converting reserved characters for html encoding



52
53
54
55
56
57
58
59
# File 'lib/sugarcrm/connection/request.rb', line 52

def convert_reserved_characters(string)
  string.gsub!(/&quot;/, '\"')
  string.gsub!(/&apos;/, '\'')
  string.gsub!(/&amp;/,  '\&')
  string.gsub!(/&lt;/,   '\<')
  string.gsub!(/&lt;/,   '\>')
  string
end

#escape(json) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/sugarcrm/connection/request.rb', line 24

def escape(json)
  # BUG: SugarCRM doesn't properly handle '&quot;' inside of JSON for some reason.  Let's unescape any html elements.
  j = convert_reserved_characters(json)
  # Now we escape the resulting string.
  j = CGI.escape(j)
  j
end

#lengthObject



42
43
44
# File 'lib/sugarcrm/connection/request.rb', line 42

def length
  self.to_s.length
end

#to_sObject Also known as: to_str



46
47
48
# File 'lib/sugarcrm/connection/request.rb', line 46

def to_s
  @request
end

#unescapeObject

TODO: Fix this so that it JSON.parse will consume it.



33
34
35
36
# File 'lib/sugarcrm/connection/request.rb', line 33

def unescape
  j = CGI.unescape(@json)
  j.gsub!(/\n/, '')
end