Class: NextErpBridge::Core::FrappeClient
- Inherits:
-
Object
- Object
- NextErpBridge::Core::FrappeClient
- Includes:
- HTTParty
- Defined in:
- lib/next_erp_bridge/core/frappe_client.rb
Instance Attribute Summary collapse
-
#password ⇒ Object
Returns the value of attribute password.
-
#session_cookie ⇒ Object
Returns the value of attribute session_cookie.
-
#url ⇒ Object
Returns the value of attribute url.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #api_method(method_name, params) ⇒ Object
-
#fetch(data, filters = [], fields = [], limit_start = 0, limit_page_length = 20) ⇒ Object
fields : fields from the docType required in response.
-
#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”.
- #get_all_doc_types ⇒ Object
- #get_authenticated_user ⇒ Object
-
#initialize(opt_url = nil, opt_username = nil, opt_password = nil) ⇒ FrappeClient
constructor
A new instance of FrappeClient.
- #insert(data) ⇒ Object
- #login(username, password) ⇒ Object
- #parse_set_cookie(all_cookies_string) ⇒ Object
-
#raw_api(url, request_type, params) ⇒ Object
This method can be used to call any raw api supported by frappe/erpnext.
- #update(data) ⇒ Object
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
#password ⇒ Object
Returns the value of attribute password.
12 13 14 |
# File 'lib/next_erp_bridge/core/frappe_client.rb', line 12 def password @password end |
#session_cookie ⇒ Object
Returns the value of attribute session_cookie.
12 13 14 |
# File 'lib/next_erp_bridge/core/frappe_client.rb', line 12 def end |
#url ⇒ Object
Returns the value of attribute url.
12 13 14 |
# File 'lib/next_erp_bridge/core/frappe_client.rb', line 12 def url @url end |
#username ⇒ Object
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., "Accept" => "application/json", "X-Frappe-CSRF-Token" => self., "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., "Accept" => "application/json", "X-Frappe-CSRF-Token" => self. }) 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., "Accept" => "application/json", "X-Frappe-CSRF-Token" => self. }) return response.parsed_response end |
#get_all_doc_types ⇒ Object
30 31 32 |
# File 'lib/next_erp_bridge/core/frappe_client.rb', line 30 def get_all_doc_types # action = end |
#get_authenticated_user ⇒ Object
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" login_url = File.join(self.url, action) response = HTTParty.get(login_url, :headers => { "Cookie" => self., "Content-Type" => "application/json", "Accept" => "application/json", "X-Frappe-CSRF-Token" => self. }) 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., "Accept" => "application/json", "X-Frappe-CSRF-Token" => self., "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 login(username, password) action = "api/method/login" login_url = File.join(self.url, action) response = HTTParty.post(login_url, :body => {usr: username, pwd: password}) self. = "sid=#{parse_set_cookie(response.headers["set-cookie"])["sid"]}" end |
#parse_set_cookie(all_cookies_string) ⇒ Object
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 () = Hash.new if !.empty? .split(',').each do || = .strip.split(';')[0] = .strip.split('=') key = [0] value = [1] [key] = value end end 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., "Accept" => "application/json", "X-Frappe-CSRF-Token" => self. }) else response = HTTParty.post(encoded_url_path, :headers => { "Cookie" => self., "Accept" => "application/json", "X-Frappe-CSRF-Token" => self., "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., "Accept" => "application/json", "X-Frappe-CSRF-Token" => self., "application/x-www-form-urlencoded" => "multipart/form-data" }, :body => {:data => data.to_json}) return response.parsed_response end |