class SomTimer::Service

Public Class Methods

new(path) click to toggle source
# File lib/som_timer/service.rb, line 2
def initialize(path)
  @path = path
end

Public Instance Methods

create_rest(mood_rating_1, mood_rating_2, content_selected, focus_interval, rest_interval) click to toggle source
# File lib/som_timer/service.rb, line 26
def create_rest(mood_rating_1, mood_rating_2, content_selected, focus_interval, rest_interval)
  to_json_post(mood_rating_1, mood_rating_2, content_selected, focus_interval, rest_interval)
end
exercises() click to toggle source
# File lib/som_timer/service.rb, line 10
def exercises
  to_json("#{@path}")
end
rand_exercise(duration, category) click to toggle source
# File lib/som_timer/service.rb, line 14
def rand_exercise(duration, category)
  to_json("#{@path}?duration=#{duration}&category=#{category}")
end
rests() click to toggle source
# File lib/som_timer/service.rb, line 22
def rests
  to_json("#{@path}")
end
timer() click to toggle source
# File lib/som_timer/service.rb, line 6
def timer
  to_json("#{@path}")
end
update_timer(work_interval, rest_interval, sound) click to toggle source
# File lib/som_timer/service.rb, line 18
def update_timer(work_interval, rest_interval, sound)
  to_json_put(work_interval, rest_interval, sound)
end

Private Instance Methods

conn() click to toggle source
# File lib/som_timer/service.rb, line 32
def conn
  Faraday.new("https://som-timer-be.herokuapp.com/api/")
end
to_json(url) click to toggle source
# File lib/som_timer/service.rb, line 36
def to_json(url)
  response = conn.get(url)
  JSON.parse(response.body, symbolize_names: true)
end
to_json_post(mood_rating_1, mood_rating_2, content_selected, focus_interval, rest_interval) click to toggle source
# File lib/som_timer/service.rb, line 50
def to_json_post(mood_rating_1, mood_rating_2, content_selected, focus_interval, rest_interval)
  response = conn.post do |req|
    req.url "https://som-timer-be.herokuapp.com/api/#{@path}"
    req.headers['Content-Type'] = 'application/json'
    req.body = { "mood_rating_1": "#{mood_rating_1}",
                 "mood_rating_2": "#{mood_rating_2}",
                 "content_selected": "#{content_selected}",
                 "focus_interval": "#{focus_interval}",
                 "rest_interval": "#{rest_interval}" }.to_json
  end
  JSON.parse(response.body, symbolize_names: true)
end
to_json_put(work_interval, rest_interval, sound) click to toggle source
# File lib/som_timer/service.rb, line 41
def to_json_put(work_interval, rest_interval, sound)
  response = conn.put do |req|
    req.url "https://som-timer-be.herokuapp.com/api/#{@path}"
    req.headers['Content-Type'] = 'application/json'
    req.body = { "work_interval": "#{work_interval}", "rest_interval": "#{rest_interval}", "sound": "#{sound}" }.to_json
  end
  JSON.parse(response.body, symbolize_names: true)
end