Module: SmoothOperator::Operator::ClassMethods

Defined in:
lib/smooth_operator/operator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#endpointObject



15
16
17
# File 'lib/smooth_operator/operator.rb', line 15

def endpoint
  @endpoint ||= ENV["API_ENDPOINT"]
end

#endpoint_passObject



25
26
27
# File 'lib/smooth_operator/operator.rb', line 25

def endpoint_pass
  @endpoint_pass ||= ENV["API_PASS"]
end

#endpoint_userObject



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 build_options(options)
  options = {} if options.blank?
  set_basic_auth_credentials(options)
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, options = {})
  make_the_call(:delete, relative_path, SmoothOperator::ProtocolHandler.delete_options(options))
end

#get(relative_path, options = {}) ⇒ Object



29
30
31
# File 'lib/smooth_operator/operator.rb', line 29

def get(relative_path, options = {})
  make_the_call(:get, relative_path, SmoothOperator::ProtocolHandler.get_options(options))
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, options = {})
  url = build_url(relative_path)
  options = build_options(options)
  response = SmoothOperator::ProtocolHandler.send(http_verb, url, options)
  response
end

#post(relative_path, options = {}) ⇒ Object



33
34
35
# File 'lib/smooth_operator/operator.rb', line 33

def post(relative_path, options = {})
  make_the_call(:post, relative_path, SmoothOperator::ProtocolHandler.post_options(options))
end

#put(relative_path, options = {}) ⇒ Object



37
38
39
# File 'lib/smooth_operator/operator.rb', line 37

def put(relative_path, options = {})
  make_the_call(:put, relative_path, SmoothOperator::ProtocolHandler.put_options(options))
end

#safe_delete(relative_path, options = {}) ⇒ Object



58
59
60
# File 'lib/smooth_operator/operator.rb', line 58

def safe_delete(relative_path, options = {})
  safe_response(delete(relative_path, options)) 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, options = {})
  safe_response(get(relative_path, options)) 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, options = {})
  safe_response(post(relative_path, options)) 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, options = {})
  safe_response(put(relative_path, options)) 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(options)
  if endpoint_user.present? || endpoint_pass.present? && !options.include?(:basic_auth)
    options.merge({ basic_auth: { username: endpoint_user, password: endpoint_pass } })
  else
    options
  end
end

#successful_response?(response) ⇒ Boolean

Returns:

  • (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