Module: NeedForType::API

Included in:
States::End, States::SubmitScore
Defined in:
lib/need_for_type/api.rb

Constant Summary collapse

@@api_url =
'need-for-type.herokuapp.com'

Instance Method Summary collapse

Instance Method Details

#get_scores(text_id) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/need_for_type/api.rb', line 7

def get_scores(text_id)
  url = "https://#{@@api_url}/v1/scores?text_id=#{text_id}"

  begin
    response = HTTParty.get(url)
    { scores: JSON.parse(response.body) }
  rescue HTTParty::Error, StandardError
    { error: "Ups! Something went wrong." }
  end
end

#post_score(username, text_id, stats) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/need_for_type/api.rb', line 18

def post_score(username, text_id, stats)
  url = "https://#{@@api_url}/v1/scores"
  headers = { 'Content-Type' => 'application/json' }
  body = { score: { username: username,
                    text_id: text_id,
                    wpm: stats[:wpm],
                    time: stats[:total_time], 
                    accuracy: stats[:accuracy] } }
  begin
    HTTParty.post(url, headers: headers, body: body.to_json)
  rescue HTTParty::Error, StandardError
    # Nothing is done
  end
end