springpad 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -21,8 +21,19 @@ data:
21
21
  ## Usage
22
22
 
23
23
  $ gem install springpad
24
+
25
+ Listing notes/tasks:
26
+
24
27
  $ springpad list note
25
28
  $ springpad list task
29
+
30
+ Adding a new note or task:
31
+
32
+ $ springpad add note
33
+ $ (edit with your editor)
34
+
35
+ Help about the commands:
36
+
26
37
  $ springpad --help
27
38
 
28
39
  ## Running the tests
data/bin/springpad CHANGED
@@ -26,13 +26,19 @@ command :list do |c|
26
26
  end
27
27
 
28
28
  command :add do |c|
29
- c.syntax = 'springpad add type [options]'
30
- c.summary = 'Adds an element of a certain type.'
31
- c.description = ''
32
- c.example 'description', 'command example'
33
- c.option '--some-switch', 'Some switch that does something'
29
+ c.syntax = 'springpad add TYPE [options]'
30
+ c.summary = 'Adds an element of a certain TYPE.'
31
+ c.description = 'Creates a new element of TYPE with custom properties [options]. $EDITOR will be used to create the element. The first line of the element will be its name, the other lines will vary accordying the element type.'
32
+ c.example 'Create a new private note', 'springpad add note --private'
33
+ c.option '--private', 'Make the new block private'
34
34
  c.action do |args, options|
35
- # Do something or c.when_called Springpad::Commands::Add
35
+ type = args.first
36
+ unless type
37
+ warn "A TYPE must be specified. To know about available types, run:"
38
+ warn " $ springpad types"
39
+ abort
40
+ end
41
+ Springpad.add(type, options)
36
42
  end
37
43
  end
38
44
 
data/lib/springpad.rb CHANGED
@@ -23,7 +23,18 @@ module Springpad
23
23
  end
24
24
  end
25
25
 
26
- def self.add
26
+ def self.add(type, options)
27
+ cli = Springpad::CLI.new
28
+ options = {}
29
+ options.merge({:public => false}) if options[:private]
30
+ contents = cli.edit
27
31
 
32
+ api = Springpad::API.new
33
+ case type
34
+ when "note"
35
+ p api.add_note(contents, options)
36
+ when "task"
37
+ p api.add_task(contents, options)
38
+ end
28
39
  end
29
40
  end
data/lib/springpad/api.rb CHANGED
@@ -41,7 +41,65 @@ module Springpad
41
41
  get_blocks("Task", filters)
42
42
  end
43
43
 
44
+ # Public: Adds a new note to Springpad.
45
+ #
46
+ # contents - the Array contents of the note
47
+ #
48
+ # Returns the Boolean response.
49
+ def add_note(contents, options={})
50
+ note = Blocks::Note.new(contents.first, contents[1..-1].join("\n"))
51
+ post_block(note)
52
+ end
53
+
54
+ # Public: Adds a new task to Springpad.
55
+ #
56
+ # contents - the Array contents of the task
57
+ #
58
+ # Returns the Boolean response.
59
+ def add_task(contents, options={})
60
+ task = Blocks::Task.new(contents.first, contents[1..-1].join("\n"))
61
+ post_block(task)
62
+ end
63
+
64
+ # Public: Pushes a block to Springpad.
65
+ #
66
+ # contents - the Block to be pushed
67
+ #
68
+ # Returns the Boolean response.
69
+ def post_block(block)
70
+ shard = get_shard
71
+
72
+ JSON.parse(
73
+ RestClient.post(
74
+ @url + "/users/me/commands",
75
+ block.to_params(shard),
76
+ headers.update({
77
+ "Content-Type" => "application/json"
78
+ })
79
+ )
80
+ )["success"]
81
+ end
82
+
44
83
  ## Internal methods: to be extracted
84
+ #
85
+ # Internal: Authentication headers to perform API calls.
86
+ #
87
+ # Returns the Hash headers.
88
+ def headers
89
+ {
90
+ "X-Spring-Username" => @user,
91
+ "X-Spring-Password" => @password,
92
+ "X-Spring-Api-Token" => @token,
93
+ }
94
+ end
95
+
96
+ # Internal: Gets the user shard through an API call.
97
+ #
98
+ # Returns the String shard.
99
+ def get_shard
100
+ JSON.parse(RestClient.get(@url + "/users/me", headers))["shard"]
101
+ end
102
+
45
103
 
46
104
  # Internal: Gets a collection of blocks of a given type applying some
