Skip to content

Commit

Permalink
Merge pull request #9 from spreemo/attachment
Browse files Browse the repository at this point in the history
Add Attachment API object support
  • Loading branch information
ataber committed Sep 24, 2015
2 parents fb9b50b + 1403801 commit 95c330d
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 6 deletions.
12 changes: 9 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ PATH
remote: .
specs:
rubill (0.1.2)
httparty
httmultiparty
json

GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.2.5)
httparty (0.13.3)
httmultiparty (0.3.16)
httparty (>= 0.7.3)
mimemagic
multipart-post
httparty (0.13.5)
json (~> 1.8)
multi_xml (>= 0.5.2)
json (1.8.2)
json (1.8.3)
mimemagic (0.3.0)
multi_xml (0.5.5)
multipart-post (2.0.0)
rake (10.3.1)
rspec (3.1.0)
rspec-core (~> 3.1.0)
Expand Down
1 change: 1 addition & 0 deletions lib/rubill.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def sandbox
require "rubill/bill"
require "rubill/bill_payment"
require "rubill/invoice"
require "rubill/attachment"
require "rubill/sent_payment"
require "rubill/sent_bill_payment"
require "rubill/received_payment"
Expand Down
17 changes: 17 additions & 0 deletions lib/rubill/attachment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'base64'

module Rubill
class Attachment < Base
def self.send_attachment(object_id, file_name, content)
Query.upload_attachment({
id: object_id,
fileName: file_name,
content: content
})
end

def self.remote_class_name
"Attachment"
end
end
end
4 changes: 4 additions & 0 deletions lib/rubill/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def self.send_payment(opts={})
execute("/RecordAPPayment.json", opts)
end

def self.upload_attachment(opts={})
execute("/UploadAttachment.json", opts)
end

def self.void_sent_payment(id)
execute("/VoidAPPayment.json", sentPayId: id)
end
Expand Down
10 changes: 8 additions & 2 deletions lib/rubill/session.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "httparty"
require "httmultiparty"
require "json"
require "singleton"

Expand Down Expand Up @@ -71,11 +71,17 @@ def _post(url, data, retries=0)
end

def self._post(url, options)
if options.key?(:fileName)
file = StringIO.new(options.delete(:content))
end

post_options = {
body: options,
headers: default_headers
headers: default_headers,
}

post_options[:file] = file if file

if self.configuration.debug
post_options[:debug_output] = $stdout
end
Expand Down
2 changes: 1 addition & 1 deletion rubill.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |s|
s.homepage = 'http://rubygems.org/gems/rubill'
s.license = 'MIT'

s.add_dependency "httparty"
s.add_dependency "httmultiparty"
s.add_dependency "json"

s.add_development_dependency "rspec"
Expand Down
26 changes: 26 additions & 0 deletions spec/attachment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "spec_helper"

module Rubill
describe Attachment do
let(:options) { { "a" => "b" } }

describe ".send_attachment" do
let(:id) { "123" }
let(:file_name) { "file.txt" }

context "with plain text" do
before do
expect(Query).to receive(:upload_attachment).with({
id: id,
fileName: file_name,
content: 'Test text'
})
end

it "should be uploaded" do
described_class.send_attachment(id, file_name, 'Test text')
end
end
end
end
end
11 changes: 11 additions & 0 deletions spec/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ module Rubill
end
end

describe ".upload_attachment" do
let(:url) { "/UploadAttachment.json" }
let(:query) { double(execute: true) }

it "creates a new Query object and executes it" do
expect(described_class).to receive(:new).with(url, options) { query }
expect(query).to receive(:execute)
described_class.upload_attachment(options)
end
end

describe ".pay_bills" do
let(:url) { "/PayBills.json" }
let(:query) { double(execute: true) }
Expand Down

0 comments on commit 95c330d

Please sign in to comment.