Browse files

Process springpad notes

  • Loading branch information...
1 parent 62348b2 commit a147934b8867127e0fd289d1dfe84c9d1503c282 @txus committed Dec 6, 2011
Showing with 179 additions and 0 deletions.
  1. +1 −0 lib/springpad.rb
  2. +100 −0 lib/springpad/api.rb
  3. +36 −0 lib/springpad/blocks.rb
  4. +6 −0 test/springpad/api_test.rb
  5. +36 −0 test/springpad/blocks_test.rb
View
1 lib/springpad.rb
@@ -1,5 +1,6 @@
require_relative "springpad/version"
require_relative "springpad/api"
+require_relative "springpad/blocks"
module Springpad
end
View
100 lib/springpad/api.rb
@@ -1,6 +1,17 @@
+require 'rest-client'
require 'yaml'
+require 'json'
+require 'cgi'
module Springpad
+ # Public: The best way to communicate with the Springpad API.
+ #
+ # Examples
+ #
+ # api = Springpad::API.new
+ # api.notes
+ # # => [..., ..., ...] # Your notes on Springpad
+ #
class API
# Public: Initializes a new API instance with credentials stored in a
# configuration file.
@@ -9,6 +20,95 @@ def initialize
@user = config['user']
@password = config['password']
@token = config['token']
+ @url = "http://springpadit.com/api"
+ end
+
+ # Public: Gets your notes with optional filters.
+ #
+ # filters - the Hash filters to apply to the search
+ #
+ # Returns an Array of Blocks::Note.
+ def notes(filters={})
+ get_blocks("Note", filters)
+ end
+
+ ## Internal methods: to be extracted
+
+ # Internal: Gets a collection of blocks of a given type applying some
+ # filters.
+ #
+ # type - the String type of block
+ # filters - the hash filters to apply to the search
+ #
+ # Returns an Array of Blocks::Block.
+ def get_blocks(type, filters)
+ json = get("/users/#{@user}/blocks",
+ :type => type,
+ :filters => filters)
+ Blocks.const_get(type).process(json)
+ end
+
+ # Internal: Performs a GET request on a generated URL using the credentials
+ # from the API instance.
+ #
+ # route - the String URI of the API resource
+ # options - a Hash of options
+ #
+ # Returns the Hash parsed JSON response.
+ def get(route, options={})
+ url = generate(route, options)
+ JSON.parse(
+ RestClient.get(
+ url,
+ "X-Spring-Username" => @user,
+ "X-Spring-Password" => @password,
+ "X-Spring-Api-Token" => @token
+ )
+ )
+ end
+
+ # Internal: Generates a route and applies some options to it as query
+ # parameters.
+ #
+ # route - the String URI of the API resource
+ # options - a Hash of options to narrow down the search
+ #
+ # Returns the String generated route.
+ def generate(route, options)
+ base = @url + route
+ return base if options.empty?
+ base += "?"
+ opts = []
+
+ opts << "type=#{options[:type]}" if options[:type]
+ opts << "text=#{options[:matching]}" if options[:matching]
+ opts += extract_filters(options[:filters])
+
+ base += opts.join("&")
+ base
+ end
+
+ # Internal: Extracts filters from a Hash to be encoded as a special form of
+ # query parameter.
+ #
+ # f - the Hash of filters to apply
+ #
+ # Returns the single-element Array with the query parameter.
+ def extract_filters(f)
+ filts = []
+
+ filts << "type=#{f[:type]}" if f[:type]
+ filts << "userAction=#{f[:userAction]}" if f[:userAction]
+ filts << "public=#{f[:public]}" if f[:public]
+ filts << "complete=#{f[:complete]}" if f[:complete]
+ filts << "isInPast=#{f[:isInPast]}" if f[:isInPast]
+ filts << "flagged=#{f[:flagged]}" if f[:flagged]
+
+ if filts.any?
+ ["filter=#{CGI.escape(filts.join(" and "))}"]
+ else
+ []
+ end
end
end
end
View
36 lib/springpad/blocks.rb
@@ -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
View
6 test/springpad/api_test.rb
@@ -10,5 +10,11 @@ module Springpad
api.instance_variable_get(:@password).must_equal config['password']
api.instance_variable_get(:@token).must_equal config['token']
end
+
+ it "fetches the notes of the user" do
+ notes = api.notes(:public => false)
+ notes.length.must_be :>, 1
+ notes.first.must_be_kind_of Blocks::Note
+ end
end
end
View
36 test/springpad/blocks_test.rb
@@ -0,0 +1,36 @@
+require 'test_helper'
+
+module Springpad
+ describe Blocks do
+ describe '.process' do
+ let(:json) do
+ [
+ {
+ "name" => "Foo",
+ "properties" => {
+ "text" => "Foo bar baz"
+ }
+ },
+
+ {
+ "name" => "Bar",
+ "properties" => {
+ "text" => "Foo bar baz"
+ }
+ }
+ ]
+ end
+
+ it 'processes JSON notes and outputs Note objects' do
+ notes = Blocks::Note.process(json)
+ notes.length.must_equal 2
+
+ notes.first.name.must_equal "Foo"
+ notes.first.text.must_equal "Foo bar baz"
+
+ notes.last.name.must_equal "Bar"
+ notes.last.text.must_equal "Foo bar baz"
+ end
+ end
+ end
+end

0 comments on commit a147934

Please sign in to comment.