Module: SmoothOperator::Operator::ClassMethods
- Defined in:
- lib/smooth_operator/operator.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
- #build_options(options) ⇒ Object
- #build_url(relative_path) ⇒ Object
- #delete(relative_path, options = {}) ⇒ Object
- #get(relative_path, options = {}) ⇒ Object
- #make_the_call(http_verb, relative_path, options = {}) ⇒ Object
- #post(relative_path, options = {}) ⇒ Object
- #put(relative_path, options = {}) ⇒ Object
- #safe_delete(relative_path, options = {}) ⇒ Object
- #safe_get(relative_path, options = {}) ⇒ Object
- #safe_post(relative_path, options = {}) ⇒ Object
- #safe_put(relative_path, options = {}) ⇒ Object
- #safe_response(response) ⇒ Object
- #set_basic_auth_credentials(options) ⇒ Object
- #successful_response?(response) ⇒ Boolean
Instance Attribute Details
#endpoint ⇒ Object
15 16 17 |
# File 'lib/smooth_operator/operator.rb', line 15 def endpoint @endpoint ||= ENV["API_ENDPOINT"] end |
#endpoint_pass ⇒ Object
25 26 27 |
# File 'lib/smooth_operator/operator.rb', line 25 def endpoint_pass @endpoint_pass ||= ENV["API_PASS"] end |
#endpoint_user ⇒ Object
20 21 22 |
# File 'lib/smooth_operator/operator.rb', line 20 def endpoint_user @endpoint_user ||= ENV["API_USER"] end |
Instance Method Details
#build_options(options) ⇒ Object
88 89 90 91 |
# File 'lib/smooth_operator/operator.rb', line 88 def () = {} if .blank? set_basic_auth_credentials() end |
#build_url(relative_path) ⇒ Object
74 75 76 77 78 |
# File 'lib/smooth_operator/operator.rb', line 74 def build_url(relative_path) slash = '/' if relative_path.present? extention = '.json' [endpoint, table_name, slash, relative_path.to_s, extention].join end |
#delete(relative_path, options = {}) ⇒ Object
41 42 43 |
# File 'lib/smooth_operator/operator.rb', line 41 def delete(relative_path, = {}) make_the_call(:delete, relative_path, SmoothOperator::ProtocolHandler.()) end |
#get(relative_path, options = {}) ⇒ Object
29 30 31 |
# File 'lib/smooth_operator/operator.rb', line 29 def get(relative_path, = {}) make_the_call(:get, relative_path, SmoothOperator::ProtocolHandler.()) end |
#make_the_call(http_verb, relative_path, options = {}) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/smooth_operator/operator.rb', line 67 def make_the_call(http_verb, relative_path, = {}) url = build_url(relative_path) = () response = SmoothOperator::ProtocolHandler.send(http_verb, url, ) response end |
#post(relative_path, options = {}) ⇒ Object
33 34 35 |
# File 'lib/smooth_operator/operator.rb', line 33 def post(relative_path, = {}) make_the_call(:post, relative_path, SmoothOperator::ProtocolHandler.()) end |
#put(relative_path, options = {}) ⇒ Object
37 38 39 |
# File 'lib/smooth_operator/operator.rb', line 37 def put(relative_path, = {}) make_the_call(:put, relative_path, SmoothOperator::ProtocolHandler.()) end |
#safe_delete(relative_path, options = {}) ⇒ Object
58 59 60 |
# File 'lib/smooth_operator/operator.rb', line 58 def safe_delete(relative_path, = {}) safe_response(delete(relative_path, )) rescue nil end |
#safe_get(relative_path, options = {}) ⇒ Object
46 47 48 |
# File 'lib/smooth_operator/operator.rb', line 46 def safe_get(relative_path, = {}) safe_response(get(relative_path, )) rescue nil end |
#safe_post(relative_path, options = {}) ⇒ Object
50 51 52 |
# File 'lib/smooth_operator/operator.rb', line 50 def safe_post(relative_path, = {}) safe_response(post(relative_path, )) rescue nil end |
#safe_put(relative_path, options = {}) ⇒ Object
54 55 56 |
# File 'lib/smooth_operator/operator.rb', line 54 def safe_put(relative_path, = {}) safe_response(put(relative_path, )) rescue nil end |
#safe_response(response) ⇒ Object
62 63 64 |
# File 'lib/smooth_operator/operator.rb', line 62 def safe_response(response) successful_response?(response) ? response.parsed_response : nil end |
#set_basic_auth_credentials(options) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/smooth_operator/operator.rb', line 80 def set_basic_auth_credentials() if endpoint_user.present? || endpoint_pass.present? && !.include?(:basic_auth) .merge({ basic_auth: { username: endpoint_user, password: endpoint_pass } }) else end end |
#successful_response?(response) ⇒ Boolean
93 94 95 |
# File 'lib/smooth_operator/operator.rb', line 93 def successful_response?(response) HTTP_SUCCESS_CODES.include?(response.code) || response.blank? end |