loco_bill 0.0.2 → 0.0.4

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/README.rdoc ADDED
@@ -0,0 +1,28 @@
1
+ LocoBill
2
+ --------
3
+
4
+ An interface for interacting with Bill.com's API.
5
+
6
+ Requires Nokogiri.
7
+
8
+ Create an initializer (or what have you) to set the following:
9
+ ==============================================================
10
+
11
+ LocoBill::Configuration.environment = :production # or :sandbox
12
+ LocoBill::Configuration.application_key = "aaa"
13
+ LocoBill::Configuration.org_id = "bbb"
14
+ LocoBill::Configuration.username = "xxx"
15
+ LocoBill::Configuration.password = "yyy"
16
+
17
+ Use like so:
18
+ ============
19
+
20
+ vendor = LocoBill.create_vendor :vendor => {:name => 'Test'} # will auto-login for you and store the session ID, if you're not already logged in
21
+ vendor.result.inspect => #<LocoBill::RequestResult transactionId="Transaction1", status="OK", id="xxy">
22
+
23
+ LocoBill.create_bill :bill => {
24
+ :billLineItems => [
25
+ {:billLineItem => {:amount => 11}},
26
+ {:billLineItem => {:amount => 32}}
27
+ ]
28
+ }
@@ -0,0 +1,42 @@
1
+ module LocoBill
2
+ module Configuration
3
+ API_VERSION = "1.0" # :nodoc:
4
+
5
+ class << self
6
+ attr_accessor :session_id, :transaction_id
7
+ attr_writer :application_key, :username, :password, :org_id
8
+ end
9
+
10
+ def self.expectant_reader(*attributes) # :nodoc:
11
+ attributes.each do |attribute|
12
+ (class << self; self; end).send(:define_method, attribute) do
13
+ attribute_value = instance_variable_get("@#{attribute}")
14
+ raise "#{attribute} needs to be set" unless attribute_value
15
+ attribute_value
16
+ end
17
+ end
18
+ end
19
+ expectant_reader :environment, :application_key, :username, :password, :org_id
20
+
21
+ def self.environment=(env)
22
+ unless [:sandbox, :production].include?(env)
23
+ raise ArgumentError, "#{env.inspect} is not a valid environment"
24
+ end
25
+ @environment = env
26
+ end
27
+
28
+ def self.api_endpoint
29
+ "/crudApi"
30
+ end
31
+
32
+ def self.server # :nodoc:
33
+ case environment
34
+ when :production
35
+ "api.bill.com"
36
+ when :sandbox
37
+ "api-test.cashview.com"
38
+ end
39
+ end
40
+
41
+ end
42
+ end
@@ -1,5 +1,5 @@
1
1
  module LocoBill
2
2
  def self.login
3
- Request.new(:login, :username => BILL_API_USERNAME, :password => BILL_API_PASSWORD, :orgID => BILL_API_ORG_ID)
3
+ Request.new(:login, :username => LocoBill::Configuration.username, :password => LocoBill::Configuration.password, :orgID => LocoBill::Configuration.org_id)
4
4
  end
5
5
  end
@@ -1,31 +1,32 @@
1
1
  module LocoBill
2
2
  class Request
3
- attr_accessor :result
4
- @@api_domain = 'api.bill.com'
5
- @@api_endpoint = "/crudApi"
6
- @@session_id = nil
7
- @@transaction_id = 0
3
+ attr_reader :result
8
4
  NON_OPERATION_WRAPPED_ACTIONS = [:login, :logout, :getorglist]
9
5
  INLINE_PARAMS_ACTIONS = {:get_list => :object}
10
6
 
11
7
  def initialize(action, params={})
12
8
  @action = action.to_sym
13
- # auto-login & grab session id if we don't already have one.
14
- if @@session_id.nil? && @action != :login
15
- @@session_id = LocoBill.login.result.sessionId
16
- end
9
+ auto_login
10
+
17
11
  @params = params
18
12
  @result = process_response(api(build_request_xml))
19
13
  end
20
14
 
15
+ # auto-login & grab session id if we don't already have one.
16
+ def auto_login
17
+ if LocoBill::Configuration.session_id.nil? && @action != :login
18
+ LocoBill::Configuration.session_id = LocoBill.login.result.sessionId
19
+ end
20
+ end
21
+
21
22
  def build_request_xml
22
23
  operation_wrapped = !NON_OPERATION_WRAPPED_ACTIONS.include?(@action)
