Class: Springpad::API

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

Overview

Public: The best way to communicate with the Springpad API.

Examples

api = Springpad::API.new
api.notes
# => [..., ..., ...] # Your notes on Springpad

Instance Method Summary collapse

Constructor Details

#initializeAPI

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.expand_path("~/.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, options={})
  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, options={})
  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, 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

#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, options={})
  url  = generate(route, options)
  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_shardObject

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

#headersObject

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