Permalink
Browse files

Refactor blocks and add Task

  • Loading branch information...
1 parent a147934 commit 68ae61c5c787e836d3bac14b9dc6809e2f472cb7 @txus committed Dec 8, 2011
View
@@ -32,6 +32,15 @@ def notes(filters={})
get_blocks("Note", filters)
end
+ # Public: Gets your tasks with optional filters.
+ #
+ # filters - the Hash filters to apply to the search
+ #
+ # Returns an Array of Blocks::Task.
+ def tasks(filters={})
+ get_blocks("Task", filters)
+ end
+
## Internal methods: to be extracted
# Internal: Gets a collection of blocks of a given type applying some
@@ -45,6 +54,7 @@ def get_blocks(type, filters)
json = get("/users/#{@user}/blocks",
:type => type,
:filters => filters)
+
Blocks.const_get(type).process(json)
end
View
@@ -1,36 +1,7 @@
module Springpad
module Blocks
- # Public: Maps to a Springpad Note block.
- #
- # Examples
- #
- # Blocks::Note.process(json_notes)
- # # => [#<Blocks::Note>..., #<Blocks::Note...>,]
- #
- class Note
- attr_reader :name, :text
- # Public: Converts a Hash of JSON note blocks to an Array of actual Note
- # instances.
- #
- # json - the Hash JSON with the note blocks
- #
- # Returns an Array of Notes.
- def self.process(json)
- json.map do |note|
- Note.new(
- note['name'],
- note['properties']['text']
- )
- end
- end
-
- # Internal: Initializes a new Note.
- #
- # name - the String name
- # text - the String text content
- def initialize(name, text)
- @name, @text = name, text
- end
- end
end
end
+
+require_relative "blocks/note"
+require_relative "blocks/task"
@@ -0,0 +1,36 @@
+module Springpad
+ module Blocks
+ # Public: Maps to a Springpad Note block.
+ #
+ # Examples
+ #
+ # Blocks::Note.process(json_notes)
+ # # => [#<Blocks::Note>..., #<Blocks::Note...>,]
+ #
+ class Note
+ attr_reader :name, :text
+ # Public: Converts a Hash of JSON note blocks to an Array of actual Note
+ # instances.
+ #
+ # json - the Hash JSON with the note blocks
+ #
+ # Returns an Array of Notes.
+ def self.process(json)
+ json.map do |note|
+ Note.new(
+ note['name'],
+ note['properties']['text']
+ )
+ end
+ end
+
+ # Internal: Initializes a new Note.
+ #
+ # name - the String name
+ # text - the String text content
+ def initialize(name, text)
+ @name, @text = name, text
+ end
+ end
+ end
+end
@@ -0,0 +1,40 @@
+module Springpad
+ module Blocks
+ # Public: Maps to a Springpad Task block.
+ #
+ # Examples
+ #
+ # Blocks::Task.process(json_tasks)
+ # # => [#<Blocks::Task>..., #<Blocks::Task...>,]
+ #
+ class Task
+ attr_reader :name, :description, :category
+ # Public: Converts a Hash of JSON task blocks to an Array of actual Task
+ # instances.
+ #
+ # json - the Hash JSON with the task blocks
+ #
+ # Returns an Array of Tasks.
+ def self.process(json)
+ json.map do |task|
+ Task.new(
+ task['name'],
+ task['properties']['description'],
+ task['properties']['category']['name']
+ )
+ end
+ end
+
+ # Internal: Initializes a new Task.
+ #
+ # name - the String name
+ # description - the String description
+ # category - the String category
+ def initialize(name, description, category)
+ @name = name
+ @description = description
+ @category = category
+ end
+ end
+ end
+end
@@ -13,8 +13,14 @@ module Springpad
it "fetches the notes of the user" do
notes = api.notes(:public => false)
- notes.length.must_be :>, 1
+ notes.length.must_be :>, 0
notes.first.must_be_kind_of Blocks::Note
end
+
+ it "fetches the tasks of the user" do
+ tasks = api.tasks(:public => false)
+ tasks.length.must_be :>, 0
+ tasks.first.must_be_kind_of Blocks::Task
+ end
end
end
@@ -1,7 +1,7 @@
require 'test_helper'
-module Springpad
- describe Blocks do
+module Springpad::Blocks
+ describe Note do
describe '.process' do
let(:json) do
[
@@ -22,7 +22,7 @@ module Springpad
end
it 'processes JSON notes and outputs Note objects' do
- notes = Blocks::Note.process(json)
+ notes = Note.process(json)
notes.length.must_equal 2
notes.first.name.must_equal "Foo"
@@ -0,0 +1,44 @@
+require 'test_helper'
+
+module Springpad::Blocks
+ describe Task do
+ describe '.process' do
+ let(:json) do
+ [
+ {
+ "name" => "Foo",
+ "properties" => {
+ "description" => "Foo bar baz",
+ "category" => {
+ "name" => "Learning"
+ }
+ }
+ },
+
+ {
+ "name" => "Bar",
+ "properties" => {
+ "description" => "Foo bar baz",
+ "category" => {
+ "name" => "Learning"
+ }
+ }
+ }
+ ]
+ end
+
+ it 'processes JSON tasks and outputs Task objects' do
+ tasks = Task.process(json)
+ tasks.length.must_equal 2
+
+ tasks.first.name.must_equal "Foo"
+ tasks.first.description.must_equal "Foo bar baz"
+ tasks.first.category.must_equal "Learning"
+
+ tasks.last.name.must_equal "Bar"
+ tasks.last.description.must_equal "Foo bar baz"
+ tasks.last.category.must_equal "Learning"
+ end
+ end
+ end
+end

0 comments on commit 68ae61c

Please sign in to comment.