Class: Datakick
- Inherits:
-
Object
- Object
- Datakick
- Defined in:
- lib/datakick.rb,
lib/datakick/item.rb,
lib/datakick/version.rb
Defined Under Namespace
Classes: Item
Constant Summary collapse
- API_VERSION =
1
- API_HOST =
API_HOST = “0.0.0.0:5000”
"https://www.datakick.org"
- VERSION =
"0.0.1"
Instance Method Summary collapse
- #add_image(gtin, image, image_type) ⇒ Object
-
#initialize ⇒ Datakick
constructor
A new instance of Datakick.
- #item(gtin) ⇒ Object
- #items(params = {}) ⇒ Object
- #paginated_items(params, &block) ⇒ Object
- #update_item(gtin, attributes) ⇒ Object
Constructor Details
#initialize ⇒ Datakick
Returns a new instance of Datakick.
12 13 |
# File 'lib/datakick.rb', line 12 def initialize end |
Instance Method Details
#add_image(gtin, image, image_type) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/datakick.rb', line 29 def add_image(gtin, image, image_type) params = { gtin: gtin, image: image, perspective: image_type } response = http_client.post("/api/items/#{gtin}/images?version=#{API_VERSION}", params) if response.success? JSON.parse(response.body) end end |
#item(gtin) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/datakick.rb', line 15 def item(gtin) response = http_client.get("/api/items/#{gtin}?version=#{API_VERSION}") if response.success? Item.new(JSON.parse(response.body)) end end |
#items(params = {}) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/datakick.rb', line 41 def items(params = {}) response = http_client.get("/api/items?version=#{API_VERSION}", params) if response.success? JSON.parse(response.body).map do |item| Item.new(item) end end end |
#paginated_items(params, &block) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/datakick.rb', line 50 def paginated_items(params, &block) response = http_client.get("/api/items?version=#{API_VERSION}", params) begin links = {} if response.success? JSON.parse(response.body).map do |item| yield Item.new(item) end # https://gist.github.com/davidcelis/5896686 response.headers['Link'].to_s.split(',').each do |link| link.strip! parts = link.match(/<(.+)>; *rel="(.+)"/) links[parts[2]] = parts[1] end end # p links end while links["next"] and (response = http_client.get(links["next"])) end |
#update_item(gtin, attributes) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/datakick.rb', line 22 def update_item(gtin, attributes) response = http_client.put("/api/items/#{gtin}?version=#{API_VERSION}", attributes) if response.success? Item.new(JSON.parse(response.body)) end end |