Class: Zoomify::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/zoomify/request.rb

Class Method Summary collapse

Class Method Details

.argument_error(name) ⇒ Object



13
14
15
# File 'lib/zoomify/request.rb', line 13

def argument_error(name)
  name ? ArgumentError.new("#{name} is required!") : ArgumentError.new
end

.build_object(code, msg) ⇒ Object



52
53
54
# File 'lib/zoomify/request.rb', line 52

def build_object code, msg
  OpenStruct.new(code: code, message: msg)
end

.extract_errors(response, method_name, url, args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/zoomify/request.rb', line 19

def extract_errors response, method_name, url, args
  Request.raise_error(response)
  case response.code
    when 204
      Request.extract_response_vs_object(response) {
        Request.build_object(204, 'Action Completed Successfully!')
      }
    when 404
      Request.extract_response_vs_object(response){
        Request.build_object(404, 'Content Not Found!')
      }
    when 401
      parsed_response = response.parsed_response
      parsed_response['message'].include?('expire') ? (Zoomify::Client.headers(Request.headers) && Zoomify::Client.send(method_name,url, args)) : OpenStruct.new(parsed_response)
    else
      Request.extract_response_vs_object(response){
        Request.build_object(response.code, 'Invalid Response!')
      }
  end
end

.extract_id_from_params(params) ⇒ Object



7
8
9
# File 'lib/zoomify/request.rb', line 7

def extract_id_from_params params
  params[:id].blank? ? params[:email] : params[:id]
end

.extract_id_vs_uuid_from_params(params) ⇒ Object



10
11
12
# File 'lib/zoomify/request.rb', line 10

def extract_id_vs_uuid_from_params params
  params[:id].blank? ? params[:uuid] : params[:id]
end

.extract_params(args) ⇒ Object



4
5
6
# File 'lib/zoomify/request.rb', line 4

def extract_params(args)
  args.last.is_a?(::Hash) ? args.pop.with_indifferent_access : {}
end

.extract_params_and_manage_id_error(*args) ⇒ Object



75
76
77
78
# File 'lib/zoomify/request.rb', line 75

def extract_params_and_manage_id_error *args
  params = Request.extract_params(args)
  params[:id].blank? ? (raise Request.argument_error "id") : params
end

.extract_params_and_manage_user_id_error(*args) ⇒ Object



64
65
66
67
68
# File 'lib/zoomify/request.rb', line 64

def extract_params_and_manage_user_id_error *args
  params = Request.extract_params(args)
  Request.raise_user_id_email_error params
  params
end

.extract_response_vs_object(response) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/zoomify/request.rb', line 55

def extract_response_vs_object response
  parsed_response = response.parsed_response
  unless parsed_response.blank?
    hash = parsed_response.kind_of?(Hash) ? parsed_response : JSON.parse(parsed_response)
    OpenStruct.new(hash)
  else
    yield
  end
end

.headersObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/zoomify/request.rb', line 39

def headers
  exp = (Time.now.to_i + (4*3600))
  iss_payload = {
      iss: Zoomify.api_key,
      exp: exp
  }
  token = JWT.encode iss_payload, Zoomify.api_secret, 'HS256'
  {
      'Accept' => 'application/json',
      'Content-Type' => 'application/json',
      'Authorization' => "Bearer #{token}"
  }
end

.raise_error(response) ⇒ Object



16
17
18
# File 'lib/zoomify/request.rb', line 16

def raise_error(response)
  response["error"] ? raise(Error.new(response["error"]["message"])) : response
end

.raise_from_to_error(params) ⇒ Object



79
80
81
# File 'lib/zoomify/request.rb', line 79

def raise_from_to_error params
  (raise Request.argument_error "from and to") if (params[:from].blank? || params[:to].blank?)
end

.raise_meeting_id_error(params) ⇒ Object



72
73
74
# File 'lib/zoomify/request.rb', line 72

def raise_meeting_id_error params
  (raise Request.argument_error "Meeting id or uuid") if params[:id].blank? && params[:uuid].blank?
end

.raise_user_id_email_error(params) ⇒ Object



69
70
71
# File 'lib/zoomify/request.rb', line 69

def raise_user_id_email_error params
  (raise Request.argument_error "User's id or email") if params[:id].blank? && params[:email].blank?
end

.retrieve_params_and_manage_from_to_error(*args) ⇒ Object



82
83
84
85
86
# File 'lib/zoomify/request.rb', line 82

def retrieve_params_and_manage_from_to_error *args
  params = Request.extract_params args
  Request.raise_from_to_error params
  params
end