Class: NextErpBridge::Core::FrappeClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/next_erp_bridge/core/frappe_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt_url = nil, opt_username = nil, opt_password = nil) ⇒ FrappeClient



17
18
19
20
21
# File 'lib/next_erp_bridge/core/frappe_client.rb', line 17

def initialize(opt_url=nil, opt_username=nil, opt_password=nil)
  self.url = opt_url || ERPNEXT_CONFIG["host_url"]
  self.username = opt_username || ERPNEXT_CONFIG["username"]
  self.password = opt_password || ERPNEXT_CONFIG["password"]
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



12
13
14
# File 'lib/next_erp_bridge/core/frappe_client.rb', line 12

def password
  @password
end

Returns the value of attribute session_cookie.



12
13
14
# File 'lib/next_erp_bridge/core/frappe_client.rb', line 12

def session_cookie
  @session_cookie
end

#urlObject

Returns the value of attribute url.



12
13
14
# File 'lib/next_erp_bridge/core/frappe_client.rb', line 12

def url
  @url
end

#usernameObject

Returns the value of attribute username.



12
13
14
# File 'lib/next_erp_bridge/core/frappe_client.rb', line 12

def username
  @username
end

Instance Method Details

#api_method(method_name, params) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/next_erp_bridge/core/frappe_client.rb', line 118

def api_method(method_name, params)
  url_path = self.url + "/api/method/" + method_name
  encoded_url_path = URI.encode url_path

  response = HTTParty.post(encoded_url_path, :headers => {
    "Cookie" => self.session_cookie, "Accept" => "application/json",
    "X-Frappe-CSRF-Token" => self.session_cookie,
    "application/x-www-form-urlencoded" => "multipart/form-data"
    }, :body => params)
  return response.parsed_response
end

#fetch(data, filters = [], fields = [], limit_start = 0, limit_page_length = 20) ⇒ Object

fields : fields from the docType required in response. default is only name fields can be set as follows : [“name”,“email_id”]



70
71
72
73
74
75
76
77
78
79
# File 'lib/next_erp_bridge/core/frappe_client.rb', line 70

def fetch(data, filters=[], fields=[], limit_start=0, limit_page_length=20)
  url_path = self.url + "/api/resource/" + data[:doctype] + "?filters=#{filters}&fields=#{fields}&limit_start=#{limit_start}&limit_page_length=#{limit_page_length}"
  encoded_url_path = URI.encode url_path

  response = HTTParty.get(encoded_url_path, :headers => {
    "Cookie" => self.session_cookie, "Accept" => "application/json",
    "X-Frappe-CSRF-Token" => self.session_cookie
  })
  return response.parsed_response
end

#fetch_single_record(data) ⇒ Object

To fetch all the details from ERP for single record Example log in login_url = “nest.erpnext.com/api/resource/Lead/LEAD-00420



83
84
85
86
87
88
89
90
91
92
# File 'lib/next_erp_bridge/core/frappe_client.rb', line 83

def fetch_single_record(data)
  url_path = self.url + "/api/resource/" + data[:doctype] + "/" + data[:id]
  encoded_url_path = URI.encode url_path

  response = HTTParty.get(encoded_url_path, :headers => {
    "Cookie" => self.session_cookie, "Accept" => "application/json",
    "X-Frappe-CSRF-Token" => self.session_cookie
    })
  return response.parsed_response
end

#get_all_doc_typesObject



30
31
32
# File 'lib/next_erp_bridge/core/frappe_client.rb', line 30

def get_all_doc_types
  # action =
end

#get_authenticated_userObject



34
35
36
37
38
39
40
41
42
# File 'lib/next_erp_bridge/core/frappe_client.rb', line 34

def get_authenticated_user
  action = "api/method/frappe.auth.get_logged_user"
   = File.join(self.url, action)
  response = HTTParty.get(, :headers => {
    "Cookie" => self.session_cookie, "Content-Type" => "application/json",
    "Accept" => "application/json", "X-Frappe-CSRF-Token" => self.session_cookie
    })
  return response.parsed_response
end

#insert(data) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/next_erp_bridge/core/frappe_client.rb', line 94

def insert(data)
  url_path = self.url + "/api/resource/" + data[:doctype]
  encoded_url_path = URI.encode url_path

  response = HTTParty.post(encoded_url_path, :headers => {
    "Cookie" => self.session_cookie, "Accept" => "application/json",
    "X-Frappe-CSRF-Token" => self.session_cookie,
    "application/x-www-form-urlencoded" => "multipart/form-data"
    }, :body => {:data => data.to_json})
  return response.parsed_response
end

#login(username, password) ⇒ Object



23
24
25
26
27
28
# File 'lib/next_erp_bridge/core/frappe_client.rb', line 23

def (username, password)
  action = "api/method/login"
   = File.join(self.url, action)
  response = HTTParty.post(, :body => {usr: username, pwd: password})
  self.session_cookie = "sid=#{parse_set_cookie(response.headers["set-cookie"])["sid"]}"
end


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/next_erp_bridge/core/frappe_client.rb', line 44

def parse_set_cookie(all_cookies_string)
  cookies = Hash.new

  if !all_cookies_string.empty?
    all_cookies_string.split(',').each do |single_cookie_string|
      cookie_part_string  = single_cookie_string.strip.split(';')[0]
      cookie_part         = cookie_part_string.strip.split('=')
      key                 = cookie_part[0]
      value               = cookie_part[1]

      cookies[key] = value
    end
  end

  cookies
end

#raw_api(url, request_type, params) ⇒ Object

This method can be used to call any raw api supported by frappe/erpnext



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/next_erp_bridge/core/frappe_client.rb', line 132

def raw_api(url, request_type, params)
  url_path = self.url + url
  encoded_url_path = URI.encode url_path
  if request_type == "Get"
    response = HTTParty.get(encoded_url_path, :headers => {
    "Cookie" => self.session_cookie, "Accept" => "application/json",
    "X-Frappe-CSRF-Token" => self.session_cookie
    })
  else
    response = HTTParty.post(encoded_url_path, :headers => {
    "Cookie" => self.session_cookie, "Accept" => "application/json",
    "X-Frappe-CSRF-Token" => self.session_cookie,
    "Content-Type" => "application/x-www-form-urlencoded" }, :body => params)
  end
  return response.parsed_response
end

#update(data) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/next_erp_bridge/core/frappe_client.rb', line 106

def update(data)
  url_path = self.url + "/api/resource/" + data[:doctype] + "/" + data[:id]
  encoded_url_path = URI.encode url_path

  response = HTTParty.put(encoded_url_path, :headers => {
    "Cookie" => self.session_cookie, "Accept" => "application/json",
    "X-Frappe-CSRF-Token" => self.session_cookie,
    "application/x-www-form-urlencoded" => "multipart/form-data"
    }, :body => {:data => data.to_json})
  return response.parsed_response
end