Class: Emvoicer::Services::Base
- Inherits:
-
Object
- Object
- Emvoicer::Services::Base
- Defined in:
- lib/emvoicer/services/base.rb
Direct Known Subclasses
Class Method Summary collapse
- .extract_client_node(source) ⇒ Object
- .extract_invoice_nodes(source) ⇒ Object
- .parse_client(node) ⇒ Object
- .parse_invoice(node) ⇒ Object
Instance Method Summary collapse
- #client(id, callback) ⇒ Object
- #client_url(id) ⇒ Object
- #generate_client(node) ⇒ Object
- #generate_invoice(node) ⇒ Object
- #handle_client_source(source) ⇒ Object
- #handle_invoices_source(source) ⇒ Object
-
#headers ⇒ Object
Override methods below to comform speific API.
- #invoices(params, callback) ⇒ Object
- #invoices_url ⇒ Object
- #request(url, params, callback) ⇒ Object
- #request_headers ⇒ Object
Class Method Details
.extract_client_node(source) ⇒ Object
71 72 73 |
# File 'lib/emvoicer/services/base.rb', line 71 def self.extract_client_node(source) [] end |
.extract_invoice_nodes(source) ⇒ Object
63 64 65 |
# File 'lib/emvoicer/services/base.rb', line 63 def self.extract_invoice_nodes(source) [] end |
.parse_client(node) ⇒ Object
75 76 77 |
# File 'lib/emvoicer/services/base.rb', line 75 def self.parse_client(node) {} end |
.parse_invoice(node) ⇒ Object
67 68 69 |
# File 'lib/emvoicer/services/base.rb', line 67 def self.parse_invoice(node) {} end |
Instance Method Details
#client(id, callback) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/emvoicer/services/base.rb', line 29 def client(id, callback) request(client_url(id), {}, proc { |http| client = handle_client_source(http.response) callback.call(client) }) end |
#client_url(id) ⇒ Object
59 60 61 |
# File 'lib/emvoicer/services/base.rb', line 59 def client_url(id) "" end |
#generate_client(node) ⇒ Object
41 42 43 44 |
# File 'lib/emvoicer/services/base.rb', line 41 def generate_client(node) attributes = self.class.parse_client(node) Emvoicer::Client.new(self, attributes) end |
#generate_invoice(node) ⇒ Object
24 25 26 27 |
# File 'lib/emvoicer/services/base.rb', line 24 def generate_invoice(node) attributes = self.class.parse_invoice(node) Emvoicer::Invoice.new(self, attributes) end |
#handle_client_source(source) ⇒ Object
36 37 38 39 |
# File 'lib/emvoicer/services/base.rb', line 36 def handle_client_source(source) node = self.class.extract_client_node(source) generate_client(node) end |
#handle_invoices_source(source) ⇒ Object
19 20 21 22 |
# File 'lib/emvoicer/services/base.rb', line 19 def handle_invoices_source(source) nodes = self.class.extract_invoice_nodes(source) nodes.map { |n| generate_invoice(n) } end |
#headers ⇒ Object
Override methods below to comform speific API
51 52 53 |
# File 'lib/emvoicer/services/base.rb', line 51 def headers {} end |
#invoices(params, callback) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/emvoicer/services/base.rb', line 12 def invoices(params, callback) request(invoices_url, params, proc { |http| invoices = handle_invoices_source(http.response) callback.call(invoices) }) end |
#invoices_url ⇒ Object
55 56 57 |
# File 'lib/emvoicer/services/base.rb', line 55 def invoices_url "" end |
#request(url, params, callback) ⇒ Object
6 7 8 9 10 |
# File 'lib/emvoicer/services/base.rb', line 6 def request(url, params, callback) http = EM::HttpRequest.new(url).get(:query => params, :head => request_headers) http.callback { |http| callback.call(http) } http.errback { raise Emvoicer::RequestError } end |
#request_headers ⇒ Object
46 47 48 |
# File 'lib/emvoicer/services/base.rb', line 46 def request_headers headers end |