diff --git a/.gitignore b/.gitignore index 235624d..8eabada 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,6 @@ billomat*.gem # rspec failure tracking .rspec_status + +# Mac +.DS_Store \ No newline at end of file diff --git a/doc/article.md b/doc/article.md new file mode 100644 index 0000000..6c5c09c --- /dev/null +++ b/doc/article.md @@ -0,0 +1,433 @@ +# Article/Item/Position + +Documentation exists in English (1) and German (2). Both cover articles as well as user defined properties. The German version also covers tags (Schlagwörter). If you can try to follow the German documentation since it is more complete than the English version. The documentation shows the method (get, put, post, delete) and the path and query parameters beyond the base URL as well as some of the parameters the URL takes. All examples are in XML and often are escaped. To read the code you will need to unescape the examples. + +All examples here will be shown once in CURL and JSON and once using the Billomat Gem. If you replace and with the correct values you should be able to use the following curl commands. + +## List + +The per_page value defaults to 100, the page value to 1. + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/articles\?per_page\=2\&page\=1 +``` + +The returned data is a nested object, articles.article contains an array of all the returned article objects. If there is only one articles.article is an object. There is code in utils.rs to handle this case. + +``` JSON +{ + "articles": { + "article": [], + "@page": "1", + "@per_page": "2", + "@total": "12" + } +} +``` + +In ruby you have two ways to get at the list data. The first is to call the list function (page and per_page optional) and the second is to call paged_list (again with page and per_page optional). Each give back slightly different data. + +``` RUBY +articles = Billomat::Models::Article.list +articles.each do |article| + pp article.data +end +``` + +list returns a list of Billomat::Models::Article. To get at the date call the data property. + +``` RUBY +articles = Billomat::Models::Article.paged_list(page = 2, per_page = 10) +pp articles +``` + +paged_list returns a hash with two properties, data which contains the same data as list does and paging_data, a hash that has page, per_page and total as properties. + +## Filter + +You can filter by attribute, in this case the type attribute. + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/articles\?title\=shipping +``` + +Or you can filter by a user defined field by using "property\#\{article_property_id\}" as attribute. + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/articles\?property5601\=service +``` + +In ruby you can filter by calling the where method on the resource. + +``` RUBY +articles = Billomat::Models::Article.where({"title": "shipping"}) +``` + +## Create + +Lets create an article by sending the following JSON to the API. + +``` JSON +{ + "article": { + "type": "SERVICE", + "title": "Update base data", + "description": "Recollect data and update data in system.", + "sales_price": "250" + } +} +``` + +We do that by posting the data like this: + +``` BASH +curl -X POST -H 'X-BillomatApiKey: ' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"article":{"type":"SERVICE","title":"Update base data","description":"Recollect data and update data in system.","sales_price":"250"}}' https://.billomat.net/api/articles +``` + +The return data is the resulting article. The same can be achieved over in ruby like this: + +``` RUBY +article = Billomat::Models::Article.new({ + "type" => "PRODUCT", + "title" => "recard", + "description" => "replacement card", + "sales_price" => "10.95" +}) +article.save +``` + +The return data will be pushed to article.data. + +## Update + +Lets update an article. + +``` JSON +{ + "article": { + "sales_price": "9.99" + } +} +``` + +By putting the changed data to the API. + +``` BASH +curl -X PUT -H 'X-BillomatApiKey: ' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"article":{"sales_price":"9.99"}}' https://.billomat.net/api/articles/1444307 +``` + +The same can be done in ruby by instantiating an article with ID and saving. + +``` RUBY +article = Billomat::Models::Article.new('id' => 1444307, 'sales_price' => '10') +article.save +``` + +The save method is a switch that calls the update method if an id is set and create if not. + +## Delete + +You can delete an article like this: + +``` BASH +curl -X DELETE -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/articles/1444307 +``` + +Using the gem the same can be achieved by: + +``` RUBY +article = Billomat::Models::Article.new('id' => 1444301) +article.delete +``` + +## Tax Rates + +Documentation for tax rates exists in English (1) and German (2). For Germany tax rates are predefined and you probably won't have to set up your own tax rates. + +### List + +The per_page value defaults to 100, the page value to 1. + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/taxes\?per_page\=10\&page\=1 +``` + +The returned data is a nested object, taxes.tax contains an array of all the returned tax rates (0%, 7% and 19%). + +``` JSON +{ + "taxes": { + "tax": [], + "@page": "1", + "@per_page": "2", + "@total": "12" + } +} +``` + +There is no way to do this in Ruby because you are unlikely to ever need to do this. + +## User defined fields + +Additional fields can be defined for articles as well as a few other resources. Documentation exists for user defined fields in English (5) and German (6). + +### List + +The per_page value defaults to 100, the page value to 1. + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/article-property-values\?per_page\=5\&page\=1 +``` + +The returned data is a nested object, article-property-values.article-property-value contains an array of all the returned article-property-values objects. If there is only one article-property-values.article-property-value is an object. There is code in utils.rs to handle this case. + +``` JSON +{ + "article-property-values": { + "article-property-value": [], + "@page": "1", + "@per_page": "2", + "@total": "12" + } +} +``` + +In ruby you have two ways to get at the list data. The first is to call the list function (page and per_page optional) and the second ist to call paged_list (again with page and per_page optional). Each give back slightly different data. + +``` RUBY +article_property_values = Billomat::Models::ArticlePropertyValue.list +article_property_values.each do |property| + pp property.data +end +``` + +list returns a list of Billomat::Models::ArticleProperty. To get at the date call the data property. + +``` RUBY +article_property_values = Billomat::Models::ArticlePropertyValue.paged_list(page = 2, per_page = 10) +pp article_property_values +``` + +paged_list returns a hash with two properties, data which contains the same data as list does and paging_data, a hash that has page, per_page and total as properties. + +### Filter + +You can filter by attribute, in this case the article_id attribute. + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/article-property-values\?article_id\=1434499 +``` + +The same can be achieve in ruby by: + +``` RUBY +article_property_values = Billomat::Models::ArticlePropertyValue.where({"article_id": "1434499"}) +``` + +You can also filter on the article using article-property-values like described in the appropriate section above. + +### Create + +Lets create an article property by sending the following JSON to the API. + +``` JSON +{ + "article-property-value": { + "article_id": "1444406", + "article_property_id": "5601", + "value": "just some value" + } +} +``` + +We do that by posting the data like this: + +``` BASH +curl -X POST -H 'X-BillomatApiKey: ' -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{"article-property-value":{"article_id":"1444406","article_property_id":"5601","value":"just some value"}}' https://.billomat.net/api/article-property-values +``` + +The return data is the resulting article-property-value. The same can be achieved over in ruby like this: + +``` RUBY +article_property_value = Billomat::Models::ArticlePropertyValue.new({ + "article_id": "1444406", + "article_property_id": "5601", + "value": "just some other value" +}) +article_property_value.save +``` + +The return data will be pushed to article_property_value.data. + + +### Update + +Lets update an article property value. + +``` JSON +{ + "article-property-value": { + "value":"one more time" + } +} +``` + +By putting the changed data to the API. + +``` BASH +curl -X PUT -H 'X-BillomatApiKey: ' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"article-property-value":{"value":"one more time"}}' https://.billomat.net/api/article-property-values/4183225 +``` + +The same can be done in ruby by instantiating an article property value with ID and saving. + +``` RUBY +article_property_value = Billomat::Models::ArticlePropertyValue.new('id' => 4183225, 'value' => 'and done') +article_property_value.save +``` + +The save method is a switch that calls the update method if an id is set and create if not. + +### Delete + +You can delete an article property value like this: + +``` BASH +curl -X DELETE -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/article-property-values/9672513 +``` + +Using the gem the same can be achieved by: + +``` RUBY +article_property_value = Billomat::Models::ArticlePropertyValue.new('id' => 4169172) +article_property_value.delete +``` + +## Metafield + +Articles, like all resources in the API, have a field called customfield. It is a metadata field that can be set and filtered for without having to use the property or tags API. + +``` BASH +curl -X PUT -H 'X-BillomatApiKey: ' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"article":{"customfield":"first-product"}}' https://.billomat.net/api/articles/1434499/customfield +``` + +The same can be achieved in Ruby by: + +``` RUBY +article = Billomat::Models::Article.new({ + 'id' => 1434499, + 'customfield' => 'first-product' +}) +article.save +``` + +## Tags (Schlagwörter) + +Documentation for article tags exists in German (5). + +### List + +The per_page value defaults to 100, the page value to 1. + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/article-tags\?per_page\=5\&page\=1 +``` + +The returned data is a nested object, article-tags.article-tag contains an array of all the returned article-tag objects. If there is only one article-tags.article-tag is an object. There is code in utils.rs to handle this case. + +``` JSON +{ + "article-tags": { + "article-tag": [], + "@page": "1", + "@per_page": "2", + "@total": "12" + } +} +``` + +In ruby you have two ways to get at the list data. The first is to call the list function (page and per_page optional) and the second is to call paged_list (again with page and per_page optional). Each give back slightly different data. + +``` RUBY +article_tags = Billomat::Models::ArticleTag.list +article_tags.each do |tag| + pp tag.data +end +``` + +list returns a list of Billomat::Models::ArticleTag. To get at the date call the data property. + +``` RUBY +article_tags = Billomat::Models::ArticleTag.paged_list(page = 2, per_page = 10) +pp article_tags +``` + +paged_list returns a hash with two properties, data which contains the same data as list does and paging_data, a hash that has page, per_page and total as properties. + +If you want to list the article tags of an article you can do so by calling: + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/article-tags\?article_id\=1434499 +``` + +The same can be achieved over in ruby like this: + +``` RUBY +article_tag = Billomat::Models::ArticleTag.where('article_id' => 1434499) +pp article_tag +``` + +### Create + +Lets add an article tag by sending the following JSON to the API. + +``` JSON +{ + "article-tag": { + "article_id": "1434499", + "name": "imported" + } +} +``` + +We do that by posting the data like this: + +``` BASH +curl -X POST -H 'X-BillomatApiKey: ' -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{"article-tag":{"article_id":"1434499","name":"imported"}}' https://.billomat.net/api/article-tags +``` + +The return data is the resulting article-tag. The same can be achieved over in ruby like this: + +``` RUBY +article_tag = Billomat::Models::ArticleTag.new({ + "article_id" => "7299528", + "name" => "api-posted" +}) +article_tag.save +``` + +The return data will be pushed to article_tag.data. + +### Delete + +You can delete an article tag like this: + +``` BASH +curl -X DELETE -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/article-tags/126480 +``` + +Using the gem the same can be achieved by: + +``` RUBY +article_tag = Billomat::Models::ArticleTag.new('id' => 689750) +article_tag.delete +``` + +--- + +#### Links + +\[1\] [English Article Documentation](https://www.billomat.com/en/api/articles/) +\[2\] [German Article Documentation](https://www.billomat.com/api/artikel/) +\[3\] [English Taxes Documentation](https://www.billomat.com/en/api/settings/taxes/) +\[4\] [German Taxes Documentation](billomat.com/api/einstellungen/steuersaetze/) +\[5\] [German Article Tag Documentation](https://www.billomat.com/api/artikel/schlagworte/) diff --git a/doc/client.md b/doc/client.md new file mode 100644 index 0000000..36c0e26 --- /dev/null +++ b/doc/client.md @@ -0,0 +1,548 @@ +# Client/Customer + +Documentation exists in English (1) and German (2). Both cover the client as well as contacts. The German version also covers user defined fields (Attribute) and tags (Schlagwörter). If you can try to follow the German documentation since it is more complete than the English version. The documentation shows the method (get, put, post, delete) and the path and query parameters beyond the base URL as well as some of the parameters the URL takes. All examples are in XML and often are escaped. To read the code you will need to unescape the examples. + +All examples here will be shown once in CURL and JSON and once using the Billomat Gem. If you replace and with the correct values you should be able to use the following curl commands. + +## List + +The per_page value defaults to 100, the page value to 1. + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/clients\?per_page\=2\&page\=1 +``` + +The returned data is a nested object, clients.client contains an array of all the returned client objects. If there is only one clients.client is an object. There is code in utils.rs to handle this case. + +``` JSON +{ + "clients": { + "client": [], + "@page": "1", + "@per_page": "2", + "@total": "12" + } +} +``` + +In ruby you have two ways to get at the list data. The first is to call the list function (page and per_page optional) and the second is to call paged_list (again with page and per_page optional). Each give back slightly different data. + +``` RUBY +clients = Billomat::Models::Client.list +clients.each do |client| + pp client.data +end +``` + +list returns a list of Billomat::Models::Client. To get at the date call the data property. + +``` RUBY +clients = Billomat::Models::Client.paged_list(page = 2, per_page = 10) +pp clients +``` + +paged_list returns a hash with two properties, data which contains the same data as list does and paging_data, a hash that has page, per_page and total as properties. + +## Filter + +You can filter by attribute, in this case the client_number attribute. + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/clients\?client_number\=KD2 +``` + +Or you can filter by a user defined field by using "property\#\{client_property_id\}" as attribute. + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/clients\?property18280\=649062cae817b347c8780fb5 +``` + +In ruby you can filter by calling the where method on the resource. + +``` RUBY +clients = Billomat::Models::Client.where({"client_number": "KD2"}) +``` + +## Create + +Lets create a client by sending the following JSON to the API. + +``` JSON +{ + "client": { + "name": "Test Company", + "first_name": "Tara", + "last_name": "Testarossa", + "street": "Via Ticino 6", + "city": "Turin", + "country_code": "IT" + } +} +``` + +We do that by posting the data like this: + +``` BASH +curl -X POST -H 'X-BillomatApiKey: ' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"client":{"name":"Test Company","first_name":"Tara","last_name":"Testarossa","street":"Via Ticino 6","city":"Turin","country_code":"IT"}}' https://.billomat.net/api/clients +``` + +The return data is the resulting client. The same can be achieved over in ruby like this: + +``` RUBY +client = Billomat::Models::Client.new({ + "name" => "Test Company", + "first_name" => "Tara", + "last_name" => "Testarossa", + "street" => "Via Ticino 6", + "city" => "Turin", + "country_code" => "IT" +}) +client.save +``` + +The return data will be pushed to client.data. + +## Update + +Lets update a client. + +``` JSON +{ + "client": { + "street": "Via Ticino 1" + } +} +``` + +By putting the changed data to the API. + +``` BASH +curl -X PUT -H 'X-BillomatApiKey: ' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"client":{"street":"Via Ticino 1"}}' https://.billomat.net/api/clients/7299528 +``` + +The same can be done in ruby by instantiating a client with ID and saving. + +``` RUBY +client = Billomat::Models::Client.new('id' => 7299528, 'street' => 'Via Ticino 2') +client.save +``` + +The save method is a switch that calls the update method if an id is set and create if not. + +## Delete + +You can delete a client like this: + +``` BASH +curl -X DELETE -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/clients/7272454 +``` + +Using the gem the same can be achieved by: + +``` RUBY +client = Billomat::Models::Client.new('id' => 7272453) +client.delete +``` + +## Contacts + +Documentation for contacts exists in English (3) and German (4). + +### List + +The per_page value defaults to 100, the page value to 1. + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/contacts\?per_page\=2\&page\=1 +``` + +The returned data is a nested object, contacts.contact contains an array of all the returned contact objects. If there is only one contacts.contact is an object. There is code in utils.rs to handle this case. + +``` JSON +{ + "contacts": { + "contact": [], + "@page": "1", + "@per_page": "2", + "@total": "2" + } +} +``` + +In ruby you have two ways to get at the list data. The first is to call the list function (page and per_page optional) and the second is to call paged_list (again with page and per_page optional). Each give back slightly different data. + +``` RUBY +contacts = Billomat::Models::Contact.list +contacts.each do |contact| + pp contact.data +end +``` + +list returns a list of Billomat::Models::Contact. To get at the date call the data property. + +``` RUBY +contacts = Billomat::Models::Contact.paged_list(page = 2, per_page = 10) +pp contacts +``` + +paged_list returns a hash with two properties, data which contains the same data as list does and paging_data, a hash that has page, per_page and total as properties. + +## Filter + +You can filter by attribute, in this case the last_name attribute. + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/contacts\?last_name\=Mustaman +``` + +The same can be achieve in ruby by: + +``` RUBY +contacts = Billomat::Models::Contact.where({"last_name": "Mustaman"}) +``` + +### Create + +Lets create a client contact by sending the following JSON to the API. + +``` JSON +{ + "contact": { + "client_id": 7233379, + "first_name": "Mustafa", + "last_name": "Mustaman", + "street": "Musterstr. 1", + "city": "Mutterstadt", + "email": "mustafa.musterman@example.com" + } +} +``` + +We do that by posting the data like this: + +``` BASH +curl -X POST -H 'X-BillomatApiKey: ' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"contact":{"client_id":7233379,"first_name":"Mustafa","last_name":"Mustama","street":"Musterstr. 1","city":"Mutterstadt","email":"mustafa.musterman@example.com"}}' https://.billomat.net/api/contacts +``` + +The return data is the resulting contact. The same can be achieved over in ruby like this: + +``` RUBY +contact = Billomat::Models::Contact.new({ + "client_id": 7233379, + "first_name": "Maria", + "last_name": "Mustaman", + "street": "Musterstr. 2", + "city": "Mutterstadt", + "email": "maria.musterman@example.com" +}) +contact.save +``` + +The return data will be pushed to contact.data. + +### Update + +Lets update a contact. + +``` JSON +{ + "contact": { + "street": "Thomas-Mann-Straße 7" + } +} +``` + +By putting the changed data to the API. + +``` BASH +curl -X PUT -H 'X-BillomatApiKey: ' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"client":{"street":"Via Ticino 1"}}' https://.billomat.net/api/contacts/101955 +``` + +The same can be done in ruby by instantiating a contact with ID and saving. + +``` RUBY +contact = Billomat::Models::Contact.new('id' => 101955, 'last_name' => 'Meyer') +contact.save +``` + +The save method is a switch that calls the update method if an id is set and create if not. + +### Delete + +You can delete a contact like this: + +``` BASH +curl -X DELETE -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/contacts/101950 +``` + +Using the gem the same can be achieved by: + +``` RUBY +contact = Billomat::Models::Contact.new('id' => 101955) +contact.delete +``` + +## User defined fields + +Additional fields can be defined for clients/customers as well as a few other resources. + +### List + +The per_page value defaults to 100, the page value to 1. + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/client-property-values\?per_page\=2\&page\=1 +``` + +The returned data is a nested object, client-property-values.client-property-value contains an array of all the returned client-property objects. If there is only one client-property-values.client-property-value is an object. There is code in utils.rs to handle this case. + +``` JSON +{ + "client-property-values": { + "client-property-value": [], + "@page": "1", + "@per_page": "2", + "@total": "12" + } +} +``` + +In ruby you have two ways to get at the list data. The first is to call the list function (page and per_page optional) and the second is to call paged_list (again with page and per_page optional). Each give back slightly different data. + +``` RUBY +client_properties = Billomat::Models::ClientPropertyValue.list +client_properties.each do |property| + pp property.data +end +``` + +list returns a list of Billomat::Models::ClientPropertyValue. To get at the date call the data property. + +``` RUBY +client_properties = Billomat::Models::ClientPropertyValue.paged_list(page = 2, per_page = 10) +pp client_properties +``` + +paged_list returns a hash with two properties, data which contains the same data as list does and paging_data, a hash that has page, per_page and total as properties. + +### Filter + +You can filter by attribute, in this case the client_id attribute. + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/client-property-values\?client_id\=7245269 +``` + +The same can be achieve in ruby by: + +``` RUBY +clients = Billomat::Models::ClientPropertyValue.where({"client_number": "KD2"}) +``` + +You can also filter on the client using client-property-values like described in the appropriate section above. + +### Create + +Lets create a client property by sending the following JSON to the API. + +``` JSON +{ + "client-property-value": { + "client_id": "7233379", + "client_property_id": "18291", + "value": "ext-1237" + } +} +``` + +We do that by posting the data like this: + +``` BASH +curl -X POST -H 'X-BillomatApiKey: ' -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{"client-property-value":{"client_id":"7233379","client_property_id":"18291","value":"ext-1237"}}' https://.billomat.net/api/client-property-values +``` + +The return data is the resulting client-property-value. The same can be achieved over in ruby like this: + +``` RUBY +client_property = Billomat::Models::ClientPropertyValue.new({ + "client_id" => "7235858", + "client_property_id" => "18291", + "value" => "ext-1007" +}) +client_property.save +``` + +The return data will be pushed to client_property.data. + +### Update + +Lets update a client property. + +``` JSON +{ + "client-property-value": { + "value":"ext-1234" + } +} +``` + +By putting the changed data to the API. + +``` BASH +curl -X PUT -H 'X-BillomatApiKey: ' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"client-property-value":{"value":"ext-1234"}}' https://.billomat.net/api/client-property-values/9672334 +``` + +The same can be done in ruby by instantiating a client property with ID and saving. + +``` RUBY +client = Billomat::Models::ClientPropertyValue.new('id' => 9672334, 'value' => 'ext-12345') +client.save +``` + +The save method is a switch that calls the update method if an id is set and create if not. + +### Delete + +You can delete a client property like this: + +``` BASH +curl -X DELETE -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/client-property-values/9672513 +``` + +Using the gem the same can be achieved by: + +``` RUBY +client = Billomat::Models::ClientPropertyValue.new('id' => 9672334) +client.delete +``` + +## Metafield + +Clients, like all resources in the API, have a field called customfield. It is a metadata field that can be set and filtered for without having to use the property or tags API. + +``` BASH +curl -X PUT -H 'X-BillomatApiKey: ' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"client":{"customfield":"muster"}}' https://.billomat.net/api/clients/7245252/customfield +``` + +The same can be achieved in Ruby by: + +``` RUBY +client = Billomat::Models::Client.new({ + 'id' => 15949653, + 'customfield' => 'test data' +}) +client.save +``` + +## Tags (Schlagwörter) + +Documentation for client tags exists in German (6). + +### List + +The per_page value defaults to 100, the page value to 1. + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/client-tags\?per_page\=5\&page\=1 +``` + +The returned data is a nested object, client-tags.client-tag contains an array of all the returned client-tag objects. If there is only one client-tags.client-tag is an object. There is code in utils.rs to handle this case. + +``` JSON +{ + "client-tags": { + "client-tag": [], + "@page": "1", + "@per_page": "2", + "@total": "12" + } +} +``` + +In ruby you have two ways to get at the list data. The first is to call the list function (page and per_page optional) and the second is to call paged_list (again with page and per_page optional). Each give back slightly different data. + +``` RUBY +client_tags = Billomat::Models::ClientTag.list +client_tags.each do |tag| + pp tag.data +end +``` + +list returns a list of Billomat::Models::ClientTag. To get at the date call the data property. + +``` RUBY +client_tags = Billomat::Models::ClientTag.paged_list(page = 2, per_page = 10) +pp client_tags +``` + +paged_list returns a hash whith two properties, data which contains the same data as list does and paging_data, a hash that has page, per_page and total as properties. + +If you want to list the client tags of a client you can do so by calling: + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/client-tags\?client_id\=7299528 +``` + +The same can be achieved over in ruby like this: + +``` RUBY +client_tag = Billomat::Models::ClientTag.where('client_id' => 7299528) +pp client_tag +``` + +### Create + +Lets add a client tag by sending the following JSON to the API. + +``` JSON +{ + "client-tag": { + "client_id": "7299528", + "name": "test" + } +} +``` + +We do that by posting the data like this: + +``` BASH +curl -X POST -H 'X-BillomatApiKey: ' -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{"client-tag":{"client_id":"7299528","name":"test"}}' https://.billomat.net/api/client-tags +``` + +The return data is the resulting client-tag. The same can be achieved over in ruby like this: + +``` RUBY +client_tag = Billomat::Models::ClientTag.new({ + "client_id" => "7299528", + "name" => "api-posted" +}) +client_tag.save +``` + +The return data will be pushed to client_tag.data. + +### Delete + +You can delete a client tag like this: + +``` BASH +curl -X DELETE -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/client-tags/689757 +``` + +Using the gem the same can be achieved by: + +``` RUBY +client_tag = Billomat::Models::ClientTag.new('id' => 689750) +client_tag.delete +``` + +--- + +#### Links + +\[1\] [English Client Documentation](https://www.billomat.com/en/api/clients/) +\[2\] [German Client Documentation](https://www.billomat.com/api/kunden/) +\[3\] [English Contact Documentation](https://www.billomat.com/en/api/clients/contacts/) +\[4\] [German Contact Documentation](https://www.billomat.com/api/kunden/kontakte/) +\[5\] [German Client Attribute Documentation](https://www.billomat.com/api/kunden/attribute/) +\[6\] [German Client Tags Documentation](https://www.billomat.com/api/kunden/schlagworte/) diff --git a/doc/invoice.md b/doc/invoice.md new file mode 100644 index 0000000..219e1f5 --- /dev/null +++ b/doc/invoice.md @@ -0,0 +1,358 @@ +# Invoice + +Documentation exists in English (1) and German (2). Both cover the basics of Invoicing, comments and payment. The German version also covers invoice positions and tags (Schlagwörter). If you can try to follow the German documentation since it is more complete than the English version. The documentation shows the method (get, put, post, delete) and the path and query parameters beyond the base URL as well as some of the parameters the URL takes. All examples are in XML and often are escaped. To read the code you will need to unescape the examples. + +All examples here will be shown once in CURL and JSON and once using the Billomat Gem. If you replace and with the correct values you should be able to use the following curl commands. + +## List + +The per_page value defaults to 100, the page value to 1. + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/invoices\?per_page\=2\&page\=1 +``` + +The returned data is a nested object, invoices.invoice contains an array of all the returned invoice objects. If there is only one invoices.invoice is an object. There is code in utils.rs to handle this case. + +``` JSON +{ + "invoices": { + "invoice": [], + "@page": "1", + "@per_page": "2", + "@total": "12" + } +} +``` + +In ruby you have two ways to get at the list data. The first is to call the list function (page and per_page optional) and the second is to call paged_list (again with page and per_page optional). Each give back slightly different data. + +``` RUBY +invoices = Billomat::Models::Invoice.list +invoices.each do |invoice| + pp invoice.data +end +``` + +list returns a list of Billomat::Models::Invoice. To get at the date call the data property. + +``` RUBY +invoices = Billomat::Models::Invoice.paged_list(page = 2, per_page = 10) +pp invoices +``` + +paged_list returns a hash with two properties, data which contains the same data as list does and paging_data, a hash that has page, per_page and total as properties. + +## Filter + +You can filter by attribute, in this case the customfield attribute. + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/invoices/\?customfield\=649062cae817b347c8780fb5 +``` + +In ruby you can filter by calling the where method on the resource. + +``` RUBY +invoices = Billomat::Models::Invoice.where({"customfield": "test data"}) +``` + +## Create + +There are multiple ways of working with invoices items. The easiest way to deal with them is to just nest them during invoice creation. If you need to manipulate the invoice items afterwards there is a separate invoice items api that can be used for that (3). +Lets create an invoice by sending the following JSON to the API. + +``` JSON +{ + "invoice": { + "client_id": 7233379, + "address":"Musterfirma\nMax Muster\nMusterstraße 1\nMusterhausen", + "date": "2023-07-03", + "supply_date": "2023-07-03", + "supply_date_type": "SUPPLY_DATE", + "discount_days": 14, + "payment_types": "BANK_TRANSFER", + "intro": "Short intro text!", + "note": "Note: Due date is [Invoice.due_date].", + "invoice-items": { + "invoice-item": [ + { + "article_id": 1439750, + "quantity": "1" + }, + { + "article_id": 1439751, + "quantity": "1" + } + ] + } + } +} +``` + +We do that by posting the data like this: + +``` BASH +curl -X POST -H 'X-BillomatApiKey: ' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"invoice":{"client_id":7233379,"address":"Musterfirma\nMax Muster\nMusterstraße 1\nMusterhausen","date":"2023-07-03","supply_date":"2023-07-03","supply_date_type":"SUPPLY_DATE","discount_days":14,"payment_types":"BANK_TRANSFER","intro":"Short intro text!","note":"Note: Due date is [Invoice.due_date].","invoice-items":{"invoice-item":[{"article_id":1439750,"quantity":"1"},{"article_id":1439751,"quantity":"1"}]}}}' https://.billomat.net/api/invoices +``` + +The return data is the resulting invoice in draft status. The same can be achieved over in ruby like this: + +``` RUBY +client_id = 7235883 +client = Billomat::Models::Client.find(client_id) +now = DateTime.now.to_date.as_json +invoice_data = { + 'client_id' => client_id, + 'address' => client.data.address, + 'date' => now, + 'supply_date' => now, + 'supply_date_type' => 'SUPPLY_DATE', + 'discount_days' => 14, + 'payment_types' => 'BANK_TRANSFER', + 'intro' => 'Short intro text!', + 'note' => 'Note: Due date is [Invoice.due_date].', + 'invoice-items' => { + 'invoice-item': [{ + 'article_id': 1439750, + 'quantity': 1 + }, { + 'article_id': 1439751, + 'quantity': 1 + }] + } +} +invoice = Billomat::Models::Invoice.new(invoice_data) +invoice.save +``` + +The return data will be pushed to invoice.data. + +## Update + +Lets update an invoice. + +``` JSON +{ + "invoice": { + "intro": "Updated intro!", + "note": "Updated note!", + "date": "2023-07-05", + "supply_date": "2023-07-05" + } +} +``` + +By putting the changed data to the API. + +``` BASH +curl -X PUT -H 'X-BillomatApiKey: ' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"invoice":{"intro":"Updated intro!","note":"Updated note!","date":"2023-07-05","supply_date":"2023-07-05"}}' https://.billomat.net/api/invoices/15949653 +``` + +The same can be done in ruby by instantiating an invoice with ID and saving. + +``` RUBY +invoice_data = { + 'id' => 15949653, + 'date' => '2023-07-07', + 'supply_date' => '2023-07-07', + 'intro' => 'Second updated intro!', + 'note' => 'Second updated note!' +} +invoice = Billomat::Models::Invoice.new(invoice_data) +invoice.save +``` + +The save method is a switch that calls the update method if an id is set and create if not. + +## Delete + +You can delete an invoice like this: + +``` BASH +curl -X DELETE -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/invoices/15949653 +``` + +Using the gem the same can be achieved by: + +``` RUBY +invoice = Billomat::Models::Invoice.new('id' => 15949653) +invoice.delete +``` + +## Invoice Items + +### List + +The easiest way to work on invoice items after the invoice has been created is to use the invoice items api. Lets list the items we have attached to an invoice. + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/invoice-items\?invoice_id\=15896334 +``` + +The return data is similar to that of an invoice. + +``` JSON +{ + "invoice-items": { + "invoice-item": [ + ], + "@page": "1", + "@per_page": "100", + "@total": "3" + } +} +``` + +In Ruby that call looks like this: + +``` RUBY +invoice_items = Billomat::Models::InvoiceItem.where('invoice_id' => 15896334) +``` + +And the call returns an array of Billomat::Models::InvoiceItem instances. + +### Create + +Lets add this item to an invoice. + +``` JSON +{ + "invoice-item": { + "article_id": 1439751, + "invoice_id": 15949653, + "quantity": 1 + } +} +``` + +By posting it like this: + +``` BASH +curl -X POST -H 'X-BillomatApiKey: ' -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{"invoice-item":{"article_id":1439751,"invoice_id":15949653,"quantity":1}}' https://.billomat.net/api/invoice-items +``` + +The return data is the created object. You can achieve the same in Ruby by doing this: + +``` RUBY +invoice_item = Billomat::Models::InvoiceItem.new({ + 'article_id': 1439750, + 'invoice_id': 15949653, + 'quantity': 1 +}) +invoice_item.save +``` + +The return data here is in the data attribute. + +### Update + +To update an invoice item you can just update the relevant fields. Here we update the quantity of an item and the item will be updated accordingly, calculating new total prices and so on + +``` BASH +curl -X PUT -H 'X-BillomatApiKey: ' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"invoice-item":{"quantity":2}}' https://.billomat.net/api/invoice-items/41129418 +``` + +In Ruby the same thing can be achieved like this: + +``` RUBY +invoice_item = Billomat::Models::InvoiceItem.new('id' => 41155556, 'quantity' => 5) +invoice_item.save +``` + +### Delete + +Invoice items have their own ID and can be deleted like this: + +``` BASH +curl -X DELETE -H 'X-BillomatApiKey: ' -H 'Accept: application/json' https://.billomat.net/api/invoice-items/40962566 +``` + +Using the gem the same can be achieved by: + +``` RUBY +invoice_item = Billomat::Models::InvoiceItem.new('id' => 40962565) +invoice_item.delete +``` + +## Finalize + +To finalize an invoice call the api endpoint like this: + +``` BASH +curl -X PUT -H 'X-BillomatApiKey: ' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"complete":{}}' https://.billomat.net/api/invoices//complete +``` + +The same can be done in ruby like this: + +``` RUBY +invoice_id = 15951649 +invoice = Billomat::Actions::Complete.new(invoice_id) +invoice.call +``` + +If you have a Billomat::Models::Invoice you can also call the complete! method to achieve the same. + +## Cancel + +To cancel (stornieren) an invoice call the api endpoint like this: + +``` BASH +curl -X PUT -H 'X-BillomatApiKey: ' -H 'Accept: application/json' -H 'Content-Type: application/json' https://.billomat.net/api/invoices/15951649/cancel +``` + +Using the gem the same can be achieved by: + +``` RUBY +invoice_id = 15949784 +invoice = Billomat::Actions::Cancel.new(invoice_id) +invoice.call +``` + +If you have a Billomat::Models::Invoice instance you can call the cancel! method as an alternative. + +## Download PDF + +The pdf can be downloaded like this: + +``` BASH +curl -H 'X-BillomatApiKey: ' -H 'Accept: application/pdf' https://.billomat.net/api/invoices/15951649/pdf --output invoice-15951649.pdf +``` + +The same can be done in ruby like this: + +``` RUBY +invoice_id = 15951649 +invoice = Billomat::Actions::Pdf.new(invoice_id) +pdf = invoice.call +file = File.open("#{$INVOICE_DOWNLOADS}/invoice-#{invoice_id}.pdf", 'wb') +file.write Base64.decode64(pdf['base64file']) +``` + +If you have a Billomat::Models::Invoice instance you can also call the to_pdf method to indirectly call the code to fetch the pdf. + +## Metafield + +Invoices, like all resources in the API, have a field called customfield. It is a metadata field that can be set and filtered for without having to use the property or tags API. + +``` BASH +curl -X PUT -H 'X-BillomatApiKey: ' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"invoice":{"customfield":"muster"}}' https://.billomat.net/api/invoices/15829804/customfield +``` + +The same can be achieved in Ruby by: + +``` RUBY +invoice = Billomat::Models::Invoice.new({ + 'id' => 15949653, + 'customfield' => 'test data' +}) +invoice.save +``` + +--- + +#### Links + +\[1\] [English Invoice Documentation](https://www.billomat.com/en/api/invoices/) +\[2\] [German Invoice Documentation](https://www.billomat.com/api/rechnungen/) +\[3\] [German Invoice Items Documentation](https://www.billomat.com/api/rechnungen/positionen/) diff --git a/lib/billomat.rb b/lib/billomat.rb index d14b746..7f685b6 100644 --- a/lib/billomat.rb +++ b/lib/billomat.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'billomat/version' +require 'billomat/utils' require 'billomat/configuration' require 'billomat/models' require 'billomat/actions' diff --git a/lib/billomat/actions.rb b/lib/billomat/actions.rb index 772dd70..bde24a4 100644 --- a/lib/billomat/actions.rb +++ b/lib/billomat/actions.rb @@ -5,6 +5,7 @@ require 'billomat/actions/pdf' require 'billomat/actions/cancel' require 'billomat/actions/uncancel' +require 'billomat/actions/correct' module Billomat # Actions are API calls that do not directly represent a resource. diff --git a/lib/billomat/actions/correct.rb b/lib/billomat/actions/correct.rb new file mode 100644 index 0000000..321e8bf --- /dev/null +++ b/lib/billomat/actions/correct.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +module Billomat + module Actions + # This actions correct an invoice + class Correct + # Returns a Correct object. + # + # @param invoice_id [String] the ID of the invoice + # @param opts [Hash] the options for this request + # @return [Billomat::Actions::Complete] + # + # @example + # Billomat::Actions::Complete('12345', { template_id: '10231' }) + def initialize(invoice_id, opts = {}) + @invoice_id = invoice_id + @opts = opts + end + + # Calls the gateway. + # + # @return [TrueClass] + def call + Billomat::Gateway.new(:post, path, wrapped_data).run + end + + # The given options have to be wrapped. + # + # @return [Hash] the payload for the correct request + def wrapped_data + { correct: @opts } + end + + # @return [String] the correct path with the invoice_id + def path + "/invoices/#{@invoice_id}/correct" + end + end + end +end diff --git a/lib/billomat/models.rb b/lib/billomat/models.rb index 5c69bc8..46bb1e1 100644 --- a/lib/billomat/models.rb +++ b/lib/billomat/models.rb @@ -1,7 +1,12 @@ # frozen_string_literal: true require 'billomat/models/base' +require 'billomat/models/article' +require 'billomat/models/article_property_value' +require 'billomat/models/article_tag' require 'billomat/models/client' +require 'billomat/models/client_property_value' +require 'billomat/models/client_tag' require 'billomat/models/credit_note' require 'billomat/models/credit_note_item' require 'billomat/models/invoice' @@ -10,6 +15,7 @@ require 'billomat/models/invoice_payment' require 'billomat/models/contact' require 'billomat/models/tag' +require 'billomat/models/tax' require 'billomat/models/template' module Billomat diff --git a/lib/billomat/models/article.rb b/lib/billomat/models/article.rb new file mode 100644 index 0000000..ae8622c --- /dev/null +++ b/lib/billomat/models/article.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Billomat + module Models + # Representation of the article resource. + class Article < Base + # @return [String] the resource's base path + def self.base_path + '/articles' + end + + # @return [String] the resource's name + def self.resource_name + 'article' + end + end + end +end diff --git a/lib/billomat/models/article_property_value.rb b/lib/billomat/models/article_property_value.rb new file mode 100644 index 0000000..ad99218 --- /dev/null +++ b/lib/billomat/models/article_property_value.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Billomat + module Models + # Representation of the article-property-value resource. + class ArticlePropertyValue < Base + # @return [String] the resource's base path + def self.base_path + '/article-property-values' + end + + # @return [String] the resource's name + def self.resource_name + 'article-property-value' + end + end + end +end diff --git a/lib/billomat/models/article_tag.rb b/lib/billomat/models/article_tag.rb new file mode 100644 index 0000000..8c62307 --- /dev/null +++ b/lib/billomat/models/article_tag.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Billomat + module Models + # Representation of the article-tag resource. + class ArticleTag < Base + # @return [String] the resource's base path + def self.base_path + '/article-tags' + end + + # @return [String] the resource's name + def self.resource_name + 'article-tag' + end + end + end +end diff --git a/lib/billomat/models/base.rb b/lib/billomat/models/base.rb index 1425f8b..f46f2ca 100644 --- a/lib/billomat/models/base.rb +++ b/lib/billomat/models/base.rb @@ -28,6 +28,29 @@ def self.where(hash = {}) Billomat::Search.new(self, hash).run end + # @param [Integer] page The page of data + # @param [Integer] per_page The amount of items per page + # @return [Hash{String => Mixed}] the paging_data (Hash with page, per_page and total) \ + # and the data (Array) + def self.paged_list(page = 1, per_page = 100) + info = { 'page' => page, 'per_page' => per_page } + paging_info = URI.encode_www_form(info) + path = "#{base_path}?#{paging_info}" + resp = Billomat::Gateway.new(:get, path).run + paging_data = Billomat::Utils.get_paging_data(resp, resource_name) + oob = Billomat::Utils.out_of_bounds(paging_data) + data = oob ? [] : Billomat::Utils.to_array(resp, resource_name, self) + + OpenStruct.new(paging_data.merge(data: data)) + end + + # @param [Integer] page The page of data + # @param [Integer] per_page The amount of items per page + # @return [Array] the found records + def self.list(page = 1, per_page = 100) + paged_list(page, per_page).data + end + # Initializes a new model. # # @param data [Hash] the attributes of the object diff --git a/lib/billomat/models/client_property_value.rb b/lib/billomat/models/client_property_value.rb new file mode 100644 index 0000000..a48cbba --- /dev/null +++ b/lib/billomat/models/client_property_value.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Billomat + module Models + # Representation of the client-property-value resource. + class ClientPropertyValue < Base + # @return [String] the resource's base path + def self.base_path + '/client-property-values' + end + + # @return [String] the resource's name + def self.resource_name + 'client-property-value' + end + end + end +end diff --git a/lib/billomat/models/client_tag.rb b/lib/billomat/models/client_tag.rb new file mode 100644 index 0000000..4d9fbeb --- /dev/null +++ b/lib/billomat/models/client_tag.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Billomat + module Models + # Representation of the client-tag resource. + class ClientTag < Base + # @return [String] the resource's base path + def self.base_path + '/client-tags' + end + + # @return [String] the resource's name + def self.resource_name + 'client-tag' + end + end + end +end diff --git a/lib/billomat/models/invoice.rb b/lib/billomat/models/invoice.rb index 614af4b..c2f417f 100644 --- a/lib/billomat/models/invoice.rb +++ b/lib/billomat/models/invoice.rb @@ -14,6 +14,12 @@ def self.resource_name 'invoice' end + # Completes the invoice by calling the Complete action. + def correct! + resp = Billomat::Actions::Correct.new(@data.invoice_id, @data.to_h).call + @data = OpenStruct.new(resp[self.class.resource_name]) + end + # Completes the invoice by calling the Complete action. def complete! Billomat::Actions::Complete.new(id).call diff --git a/lib/billomat/models/tax.rb b/lib/billomat/models/tax.rb new file mode 100644 index 0000000..6145e85 --- /dev/null +++ b/lib/billomat/models/tax.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Billomat + module Models + # Representation of the tax resource. + class Tax < Base + # @return [String] the resource's base path + def self.base_path + '/taxes' + end + + # @return [String] the resource's name + def self.resource_name + 'tax' + end + end + end +end diff --git a/lib/billomat/search.rb b/lib/billomat/search.rb index 5398099..e6f5184 100644 --- a/lib/billomat/search.rb +++ b/lib/billomat/search.rb @@ -26,28 +26,8 @@ def path def run return [] if @hash.compact.empty? - to_array(Billomat::Gateway.new(:get, path).run) - end - - # Corrects the response to always return an array. - # - # @todo Due to a strange API behaviour we have to fix the reponse here. - # This may be fixed in a new API version. - # - # @param [Hash] resp The response from the gateway - # @return [Array] - def to_array(resp) - case count(resp) - when 0 - [] - when 1 - # Necessary due to strange API behaviour - [@resource.new(resp["#{name}s"][name])] - else - resp["#{name}s"][name].map do |c| - @resource.new(c) - end - end + resp = Billomat::Gateway.new(:get, path).run + Billomat::Utils.to_array(resp, name, @resource) end # @return [String] the name of the resource @@ -55,14 +35,6 @@ def name @resource.resource_name end - # @param [Hash] resp The response from the gateway - # @return [Integer] the number of records found - def count(resp) - return 0 if resp.nil? - - resp["#{name}s"]['@total'].to_i - end - private # @return [String] the query as www encoded string diff --git a/lib/billomat/utils.rb b/lib/billomat/utils.rb new file mode 100644 index 0000000..b9b5926 --- /dev/null +++ b/lib/billomat/utils.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +module Billomat + # This class provides the possibility utility functions for data formatting + class Utils + def self.get_sufix(name) + return 'es' if name == 'tax' + 's' + end + + # @param [Hash] resp The response from the gateway + # @return [Integer] the number of records found + def self.count(resp, name) + return 0 if resp.nil? + + sufix = Billomat::Utils.get_sufix(name) + resp["#{name}#{sufix}"]['@total'].to_i + end + + # Corrects the response to always return an array. + # + # @todo Due to a strange API behaviour we have to fix the reponse here. + # This may be fixed in a new API version. + # + # @param [Hash] resp The response from the gateway + # @param [String] name The name of the resource + # @param [Class] resource The resource class to be queried + # @return [Array] + def self.to_array(resp, name, resource) + sufix = Billomat::Utils.get_sufix(name) + case count(resp, name) + when 0 + [] + when 1 + # Necessary due to strange API behaviour + [resource.new(resp["#{name}#{sufix}"][name])] + else + resp["#{name}#{sufix}"][name].map do |c| + resource.new(c) + end + end + end + + # @param [Hash] resp The response from the gateway + # @param [String] name The name of the resource + # @return [Hash{String => Mixed}] The paging info (page, per_page and total) + def self.get_paging_data(resp, name) + sufix = Billomat::Utils.get_sufix(name) + page = resp["#{name}#{sufix}"]['@page'].to_i + per_page = resp["#{name}#{sufix}"]['@per_page'].to_i + total = resp["#{name}#{sufix}"]['@total'].to_i + + { + 'page' => page, + 'per_page' => per_page, + 'total' => total + } + end + + # @param [Hash] paging_data The response from get_paging_data + def self.out_of_bounds(paging_data) + if paging_data['total'].zero? + true + elsif paging_data['page'] == 1 + false + else + shown = (paging_data['page'] - 1) * paging_data['per_page'] + shown > paging_data['total'] + end + end + end +end