pipejump 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -22,4 +22,7 @@ pkg
22
22
  spec/connection.yml
23
23
  coverage
24
24
  coverage.data
25
- test*.rb
25
+ test*.rb
26
+
27
+ .bundle
28
+ bin
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile CHANGED
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/ruby
2
2
  source "http://rubygems.org"
3
3
 
4
- gem 'rspec', '1.3.0'
4
+ gem 'rspec'
5
5
  gem 'rake'
6
- gem 'jeweler'
7
- gem 'rcov'
6
+ gem 'rcov'
7
+ gem 'vcr'
8
+ gem "webmock"
data/Gemfile.lock CHANGED
@@ -1,25 +1,30 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- gemcutter (0.5.0)
5
- json_pure
6
- git (1.2.5)
7
- jeweler (1.4.0)
8
- gemcutter (>= 0.1.0)
9
- git (>= 1.2.5)
10
- rubyforge (>= 2.0.0)
11
- json_pure (1.4.6)
12
- rake (0.8.7)
13
- rcov (0.9.8)
14
- rspec (1.3.0)
15
- rubyforge (2.0.3)
16
- json_pure (>= 1.1.7)
4
+ addressable (2.2.6)
5
+ crack (0.1.8)
6
+ diff-lcs (1.1.3)
7
+ rake (0.9.2)
8
+ rcov (0.9.10)
9
+ rspec (2.6.0)
10
+ rspec-core (~> 2.6.0)
11
+ rspec-expectations (~> 2.6.0)
12
+ rspec-mocks (~> 2.6.0)
13
+ rspec-core (2.6.4)
14
+ rspec-expectations (2.6.0)
15
+ diff-lcs (~> 1.1.2)
16
+ rspec-mocks (2.6.0)
17
+ vcr (1.11.2)
18
+ webmock (1.7.5)
19
+ addressable (~> 2.2, > 2.2.5)
20
+ crack (>= 0.1.7)
17
21
 
18
22
  PLATFORMS
19
23
  ruby
20
24
 
21
25
  DEPENDENCIES
22
- jeweler
23
26
  rake
24
27
  rcov
25
- rspec (= 1.3.0)
28
+ rspec
29
+ vcr
30
+ webmock
data/Rakefile CHANGED
@@ -1,32 +1,12 @@
1
- require "rubygems"
2
- require "bundler/setup"
3
- require 'rake'
4
-
5
- begin
6
- require 'jeweler'
7
- Jeweler::Tasks.new do |gem|
8
- gem.name = "pipejump"
9
- gem.summary = %Q{Pipejump API Ruby client}
10
- gem.description = %Q{Pipejump API Ruby client}
11
- gem.email = "marcin@pipejump.com"
12
- gem.homepage = "http://github.com/pipejump/pipejump"
13
- gem.authors = ["Marcin Bunsch"]
14
- gem.add_development_dependency "bundler", ">= 0"
15
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
- end
17
- Jeweler::GemcutterTasks.new
18
- rescue LoadError
19
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
- end
21
-
22
- require 'spec/rake/spectask'
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
23
3
 
24
4
  begin
25
5
  require 'rcov/rcovtask'
26
-
6
+
27
7
  desc "Run rcov for rspec"
28
8
  Spec::Rake::SpecTask.new(:rcov) do |t|
29
- t.spec_files = FileList['spec/pipejump/**/*_spec.rb']
9
+ t.spec_files = FileList['spec/pipejump/*_spec.rb', 'spec/pipejump/resources/*_spec.rb']
30
10
  t.spec_opts = %w{--color}
31
11
  t.rcov = true
32
12
  t.rcov_opts = %w{--html --exclude osx\/objc,gems\/,spec\/}
@@ -39,13 +19,6 @@ rescue LoadError
39
19
  end
40
20
  end
41
21
 
42
- Spec::Rake::SpecTask.new(:spec) do |t|
43
- t.spec_files = FileList['spec/pipejump/*_spec.rb', 'spec/pipejump/**/*_spec.rb']
44
- t.spec_opts = %w{--color}
45
- end
46
-
47
- task :default => :spec
48
-
49
22
  require 'rake/rdoctask'
50
23
  Rake::RDocTask.new do |rdoc|
