Module: FoldingAtHomeClient::Request

Included in:
Causes, Description, Descriptions, GPU, GPUs, Manager, Managers, Project, Projects, Team, Team, Teams, User, User, Users
Defined in:
lib/folding_at_home_client/request.rb

Constant Summary collapse

API_URL =
'https://api.foldingathome.org'.freeze
HEADERS =
{
  'Accept' => 'application/json',
}.freeze

Instance Method Summary collapse

Instance Method Details

#connection(base_url: API_URL) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/folding_at_home_client/request.rb', line 26

def connection(base_url: API_URL)
  Faraday.new(url: base_url) do |faraday|
    faraday.request :url_encoded
    faraday.adapter Faraday.default_adapter

    faraday.options.params_encoder = DoNotEncoder
  end
end

#format_response(response) ⇒ Object



35
36
37
38
# File 'lib/folding_at_home_client/request.rb', line 35

def format_response(response)
  parsed_response = JSON.parse(response.body, symbolize_names: true)
  parsed_response.is_a?(Array) ? parsed_response : [parsed_response]
end

#request(endpoint:, base_url: API_URL, format_response: true, params: {}) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/folding_at_home_client/request.rb', line 48

def request(endpoint:, base_url: API_URL, format_response: true, params: {})
  full_url = base_url + endpoint
  response = Faraday.get(full_url, params, HEADERS)

  return format_response(response) if format_response

  response
end

#request_and_instantiate_objects(endpoint:, object_class:, params: {}) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/folding_at_home_client/request.rb', line 57

def request_and_instantiate_objects(endpoint:, object_class:, params: {})
  request(endpoint:, params:).map do |hash|
    # require 'pry'
    # binding.pry
    # puts 'end of pry'
    object_class.new(**hash)
  end
end

#request_unencoded(endpoint_and_params:, base_url: API_URL, format_response: true) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/folding_at_home_client/request.rb', line 40

def request_unencoded(endpoint_and_params:, base_url: API_URL, format_response: true)
  response = connection(base_url:).get(endpoint_and_params)

  return format_response(response) if format_response

  response
end