Module: Remotr::Respondable::ClassMethods

Defined in:
lib/remotr/respondable.rb

Instance Method Summary collapse

Instance Method Details

#applicationObject



19
20
21
# File 'lib/remotr/respondable.rb', line 19

def application
  self.name.deconstantize.underscore
end

#configObject



11
12
13
# File 'lib/remotr/respondable.rb', line 11

def config
  self.name.deconstantize.constantize.config
end

#delete(path = {}, params = {}) ⇒ Object



31
32
33
# File 'lib/remotr/respondable.rb', line 31

def delete(path = {}, params = {})
  request :delete, path, params
end

#get(path = {}, params = {}) ⇒ Object



23
24
25
# File 'lib/remotr/respondable.rb', line 23

def get(path = {}, params = {})
  request :get, path, params
end

#namespaceObject



15
16
17
# File 'lib/remotr/respondable.rb', line 15

def namespace
  self.name.demodulize.pluralize.underscore
end

#post(path = {}, params = {}, body = nil) ⇒ Object



27
28
29
# File 'lib/remotr/respondable.rb', line 27

def post(path = {}, params = {}, body = nil)
  request :post, path, params, body
end

#put(path = {}, params = {}, body = nil) ⇒ Object



35
36
37
# File 'lib/remotr/respondable.rb', line 35

def put(path = {}, params = {}, body = nil)
  request :put, path, params, body
end

#request(method, path, params = {}, body = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/remotr/respondable.rb', line 39

def request(method, path, params = {}, body = nil)
  path         = "#{config.base_path}#{path}"
  token        = Signature::Token.new application, config.api_key
  request      = Signature::Request.new method.to_s.upcase, path, params
  auth_hash    = request.sign token
  query_params = params.merge auth_hash
  url          = URI.join(config.base_uri, path).to_s

  fail ArgumentError unless %w( get post delete put update ).include? method.to_s
  httparty_response = HTTParty.send method, url, timeout: timeout_in_seconds, query: query_params, body: body, headers: { 'Accept' => 'application/json' }

  if httparty_response.code.to_i.between? 200, 299
    Operations.success :request_succeeded, object: httparty_response
  else
    Operations.failure :request_failed, object: httparty_response
  end

rescue => exception
  Operations.failure :connection_failed, object: exception
end

#respond_with(request_operation, namespace_to_use = namespace) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/remotr/respondable.rb', line 60

def respond_with(request_operation, namespace_to_use = namespace)
  return request_operation if request_operation.failure?
  httparty_response = request_operation.object
  parsed_response = httparty_response.parsed_response

  return Operations.failure(:response_is_not_parseable, object: httparty_response) unless parsed_response
  return Operations.failure(:response_missing_success_flag, object: httparty_response) unless parsed_response && parsed_response.respond_to?(:key?) && parsed_response.key?('success')

  code    = parsed_response['code'].to_s != '' ? parsed_response['code'].to_sym : :response_unsuccessful
  message = parsed_response['message'].to_s != '' ? parsed_response['message'] : code.to_s
  return Operations.failure(code, object: httparty_response, message: message) if parsed_response['success'].to_s != 'true'

  code   = parsed_response['code'].to_s != '' ? parsed_response['code'].to_sym : :request_succeeded
  object = parsed_response[namespace_to_use.to_s]
  Operations.success code, object: object

rescue JSON::ParserError
  Operations.failure :json_parsing_failed, object: httparty_response
end

#timeout_in_secondsObject



80
81
82
# File 'lib/remotr/respondable.rb', line 80

def timeout_in_seconds
  config.default_timeout
end