atech-billy 1.0.1 → 1.0.2

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/lib/billy.rb CHANGED
@@ -5,12 +5,14 @@ require 'json'
5
5
  require 'basic_ssl'
6
6
 
7
7
  require 'billy/request'
8
+ require 'billy/base_model'
8
9
  require 'billy/service'
9
10
  require 'billy/signup_token'
11
+ require 'billy/invoice'
10
12
 
11
13
  module Billy
12
14
 
13
- VERSION = '1.0.1'
15
+ VERSION = '1.0.2'
14
16
 
15
17
  class << self
16
18
 
@@ -0,0 +1,14 @@
1
+ module Billy
2
+ class BaseModel
3
+ attr_reader :attributes
4
+
5
+ def initialize(attributes = {})
6
+ @attributes = attributes
7
+ end
8
+
9
+ def method_missing(param)
10
+ @attributes[param.to_s] || super
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,33 @@
1
+ module Billy
2
+ class Invoice < BaseModel
3
+
4
+ def add_line(params)
5
+ attributes = Billy::Request.request('invoices/add_line', :service => @attributes['service_id'], :invoice => @attributes['number'], :line => params)
6
+ attributes.is_a?(Hash) ? @attributes = attributes : false
7
+ end
8
+
9
+ def delete_line(id)
10
+ attributes = Billy::Request.request('invoices/delete_line', :service => @attributes['service_id'], :invoice => @attributes['number'], :line => id)
11
+ attributes.is_a?(Hash) ? @attributes = attributes : false
12
+ end
13
+
14
+ def finalise!
15
+ attributes = Billy::Request.request('invoices/finalise', :service => @attributes['service_id'], :invoice => @attributes['number'])
16
+ @attributes = attributes
17
+ true
18
+ end
19
+
20
+ def mark_as_paid(method, reference, verifier = nil)
21
+ options = {:service => @attributes['service_id'], :invoice => @attributes['number'], :payment_method => method, :payment_reference => reference}
22
+ options[:payment_verifier] = verifier if verifier
23
+ attributes = Billy::Request.request('invoices/mark_as_paid', options)
24
+ @attributes = attributes
25
+ true
26
+ end
27
+
28
+ def delete
29
+ Billy::Request.request('invoices/delete', :service => @attributes['service_id'], :invoice => @attributes['number'])
30
+ end
31
+
32
+ end
33
+ end
data/lib/billy/request.rb CHANGED
@@ -29,8 +29,7 @@ module Billy
29
29
  http_request.initialize_http_header({"User-Agent" => "BillyRubyClient/#{Billy::VERSION}"})
30
30
  http_request.add_field("X-Billy-Product", Billy.product)
31
31
  http_request.add_field("X-Billy-Key", Billy.product_key)
32
-
33
- http_request.set_form_data(@data)
32
+ http_request.content_type = 'application/json'
34
33
 
35
34
  http = Net::HTTP.new(uri.host, uri.port)
36
35
 
@@ -39,8 +38,14 @@ module Billy
39
38
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
40
39
  end
41
40
 
42
- http_result = http.request(http_request)
43
- @output = JSON.parse(http_result.body)
41
+ http_result = http.request(http_request, @data.to_json)
42
+ if http_result.body == 'true'
43
+ @output = true
44
+ elsif http_result.body == 'false'
45
+ @output = false
46
+ else
47
+ @output = JSON.parse(http_result.body)
48
+ end
44
49
  @success = case http_result
45
50
  when Net::HTTPSuccess
46
51
  true
@@ -53,7 +58,7 @@ module Billy
53
58
  raise Billy::Errors::NotFound, json['error']
54
59
  when Net::HTTPClientError
55
60
  json = JSON.parse(http_result.body)
56
- raise Billy::Errors::ValidationError, json['errors'].to_s
61
+ raise Billy::Errors::ValidationError, json.to_s
57
62
  else
58
63
  raise Billy::Errors::CommunicationError, http_result.body
59
64
  end
data/lib/billy/service.rb CHANGED
@@ -1,11 +1,5 @@
1
1
  module Billy
2
- class Service
3
-
4
- attr_reader :attributes
5
-
6
- def initialize(attributes)
7
- @attributes = attributes
8
- end
2
+ class Service < BaseModel
9
3
 
10
4
  ## Create a new login session and return a URL which can be used to log the user
11
5
  ## into the billing application
@@ -43,8 +37,16 @@ module Billy
43
37
  end
44
38
  end
45
39
 
46
- def method_missing(param)
47
- @attributes[param.to_s] || super
40
+ ## Create a new invoice for the service and return the new invoice object
41
+ def create_invoice
42
+ attributes = Billy::Request.request('invoices/create', :service => @attributes['id'])
43
+ attributes.is_a?(Hash) ? Billy::Invoice.new(attributes) : nil
44
+ end
45
+
46
+ ## Return the invoice for this service
47
+ def invoice(number)
48
+ attributes = Billy::Request.request('invoices/info', :invoice => number, :service => @attributes['id'])
49
+ attributes.is_a?(Hash) ? Billy::Invoice.new(attributes) : nil
48
50
  end
49
51
 
50
52
  def self.find(field, data)
@@ -52,5 +54,10 @@ module Billy
52
54
  attributes.is_a?(Hash) ? self.new(attributes) : nil
53
55
  end
54
56
 
57
+ def self.create(atech_identity, service)
58
+ attributes = Billy::Request.request('services/create', :service => service, :atech_identity_key => atech_identity)
59
+ atech_identity.is_a?(Hash) ? self.new(attributes) : nil
60
+ end
61
+
55
62
  end
56
63
  end
@@ -1,11 +1,5 @@
1
1
  module Billy
2
- class SignupToken
3
-
4
- attr_reader :attributes
5
-
6
- def initialize(attributes)
7
- @attributes = attributes
8
- end
2
+ class SignupToken < BaseModel
9
3
 
10
4
  def service?
11
5
  self.attributes.include?(:service)
@@ -21,9 +15,5 @@ module Billy
21
15
  attributes.is_a?(Hash) ? self.new(attributes) : nil
22
16
  end
23
17
 
24
- def method_missing(param)
25
- @attributes[param.to_s] || super
26
- end
27
-
28
18
  end
29
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: atech-billy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-31 00:00:00.000000000Z
12
+ date: 2012-06-01 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: basic_ssl
16
- requirement: &70223088107580 !ruby/object:Gem::Requirement
16
+ requirement: &70117254236000 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70223088107580
24
+ version_requirements: *70117254236000
25
25
  description:
26
26
  email: adam@atechmedia.com
27
27
  executables: []
@@ -29,6 +29,8 @@ extensions: []
29
29
  extra_rdoc_files: []
30
30
  files:
31
31
  - lib/billy.rb
32
+ - lib/billy/base_model.rb
33
+ - lib/billy/invoice.rb
32
34
  - lib/billy/request.rb
33
35
  - lib/billy/service.rb
34
36
  - lib/billy/signup_token.rb