Class: Beanie::Api
- Inherits:
-
Object
- Object
- Beanie::Api
- Defined in:
- lib/beanie/api.rb
Direct Known Subclasses
BankAccount, BankStatement, BankStatementData, BeanieAlert, Billable, BomItem, Company, CompanyMember, ConfigType, ConfigValue, Customer, CustomerAddress, CustomerNote, Document, FixedAsset, Journal, JournalItem, NominalAccount, NominalAccountCategory, Product, ProductCategory, ProductPrice, ProductionOrder, PurchaseInvoice, PurchaseOrder, PurchaseOrderItem, SalesInvoice, SalesInvoiceItem, SalesOrder, SalesOrderItem, StockAdjustment, StockCategory, StockItem, StockLocation, StockSupplier, Supplier, SupplierAddress, SupplierNote, TaxRegistration, VatRecord, VatReturn, WorkCentre, WorkCentreGroup
Class Method Summary collapse
-
.all(opts = {}) ⇒ Object
Find all the objects.
- .build_url(opts = {}) ⇒ Object
-
.delete(data, opts = {}) ⇒ Object
Run a REST DELETE against the Beanie API.
-
.find(id) ⇒ Object
Find an object based on its ID.
-
.get(opts = {}) ⇒ Object
Run a REST GET against the Beanie API.
-
.post(data, opts = {}) ⇒ Object
Run a REST POST against the Beanie API.
-
.put(data, opts = {}) ⇒ Object
Run a REST PUT against the Beanie API.
-
.set_headers ⇒ Object
Set the default API headers.
Instance Method Summary collapse
-
#construct_path(opts = {}) ⇒ Object
Default mechanism for constructing a path.
-
#extract ⇒ Object
Populate the instance variables from the provided hash.
-
#field_name(var) ⇒ Object
Determine the field name.
-
#object_name ⇒ Object
Compute a Beanie object name from the class name.
-
#populate(data) ⇒ Object
Populate the instance variables from the provided hash.
-
#save!(opts = {}) ⇒ Object
Save the customer data.
Class Method Details
.all(opts = {}) ⇒ Object
Find all the objects
36 37 38 39 40 41 42 43 |
# File 'lib/beanie/api.rb', line 36 def self.all(opts = {}) data = get(opts) objlist = [] data[new.object_name.pluralize].each do |objdata| objlist << new.populate(objdata) end objlist end |
.build_url(opts = {}) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/beanie/api.rb', line 131 def self.build_url(opts = {}) if opts[:url] url = "#{Beanie.base_uri}#{opts[:url]}" opts.delete(:url) else url = "#{Beanie.base_uri}/#{new.construct_path(opts)}" end if opts[:id] url += "/#{opts[:id]}" opts.delete(:id) end if opts[:extension] url += "/#{opts[:extension]}" opts.delete(:extension) end url += ".json" optstr = "" opts.each_key do |key| optstr += (optstr.length == 0) ? "?" : "&" optstr += "#{key.to_s}=#{opts[key]}" end url + optstr end |
.delete(data, opts = {}) ⇒ Object
Run a REST DELETE against the Beanie API.
124 125 126 127 |
# File 'lib/beanie/api.rb', line 124 def self.delete(data, opts = {}) response = RestClient.delete(build_url(opts), headers=set_headers) JSON.parse(response.body) end |
.find(id) ⇒ Object
Find an object based on its ID
47 48 49 50 51 |
# File 'lib/beanie/api.rb', line 47 def self.find(id) data = get(:id => id) obj = new obj.populate(data[obj.object_name]) end |
.get(opts = {}) ⇒ Object
Run a REST GET against the Beanie API.
99 100 101 102 |
# File 'lib/beanie/api.rb', line 99 def self.get(opts = {}) response = RestClient.get(build_url(opts), headers=set_headers) JSON.parse(response.body) end |
.post(data, opts = {}) ⇒ Object
Run a REST POST against the Beanie API.
117 118 119 120 |
# File 'lib/beanie/api.rb', line 117 def self.post(data, opts = {}) response = RestClient.post(build_url(opts), data.to_json, headers=set_headers) JSON.parse(response.body) end |
.put(data, opts = {}) ⇒ Object
Run a REST PUT against the Beanie API.
106 107 108 109 110 111 112 113 |
# File 'lib/beanie/api.rb', line 106 def self.put(data, opts = {}) response = RestClient.put(build_url(opts), data, headers=set_headers) if response.body and response.body.length > 0 JSON.parse(response.body) else "" end end |
Instance Method Details
#construct_path(opts = {}) ⇒ Object
Default mechanism for constructing a path
167 168 169 |
# File 'lib/beanie/api.rb', line 167 def construct_path(opts = {}) object_name.pluralize end |
#extract ⇒ Object
Populate the instance variables from the provided hash.
83 84 85 86 87 88 89 |
# File 'lib/beanie/api.rb', line 83 def extract data = {} self.instance_variables.each do |var| data[field_name(var)] = self.instance_variable_get(var) end data end |
#field_name(var) ⇒ Object
Determine the field name
93 94 95 |
# File 'lib/beanie/api.rb', line 93 def field_name(var) var.to_s.gsub(/^@/, '') end |
#object_name ⇒ Object
Compute a Beanie object name from the class name.
173 174 175 176 177 178 179 180 |
# File 'lib/beanie/api.rb', line 173 def object_name object_name = self.class.name. gsub(/.*::/, ''). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end |
#populate(data) ⇒ Object
Populate the instance variables from the provided hash.
74 75 76 77 78 79 |
# File 'lib/beanie/api.rb', line 74 def populate(data) self.instance_variables.each do |var| self.instance_variable_set(var, data[field_name(var)]) end self end |
#save!(opts = {}) ⇒ Object
Save the customer data
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/beanie/api.rb', line 55 def save!(opts = {}) data = extract() data.delete("id") objdata = {object_name => data} if @id # # Needs an update... opts[:id] = @id response = self.class.put(objdata, opts) else # # Needs a create! response = self.class.post(objdata, opts) end self.populate(response[self.object_name]) end |