23
24
 
24
25
  Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
25
- xml.request(:version => '1.0', :applicationkey => BILL_API_APPLICATION_KEY) {
26
+ xml.request(:version => LocoBill::Configuration::API_VERSION, :applicationkey => LocoBill::Configuration.application_key) {
26
27
  if operation_wrapped
27
28
  # gotta wrap most of the api calls in an "operation" block with a transaction id & session id
28
- xml.operation(:transactionId => transaction_id, :sessionId => @@session_id) { action_xml(xml) }
29
+ xml.operation(:transactionId => LocoBill::Configuration.transaction_id, :sessionId => LocoBill::Configuration.session_id) { action_xml(xml) }
29
30
  else
30
31
  action_xml(xml)
31
32
  end
@@ -46,27 +47,37 @@ module LocoBill
46
47
  # BillApi.create_vendor :vendor => {:name => 'Test'}
47
48
  def params_xml(xml, params)
48
49
  params.each do |k, v|
49
- if v.is_a?(Hash)
50
- xml.send(k) { params_xml(xml, v) }
51
- else
52
- xml.send(k) { xml.text(v) }
53
- end
50
+ xml.send(k) {
51
+ if v.is_a?(Hash)
52
+ params_xml(xml, v)
53
+ elsif v.is_a?(Array)
54
+ v.each do |hash|
55
+ params_xml(xml, hash)
56
+ end
57
+ else
58
+ if v.is_a?(Time) || v.is_a?(Date)
59
+ v = v.to_time.strftime("%m/%d/%Y")
60
+ end
61
+
62
+ xml.text(v)
63
+ end
64
+ }
54
65
  end
55
66
  end
56
67
 
57
68
  # generate a transaction id for operation requests
58
69
  def transaction_id
59
- @@transaction_id += 1
60
- "Transaction#{@@transaction_id}"
70
+ ::Configuration.transaction_id += 1
71
+ "Transaction#{::Configuration.transaction_id}"
61
72
  end
62
73
 
63
74
  def api(xml_body)
64
75
  # puts "XML BODY: #{xml_body}"
65
- https = Net::HTTP.new(@@api_domain, 443)
76
+ https = Net::HTTP.new(LocoBill::Configuration.server, 443)
66
77
  https.use_ssl = true
67
78
  https.read_timeout=5 # 5 second timeout
68
79
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
69
- req = Net::HTTP::Post.new(@@api_endpoint)
80
+ req = Net::HTTP::Post.new(LocoBill::Configuration.api_endpoint)
70
81
  req.body = "request=#{req.send(:urlencode, xml_body)}" # prefixing the request with "request=" showed up in the docs absolutely nowhere. thanks, bill.com.
71
82
  req.content_type = 'application/x-www-form-urlencoded'
72
83
 
data/lib/loco_bill.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'nokogiri'
2
2
  require 'ostruct'
3
+ require 'loco_bill/configuration'
3
4
  require 'loco_bill/request'
4
5
  require 'loco_bill/login'
5
6
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - chris mcc
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-14 00:00:00 -04:00
17
+ date: 2010-06-15 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -41,10 +41,11 @@ extensions: []
41
41
  extra_rdoc_files: []
42
42
 
43
43
  files:
44
+ - lib/loco_bill/configuration.rb
44
45
  - lib/loco_bill/login.rb
45
46
  - lib/loco_bill/request.rb
46
47
  - lib/loco_bill.rb
47
- - README
48
+ - README.rdoc
48
49
  - init.rb
49
50
  has_rdoc: true
50
51
  homepage: http://github.com/DigitalAdvisor/loco_bill
data/README DELETED
@@ -1,15 +0,0 @@
1
- An interface for interacting with Bill.com's API. Expects the following constants to be set (in environment.rb or an initializer or what have you):
2
-
3
- BILL_API_APPLICATION_KEY
4
- BILL_API_USERNAME
5
- BILL_API_PASSWORD
6
- BILL_API_ORG_ID
7
-
8
- Requires Nokogiri.
9
-
10
- Use like so:
11
-
12
- login = LocoBill.login
13
- login.result.inspect => #<LocoBill::RequestResult status="OK", sessionId="xxx", orgID="yyy">
14
- vendor = BillApi.create_vendor :vendor => {:name => 'Test'} # will auto-login for you, if you're not already logged in
15
- vendor.result.inspect => #<LocoBill::RequestResult transactionId="Transaction1", status="OK", id="xxy">