Class: Todoable::Todoable
- Inherits:
-
Object
- Object
- Todoable::Todoable
- Defined in:
- lib/todoable.rb
Overview
Main class of the `Todoable` Web-API wrapper.
Instance Attribute Summary collapse
-
#api_base_uri ⇒ String
Base URI for the HTTP API.
Instance Method Summary collapse
-
#api_uri(path, params = nil) ⇒ String
Concatenates API paths to the base URI.
-
#authenticate(user, password) ⇒ Object
Authenticate the session against the remote server and store the authentication token for future requests.
-
#initialize(api_uri = nil) ⇒ Todoable
constructor
Create new instances of the Todoable class.
-
#lists ⇒ Object
Retrieve all the lists available for the authenticated user.
-
#new_list(name) ⇒ Object
Creates a new list.
-
#request!(method, uri, params = nil, body = nil) ⇒ Object, String
Retrieve remote data via HTTP calls and attempts to parse the response body as JSON.
Constructor Details
#initialize(api_uri = nil) ⇒ Todoable
Create new instances of the Todoable class.
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_uri ⇒ String
Base URI for the HTTP API
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
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.
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 |
#lists ⇒ Object
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
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.
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 |