47
105
  # filters.
@@ -1,5 +1,6 @@
1
1
  require 'highline'
2
2
  require 'stringio'
3
+ require 'securerandom'
3
4
 
4
5
  module Springpad
5
6
  module Blocks
@@ -48,6 +49,20 @@ module Springpad
48
49
  #{@text}
49
50
  RENDER
50
51
  end
52
+
53
+ # Public: Creates a query to create a Note in Springpad.
54
+ #
55
+ # shard - the String user shard.
56
+ #
57
+ # Returns the String JSON commands.
58
+ def to_params(shard)
59
+ uuid = "/UUID(#{shard}3#{SecureRandom.uuid[3..-1]})/"
60
+ [
61
+ ["create", "Note", uuid],
62
+ ["set", uuid, "name", @name],
63
+ ["set", uuid, "text", @text]
64
+ ].to_json
65
+ end
51
66
  end
52
67
  end
53
68
  end
@@ -30,7 +30,7 @@ module Springpad
30
30
  # name - the String name
31
31
  # description - the String description
32
32
  # category - the String category
33
- def initialize(name, description, category)
33
+ def initialize(name, description, category="")
34
34
  @name = name
35
35
  @description = description
36
36
  @category = category
@@ -48,6 +48,21 @@ module Springpad
48
48
  #{@description}
49
49
  RENDER
50
50
  end
51
+
52
+ # Public: Creates a query to create a Task in Springpad.
53
+ #
54
+ # shard - the String user shard.
55
+ #
56
+ # Returns the String JSON commands.
57
+ def to_params(shard)
58
+ uuid = "/UUID(#{shard}3#{SecureRandom.uuid[3..-1]})/"
59
+ [
60
+ ["create", "Note", uuid],
61
+ ["set", uuid, "name", @name],
62
+ ["set", uuid, "description", @description],
63
+ ["set", uuid, "category", @category]
64
+ ].to_json
65
+ end
51
66
  end
52
67
  end
53
68
  end
@@ -1,3 +1,3 @@
1
1
  module Springpad
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -22,5 +22,38 @@ module Springpad
22
22
  tasks.length.must_be :>, 0
23
23
  tasks.first.must_be_kind_of Blocks::Task
24
24
  end
25
+
26
+ it "posts a note" do
27
+ contents = [
28
+ "Note title",
29
+ ["Note body", "yeah"]
30
+ ]
31
+
32
+ api.expects(:post_block).with do |note|
33
+ note.name.must_equal "Note title"
34
+ note.text.must_equal "Note body\nyeah"
35
+ end.returns true
36
+
37
+ api.add_note(contents).must_equal true
38
+ end
39
+
40
+ it "posts a task" do
41
+ contents = [
42
+ "Task title",
43
+ ["Task description", "yeah"]
44
+ ]
45
+
46
+ api.expects(:post_block).with do |task|
47
+ task.name.must_equal "Task title"
48
+ task.description.must_equal "Task description\nyeah"
49
+ end.returns true
50
+
51
+ api.add_task(contents).must_equal true
52
+ end
53
+
54
+ it 'posts a block' do
55
+ note = Blocks::Note.new("Note title", "Note body")
56
+ api.post_block(note).must_equal true
57
+ end
25
58
  end
26
59
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: springpad
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-12-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &70242399978500 !ruby/object:Gem::Requirement
16
+ requirement: &70199660179120 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70242399978500
24
+ version_requirements: *70199660179120
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mocha
27
- requirement: &70242399977980 !ruby/object:Gem::Requirement
27
+ requirement: &70199660178600 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70242399977980
35
+ version_requirements: *70199660178600
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: vcr
38
- requirement: &70242399977480 !ruby/object:Gem::Requirement
38
+ requirement: &70199660178020 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70242399977480
46
+ version_requirements: *70199660178020
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: webmock
49
- requirement: &70242399976500 !ruby/object:Gem::Requirement
49
+ requirement: &70199660177100 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70242399976500
57
+ version_requirements: *70199660177100
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rest-client
60
- requirement: &70242399975780 !ruby/object:Gem::Requirement
60
+ requirement: &70199660176380 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70242399975780
68
+ version_requirements: *70199660176380
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: commander
71
- requirement: &70242399975160 !ruby/object:Gem::Requirement
71
+ requirement: &70199660175760 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *70242399975160
79
+ version_requirements: *70199660175760
80
80
  description: Command-line client for Springpad.
81
81
  email:
82
82
  - josep.m.bach@gmail.com