class TodoableLogan

Constants

BASE_URL

Public Class Methods

new(user, password) click to toggle source
# File lib/todoable_logan.rb, line 7
def initialize(user, password)
  @user = user
  @password = password
  authenticate()
end

Public Instance Methods

create_list(name) click to toggle source
# File lib/todoable_logan.rb, line 19
def create_list(name)
  data = {
    list: {
      name: name
    }
  }
  rest("post", "lists", data)
end
create_todo_item(name, list_id) click to toggle source
# File lib/todoable_logan.rb, line 47
def create_todo_item(name, list_id)
  data = {
    item: {
      name: name
    }
  }
  rest("post", "lists/#{list_id}/items", data)
end
delete_list(list_id) click to toggle source
# File lib/todoable_logan.rb, line 41
def delete_list(list_id)
  rest("delete", "lists/#{list_id}")

  return true
end
delete_todo_item(item_id, list_id) click to toggle source
# File lib/todoable_logan.rb, line 60
def delete_todo_item(item_id, list_id)
  rest("delete", "/lists/#{list_id}/items/#{item_id}")

  return true
end
get_list(list_id) click to toggle source
# File lib/todoable_logan.rb, line 37
def get_list(list_id)
  rest("get", "lists/#{list_id}")
end
get_lists() click to toggle source
# File lib/todoable_logan.rb, line 13
def get_lists
  response = rest("get", "lists")

  return response["lists"]
end
mark_todo_item_finished(item_id, list_id) click to toggle source
# File lib/todoable_logan.rb, line 56
def mark_todo_item_finished(item_id, list_id)
  rest("put", "lists/#{list_id}/items/#{item_id}/finish")
end
update_list(list_id, name) click to toggle source
# File lib/todoable_logan.rb, line 28
def update_list(list_id, name)
  data = {
    list: {
      name: name
    }
  }
  rest("patch", "lists/#{list_id}", data)
end

Private Instance Methods

authenticate() click to toggle source
# File lib/todoable_logan.rb, line 97
def authenticate
  begin
    response = RestClient::Request.execute(
      method: :post,
      accept: :json,
      content_type: :json,
      url: "#{BASE_URL}/authenticate",
      user: @user,
      password: @password,
    )
  rescue => e
    return raise "Error authenticating. Code: #{e.response.code}. Body: #{e.response.body}"
  end
  parsed_response = JSON.parse(response)

  @token = parsed_response['token']
  @expires_at = Time.parse(parsed_response['expires_at'])
end
rest(method, endpoint, data = {}) click to toggle source
# File lib/todoable_logan.rb, line 68
def rest(method, endpoint, data = {})
  if Time.now >= @expires_at
    authenticate()
  end

  begin
    response = RestClient::Request.execute(
      headers: {
        :authorization => "Token token=\"#{@token}\""
      },
      method: method,
      accept: :json,
      content_type: :json,
      payload: data.to_json,
      url: "#{BASE_URL}/#{endpoint}",
    )
  rescue => e
    return raise "Error making API request. Code: #{e.response.code}. Body: #{e.response.body}"
  end

  begin
    body = JSON.parse(response.body)
  rescue
    body = response.body
  end

  return body
end