51
24
  version = File.exist?('VERSION') ? File.read('VERSION') : ""
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -1,7 +1,7 @@
1
1
  module Pipejump
2
-
2
+
3
3
  # Represents a collection Resources available in the Pipejump API
4
- #
4
+ #
5
5
  # ==== Available Collections:
6
6
  # * Clients
7
7
  # @session.clients
@@ -29,7 +29,7 @@ module Pipejump
29
29
  # @session.sources.size
30
30
  # @session.sources.each { |source| }
31
31
  class Collection
32
-
32
+
33
33
  class << self
34
34
  # Disable methods specified as arguments
35
35
  def disable(*methods) #:nodoc:
@@ -51,27 +51,27 @@ module Pipejump
51
51
  # @session.clients
52
52
  # or
53
53
  # @deal.notes
54
- #
54
+ #
55
55
  def initialize(session, resource, owner = nil)
56
56
  @session = session
57
57
  @resource = resource
58
58
  @owner = owner
59
59
  @prefix = owner ? owner.element_path : ''
60
60
  end
61
-
61
+
62
62
  # Returns a path to the collection of Resource objects
63
63
  def collection_path
64
- @prefix + '/' + @resource.collection_path.to_s
64
+ @prefix + '/' + @resource.collection_path.to_s
65
65
  end
66
66
 
67
67
  # Returns a path to a single Resource
68
68
  def element_path(id)
69
- @prefix + '/' + @resource.collection_path.to_s + '/' + id.to_s
69
+ @prefix + '/' + @resource.collection_path.to_s + '/' + id.to_s
70
70
  end
71
-
71
+
72
72
  # Returns a single Resource object, based on its _id_
73
73
  # ==== Arguments
74
- # * _id_ - id of Resource
74
+ # * _id_ - id of Resource
75
75
  def find(id)
76
76
  code, data = @session.get(element_path(id) + '.json')
77
77
  if code == 200
@@ -81,7 +81,7 @@ module Pipejump
81
81
  raise ResourceNotFound
82
82
  end
83
83
  end
84
-
84
+
85
85
  # Returns an Array of Resource objects
86
86
  def all
87
87
  code, data = @session.get(collection_path + '.json')
@@ -90,7 +90,7 @@ module Pipejump
90
90
  @resource.new(data[key].merge(:session => @session, :prefix => @prefix))
91
91
  }
92
92
  end
93
-
93
+
94
94
  # Creates and returns a Resource object
95
95
  # ==== Arguments
96
96
  # * _attrs_ - a Hash of attributes passed to the constructor of the Resource
@@ -99,11 +99,11 @@ module Pipejump
99
99
  resource.save
100
100
  resource
101
101
  end
102
-
102
+
103
103
  def inspect
104
104
  all.inspect
105
105
  end
106
-
106
+
107
107
  ['first', 'last', 'each', 'size', 'collect', 'reject'].each do |method|
108
108
  class_eval <<-STR
109
109
  def #{method}(*args, &block)
@@ -111,7 +111,7 @@ module Pipejump
111
111
  end
112
112
  STR
113
113
  end
114
-
114
+
115
115
  end
116
-
116
+
117
117
  end
@@ -1,40 +1,40 @@
1
1
  require 'uri'
2
2
  module Pipejump
3
-
3
+
4
4
  class Connection #:nodoc:
5
-
5
+
6
6
  attr_accessor :session, :endpoint
7
7
 
8
8
  def initialize(session, endpoint = nil)
9
9
  @session = session
10
- @endpoint = endpoint || 'https://api.pipejump.com'
10
+ @endpoint = endpoint || 'https://sales.futuresimple.com'
11
11
  end
12
-
12
+
13
13
  def site
14
14
  parser = URI.const_defined?(:Parser) ? URI::Parser.new : URI
15
15
  parser.parse(self.endpoint)
16
16
  end
17
-
17
+
18
18
  def post(path, data)
19
- http.post(path, data, headers)
19
+ http.post(session.version_prefix(path), data, headers)
20
20
  end
21
21
 
22
22
  def put(path, data)
23
- http.put(path, data, headers)
23
+ http.put(session.version_prefix(path), data, headers)
24
24
  end
25
25
 
26
26
  def get(path)
27
- http.get(path, headers)
27
+ http.get(session.version_prefix(path), headers)
28
28
  end
29
29
 
