Class: Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_1, input_2 = nil, input_3 = nil, input_4 = nil) ⇒ Client

Returns a new instance of Client.



83
84
85
86
87
88
89
# File 'lib/ft_42.rb', line 83

def initialize(input_1, input_2 = nil, input_3 = nil, input_4 = nil)
  @input_1   = input_1
  @input_2   = input_2
  @input_3   = input_3
  @input_4   = input_4
  @token     = Token.new.token
end

Instance Attribute Details

#input_1Object (readonly)

Returns the value of attribute input_1.



81
82
83
# File 'lib/ft_42.rb', line 81

def input_1
  @input_1
end

#input_2Object (readonly)

Returns the value of attribute input_2.



81
82
83
# File 'lib/ft_42.rb', line 81

def input_2
  @input_2
end

#input_3Object (readonly)

Returns the value of attribute input_3.



81
82
83
# File 'lib/ft_42.rb', line 81

def input_3
  @input_3
end

#input_4Object (readonly)

Returns the value of attribute input_4.



81
82
83
# File 'lib/ft_42.rb', line 81

def input_4
  @input_4
end

#tokenObject (readonly)

Returns the value of attribute token.



81
82
83
# File 'lib/ft_42.rb', line 81

def token
  @token
end

Instance Method Details

#campusObject



103
104
105
# File 'lib/ft_42.rb', line 103

def campus
  token.get("/v2/campus/#{input_2}").parsed
end

#projectObject



99
100
101
# File 'lib/ft_42.rb', line 99

def project
  token.get("/v2/projects/#{input_1}").parsed
end

#project_usersObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ft_42.rb', line 107

def project_users
  user_projects = []
  i = 1
  loop do
    begin
      tries ||= 3
      if input_3
        response = token.get("/v2/projects_users?filter[campus]=#{campus['id']}&filter[project_id]=#{project['id']}&range[created_at]=#{after},#{before}", params: { page: i, per_page: 100 }).parsed
      else
        response = token.get("/v2/projects_users?filter[campus]=#{campus['id']}&filter[project_id]=#{project['id']}", params: { page: i, per_page: 100 }).parsed
      end
    rescue
      puts "Something went wrong..."
      puts "REFRESHING API TOKEN... wait 8 sec"
      sleep 8
      client = OAuth2::Client.new(ENV.fetch("UID42"), ENV.fetch("SECRET42"), site: ENV.fetch("API42"))
      token  = client.client_credentials.get_token
      puts "Retrying request..."
      retry unless (tries -= 1).zero?
    else
      break if response.empty?
      user_projects << response
      i += 1
    end
  end
  user_projects
end

#userObject



91
92
93
# File 'lib/ft_42.rb', line 91

def user
  token.get("/v2/users/#{input_1}", params: { per_page: 100 }).parsed
end

#user_sessionsObject



95
96
97
# File 'lib/ft_42.rb', line 95

def user_sessions
  token.get("/v2/users/#{input_1}/locations?range[end_at]=#{time_ago},#{right_now}", params: { per_page: 100 }).parsed
end