Class: Todoable::Todoable

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

Overview

Main class of the `Todoable` Web-API wrapper.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_uri = nil) ⇒ Todoable

Create new instances of the Todoable class.

Parameters:

  • api_uri (String, nil) (defaults to: nil)

    Base URI for the API. If `nil` is passed, the default `todoable.teachable.tech` will be used.



141
142
143
144
# File 'lib/todoable.rb', line 141

def initialize api_uri = nil
  @token = nil
  @api_base_uri = api_uri || "http://todoable.teachable.tech"
end

Instance Attribute Details

#api_base_uriString

Base URI for the HTTP API

Returns:

  • (String)

    The base URI that will be used. See #initialize



134
135
136
# File 'lib/todoable.rb', line 134

def api_base_uri
  @api_base_uri
end

Instance Method Details

#api_uri(path, params = nil) ⇒ String

Concatenates API paths to the base URI

Examples:

Interpolate parameters

todo = Todoable::Todoable.new "http://foo"
todo.api_uri("/l/:id", { :id => 'af320e'}) #=> "http://foo/l/af320e"

Parameters:

  • path (String)

    Path component of the endpoint to be concatenated to the base URI

  • params (Hash, nil) (defaults to: nil)

    Hash that contains parameters that should be replaced in the URI.

Returns:

  • (String)

    The full URI with all the provided parameters interpolated.



199
200
201
202
203
# File 'lib/todoable.rb', line 199

def api_uri(path, params = nil)
  p = path
  params.each {|k, v| p = p.gsub(/(:#{k.to_s})/, v.to_s)} if params
  URI.join(api_base_uri, p)
end

#authenticate(user, password) ⇒ Object

Authenticate the session against the remote server and store the authentication token for future requests.

This must be the first method called after instantiating the Todoable::Todoable class. All the other methods that access other endpoints will fail with the AuthError exception otherwise.

Raises:



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/todoable.rb', line 153

def authenticate user, password
  uri = api_uri(AUTH_PATH)
  request = Net::HTTP::Post.new(uri.request_uri)

  # Prepare headers for authentication & JSON
  request.initialize_http_header({})
  request.basic_auth(user, password)
  request['Accept'] = request['Content-Type'] = 'application/json'

  # Collect response
  http = Net::HTTP.new(uri.host, uri.port)
  response = http.request(request)
  raise AuthError if response.code.to_i == 401

  # Retrieve token from body
  @token = JSON.parse(response.body)["token"]
end

#listsObject

Retrieve all the lists available for the authenticated user



172
173
174
175
176
# File 'lib/todoable.rb', line 172

def lists
  request!("get", LISTS_PATH)["lists"].collect { |json_list|
    List.new(self, json_list)
  }
end

#new_list(name) ⇒ Object

Creates a new list

Parameters:

  • name (String)

    The name of the new list



181
182
183
# File 'lib/todoable.rb', line 181

def new_list name
  request!("post", LISTS_PATH, nil, {"list": {"name": name}}.to_json)
end

#request!(method, uri, params = nil, body = nil) ⇒ Object, String

Retrieve remote data via HTTP calls and attempts to parse the response body as JSON

only receive get, post, patch, put or delete. URI path. sent in the body of the request. body contains JSON data. Returns a String otherwise.

Parameters:

  • method (String)

    HTTP method intended to be used. It can

  • params (Hash, nil) (defaults to: nil)

    Parameters to be interpolated in the

  • body (String, nil) (defaults to: nil)

    Any content that is intended to be

Returns:

  • (Object, String)

    Returns an Object when the response

Raises:



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/todoable.rb', line 216

def request!(method, uri, params = nil, body = nil)
  raise NotAuthenticated unless @token != nil

  uri = api_uri(uri, params)
  # Choose method
  request = {
    "get" => Net::HTTP::Get,
    "patch" => Net::HTTP::Patch,
    "post" => Net::HTTP::Post,
    "put" => Net::HTTP::Put,
    "delete" => Net::HTTP::Delete,
  }[method].new(uri.request_uri)

  # Configure request object
  request.body = body
  request.initialize_http_header({
    'Accept' => 'application/json',
    'Content-Type' => 'application/json',
    'Authorization' => "Token token=\"#{@token}\""
  })

  # Perform request and collect response
  http = Net::HTTP.new(uri.host, uri.port)
  body = http.request(request).body

  # Automatically convert to JSON if the output is
  # convertible. The Web API unfortunately doesn't inform the
  # right Content-Type header in the response, and some endpoints
  # do not return JSON, thus the begin/rescue.
  begin
    JSON.parse(body) if body != nil && !body.empty?
  rescue JSON::ParserError
    body
  end
end