30
30
  def delete(path)
31
- http.delete(path, headers)
31
+ http.delete(session.version_prefix(path), headers)
32
32
  end
33
-
33
+
34
34
  def headers
35
35
  { 'X-Pipejump-Auth' => session.token.to_s }
36
36
  end
37
-
37
+
38
38
  def http
39
39
  instance ||= Net::HTTP.new(site.host, site.port)
40
40
  if @endpoint.match(/^https:/)
@@ -43,12 +43,12 @@ module Pipejump
43
43
  end
44
44
  instance
45
45
  end
46
-
46
+
47
47
  def inspect
48
48
  "#<#{self.class} endpoint: \"#{endpoint}\">"
49
49
  end
50
-
51
-
50
+
51
+
52
52
  end
53
53
 
54
54
  end
@@ -1,7 +1,7 @@
1
1
  module Pipejump
2
2
  #
3
3
  # Represents a Resource available in the Pipejump API
4
- #
4
+ #
5
5
  # The following Resources are available:
6
6
  # * Account via @session.account
7
7
  # * Client via @session.clients
@@ -10,70 +10,71 @@ module Pipejump
10
10
  # * Note via @deal.notes
11
11
  # * Reminder via @deal.reminders
12
12
  # * Source via @deal.sources
13
- #
13
+ #
14
14
  # === Retrieving Resources
15
15
  # For information on retrieving resources, please consult the Collection class
16
16
  #
17
- # === Creating a Resource
17
+ # === Creating a Resource
18
18
  # To create a resource, call the create method on the appropriate collection.
19
- #
19
+ #
20
20
  # For example, to create a client, you would do the following:
21
21
  # @client = @session.clients.create(:name => 'Google')
22
- # This will return an instance of Pipejump::Client
23
- #
22
+ # This will return an instance of Pipejump::Client
23
+ #
24
24
  # You can access the attributes of the instance via the attributes method:
25
25
  # @client.attributes # => { 'id' => 1, 'name' => 'Google' }
26
- #
26
+ #
27
27
  # Pipejump::Resource also provides shortcut accessors for these attributes, similarly to ActiveRecord:
28
28
  # @client.name = 'Yahoo'
29
29
  # @client.name # => 'Yahoo'
30
- #
30
+ #
31
31
  # If the create fails, errors are available via the errors method
32
32
  # @client = @session.clients.create(:name => '')
33
33
  # @client.errors # => {"client"=>[{"error"=>{"code"=>"E0001", "field"=>"name", "description"=>"Please enter a client name"}}]}
34
- #
34
+ #
35
35
  # For more information on errors, please consult the Pipejump API documentation
36
36
  #
37
37
  # === Updating a Resource
38
38
  # To update a resource, change the desired attributes and call the save method
39
39
  # @client.name = 'Yahoo'
40
40
  # @client.save
41
- #
41
+ #
42
42
  # The save method returns the instance on successful save.
43
43
  #
44
44
  # If the update fails, save returns false and errors are available via the errors method
45
45
  # @client.name = ''
46
46
  # @client.save # => false
47
47
  # @client.errors # => {"client"=>[{"error"=>{"code"=>"E0001", "field"=>"name", "description"=>"Please enter a client name"}}]}
48
- #
48
+ #
49
49
  # For more information on errors, please consult the Pipejump API documentation
50
- #
50
+ #
51
51
  # === Removing a resource
52
52
  # To remove a resource, call the destroy method on the Pipejump::Resource instance
53
53
  # @client.destroy # => true
54
54
  #
55
55
  class Resource
56
-
56
+
57
57
  class << self
58
58
  # Returns the pluralized name of the resource used for the collection
59
59
  def collection_path(path = nil)
60
60
  @collection_path = path if path
61
61
  @collection_path || "#{self.to_s.split('::').last.downcase}s"
62
62
  end
63
-
63
+
64
64
  # Naive implementation of belongs_to association
65
- def belongs_to(klass) #:nodoc:
65
+ def belongs_to(klass, options = {}) #:nodoc:
66
+ klass_name = options[:class_name] || klass
66
67
  class_eval <<-STR
67
68
  def #{klass}
68
69
  if @attributes['#{klass}'].is_a?(Hash)
