Class: Springpad::API
- Inherits:
-
Object
- Object
- Springpad::API
- Defined in:
- lib/springpad/api.rb
Overview
Instance Method Summary collapse
-
#add_note(contents, options = {}) ⇒ Object
Public: Adds a new note to Springpad.
-
#add_task(contents, options = {}) ⇒ Object
Public: Adds a new task to Springpad.
-
#extract_filters(f) ⇒ Object
Internal: Extracts filters from a Hash to be encoded as a special form of query parameter.
-
#generate(route, options) ⇒ Object
Internal: Generates a route and applies some options to it as query parameters.
-
#get(route, options = {}) ⇒ Object
Internal: Performs a GET request on a generated URL using the credentials from the API instance.
-
#get_blocks(type, filters) ⇒ Object
Internal: Gets a collection of blocks of a given type applying some filters.
-
#get_shard ⇒ Object
Internal: Gets the user shard through an API call.
-
#headers ⇒ Object
Internal methods: to be extracted.
-
#initialize ⇒ API
constructor
Public: Initializes a new API instance with credentials stored in a configuration file.
-
#notes(filters = {}) ⇒ Object
Public: Gets your notes with optional filters.
-
#post_block(block) ⇒ Object
Public: Pushes a block to Springpad.
-
#tasks(filters = {}) ⇒ Object
Public: Gets your tasks with optional filters.
Constructor Details
#initialize ⇒ API
Public: Initializes a new API instance with credentials stored in a configuration file.
18 19 20 21 22 23 24 |
# File 'lib/springpad/api.rb', line 18 def initialize config = YAML.load(File.read(File.("~/.springpad"))) @user = config['user'] @password = config['password'] @token = config['token'] @url = "http://springpadit.com/api" end |
Instance Method Details
#add_note(contents, options = {}) ⇒ Object
Public: Adds a new note to Springpad.
contents - the Array contents of the note
Returns the Boolean response.
49 50 51 52 |
# File 'lib/springpad/api.rb', line 49 def add_note(contents, ={}) note = Blocks::Note.new(contents.first, contents[1..-1].join("\n")) post_block(note) end |
#add_task(contents, options = {}) ⇒ Object
Public: Adds a new task to Springpad.
contents - the Array contents of the task
Returns the Boolean response.
59 60 61 62 |
# File 'lib/springpad/api.rb', line 59 def add_task(contents, ={}) task = Blocks::Task.new(contents.first, contents[1..-1].join("\n")) post_block(task) end |
#extract_filters(f) ⇒ Object
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.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/springpad/api.rb', line 165 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 |
#generate(route, options) ⇒ Object
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.
145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/springpad/api.rb', line 145 def generate(route, ) base = @url + route return base if .empty? base += "?" opts = [] opts << "type=#{[:type]}" if [:type] opts << "text=#{[:matching]}" if [:matching] opts += extract_filters([:filters]) base += opts.join("&") base end |
#get(route, options = {}) ⇒ Object
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.
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/springpad/api.rb', line 126 def get(route, ={}) url = generate(route, ) JSON.parse( RestClient.get( url, "X-Spring-Username" => @user, "X-Spring-Password" => @password, "X-Spring-Api-Token" => @token ) ) end |
#get_blocks(type, filters) ⇒ Object
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.
111 112 113 114 115 116 117 |
# File 'lib/springpad/api.rb', line 111 def get_blocks(type, filters) json = get("/users/#{@user}/blocks", :type => type, :filters => filters) Blocks.const_get(type).process(json) end |
#get_shard ⇒ Object
Internal: Gets the user shard through an API call.
Returns the String shard.
99 100 101 |
# File 'lib/springpad/api.rb', line 99 def get_shard JSON.parse(RestClient.get(@url + "/users/me", headers))["shard"] end |
#headers ⇒ Object
Internal methods: to be extracted
Internal: Authentication headers to perform API calls.
Returns the Hash headers.
88 89 90 91 92 93 94 |
# File 'lib/springpad/api.rb', line 88 def headers { "X-Spring-Username" => @user, "X-Spring-Password" => @password, "X-Spring-Api-Token" => @token, } end |
#notes(filters = {}) ⇒ Object
Public: Gets your notes with optional filters.
filters - the Hash filters to apply to the search
Returns an Array of Blocks::Note.
31 32 33 |
# File 'lib/springpad/api.rb', line 31 def notes(filters={}) get_blocks("Note", filters) end |
#post_block(block) ⇒ Object
Public: Pushes a block to Springpad.
contents - the Block to be pushed
Returns the Boolean response.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/springpad/api.rb', line 69 def post_block(block) shard = get_shard JSON.parse( RestClient.post( @url + "/users/me/commands", block.to_params(shard), headers.update({ "Content-Type" => "application/json" }) ) )["success"] end |
#tasks(filters = {}) ⇒ Object
Public: Gets your tasks with optional filters.
filters - the Hash filters to apply to the search
Returns an Array of Blocks::Task.
40 41 42 |
# File 'lib/springpad/api.rb', line 40 def tasks(filters={}) get_blocks("Task", filters) end |