69
- Pipejump::#{klass.to_s.capitalize}.new(@attributes['#{klass}'].merge(:session => @session))
70
+ Pipejump::#{klass_name.to_s.capitalize}.new(@attributes['#{klass}'].merge(:session => @session))
70
71
  elsif @attributes['#{klass}_id']
71
- @session.#{klass}s.find(@attributes['#{klass}_id'])
72
+ @session.#{klass_name.to_s.downcase}s.find(@attributes['#{klass}_id'])
72
73
  end
73
74
  end
74
75
  STR
75
76
  end
76
-
77
+
77
78
  # Naive implementation of has_many association
78
79
  attr_accessor :has_many_blocks #:nodoc:
79
80
  def has_many(collection, &block) #:nodoc:
@@ -87,11 +88,11 @@ module Pipejump
87
88
  collection
88
89
  end
89
90
  STR
90
-
91
+
91
92
  end
92
-
93
+
93
94
  end
94
-
95
+
95
96
  attr_accessor :attributes
96
97
  # Constructor for the Resource
97
98
  def initialize(attrs)
@@ -100,19 +101,19 @@ module Pipejump
100
101
  @attributes = {}
101
102
  load(attrs)
102
103
  end
103
-
104
+
104
105
  def id
105
106
  @attributes['id']
106
107
  end
107
-
108
+
108
109
  def load(attrs = {}) #:nodoc:
109
110
  attrs.each_pair do |key, value|
110
111
  @attributes[key.to_s] = value
111
- end
112
+ end
112
113
  end
113
-
114
+
114
115
  def method_missing(meth, *args) #:nodoc:
115
- if meth.to_s[-1].chr == '=' and @attributes[meth.to_s[0..-2]]
116
+ if meth.to_s[-1].chr == '=' and @attributes.has_key?(meth.to_s[0..-2])
116
117
  @attributes[meth.to_s[0..-2]] = args.first
117
118
  elsif @attributes.has_key?(meth.to_s)
118
119
  @attributes[meth.to_s]
@@ -120,15 +121,15 @@ module Pipejump
120
121
  super(meth, args)
121
122
  end
122
123
  end
123
-
124
+
124
125
  def klassname #:nodoc:
125
126
  self.class.to_s.split('::').last.downcase
126
127
  end
127
-
128
+
128
129
  def to_query
129
130
  @attributes.collect { |pair| pair[0] = "#{klassname}[#{pair[0]}]"; pair.join('=') }.join('&')
130
131
  end
131
-
132
+
132
133
  def save
133
134
  before_save if respond_to?(:before_save)
134
135
  @errors = {}
@@ -141,42 +142,49 @@ module Pipejump
141
142
  true
142
143
  end
143
144
  end
144
-
145
+
145
146
  def inspect
146
147
  "#<#{self.class} #{@attributes.collect { |pair| pair[1] = "\"#{pair[1]}\""; pair.join(': ') }.join(', ')}>"
147
148
  end
148
-
149
+
149
150
  def element_path
150
151
  @prefix + '/' + self.class.collection_path.to_s + '/' + id.to_s
151
152
  end
152
-
153
+
153
154
  def collection_path
154
- @prefix + '/' + self.class.collection_path.to_s
155
+ @prefix + '/' + self.class.collection_path.to_s
155
156
  end
156
-
157
+
157
158
  def create #:nodoc:
158
- @session.post(collection_path + '.json', to_query)
159
+ @session.post(collection_path + '.json', to_query)
159
160
  end
160
161
 
161
162
  def update #:nodoc:
162
- @session.put(element_path + '.json', to_query)
163
+ @session.put(element_path + '.json', to_query)
163
164
  end
164
-
165
+
165
166
  # Destroys the Resource
166
167
  def destroy
167
168
  code, data = @session.delete(element_path + '.json')
168
169
  code.to_i == 200
169
170
  end
170
-
171
+
172
+ # Reloads the resource data from the API
173
+ def reload
174
+ code, data = @session.get(element_path + '.json')
175
+ load(data[klassname])
176
+ self
177
+ end
178
+
171
179
  # Returns a Hash of errors
172
180
  def errors
173
181
  @errors ||= {}
174
182
  end
175
-
183
+
176
184
  def created?
177
185
  !!id
178
186
  end
179
-
187
+
180
188
  end
181
-
189
+
182
190
  end