datakick 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 63d3362ff6faa2dd896fd5f1d8781ad00dad45eb
4
+ data.tar.gz: e8940d3a8b3906431897f58e56857fe593be20fb
5
+ SHA512:
6
+ metadata.gz: e6e4eddc8f4a752058698ff038a3f418d41e31f89a408fdac83538b89b0c4d4901deb351a857f196406b29ac6dbb8e27fd96890c39b546a72fae9188a923f379
7
+ data.tar.gz: d1e6fc808752deb657dff2848d036bce9ec428ac1046e2024cd43d5bf7197b7dfc470541c3b12f3dc70c92ad74bcb5640a7d63818ce3cce88153ecfd0398f8e3
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in datakick.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Andrew Kane
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,72 @@
1
+ # Datakick
2
+
3
+ Ruby client for [Datakick](https://www.datakick.org/) - the open product database
4
+
5
+ ## Get Started
6
+
7
+ Add this line to your application’s Gemfile:
8
+
9
+ ```ruby
10
+ gem 'datakick'
11
+ ```
12
+
13
+ Create a client
14
+
15
+ ```ruby
16
+ datakick = Datakick.new
17
+ ```
18
+
19
+ Get an item
20
+
21
+ ```ruby
22
+ item = datakick.item("013562610020") # or nil if not found
23
+ item.gtin14
24
+ item.brand_name
25
+ item.name
26
+ item.size
27
+ ```
28
+
29
+ Create or update an item
30
+
31
+ ```ruby
32
+ datakick.update_item("000000000000", {name: "Test"})
33
+ ```
34
+
35
+ List items
36
+
37
+ ```ruby
38
+ datakick.items
39
+ ```
40
+
41
+ Returns the first 100 items.
42
+
43
+ To get all items, use:
44
+
45
+ ```ruby
46
+ datakick.paginated_items do |item|
47
+ item.gtin14
48
+ end
49
+ ```
50
+
51
+ Search items
52
+
53
+ ```ruby
54
+ datakick.items(query: "peanut butter")
55
+ ```
56
+
57
+ Add an image
58
+
59
+ ```ruby
60
+ image = Faraday::UploadIO.new("ice_cream.jpg", "image/jpeg")
61
+ image_type = "scan" # or "photo"
62
+ datakick.add_image("000000000000", image, image_type)
63
+ ```
64
+
65
+ ## Contributing
66
+
67
+ Everyone is encouraged to help improve this project. Here are a few ways you can help:
68
+
69
+ - [Report bugs](https://github.com/ankane/datakick/issues)
70
+ - Fix bugs and [submit pull requests](https://github.com/ankane/datakick/pulls)
71
+ - Write, clarify, or fix documentation
72
+ - Suggest or add new features
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ task :default => :test
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.pattern = "test/**/*_test.rb"
8
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'datakick/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "datakick"
8
+ spec.version = Datakick::VERSION
9
+ spec.authors = ["Andrew Kane"]
10
+ spec.email = ["andrew@datakick.org"]
11
+ spec.summary = %q{Ruby client for Datakick - the open product database}
12
+ spec.description = %q{Ruby client for Datakick - the open product database}
13
+ spec.homepage = "https://github.com/ankane/datakick-rb"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "faraday"
22
+ spec.add_dependency "hashie"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.5"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "minitest"
27
+ end
@@ -0,0 +1,80 @@
1
+ require "json"
2
+ require "faraday"
3
+ require "hashie"
4
+ require "datakick/version"
5
+ require "datakick/item"
6
+
7
+ class Datakick
8
+ API_VERSION = 1
9
+ # API_HOST = "http://0.0.0.0:5000"
10
+ API_HOST = "https://www.datakick.org"
11
+
12
+ def initialize
13
+ end
14
+
15
+ def item(gtin)
16
+ response = http_client.get("/api/items/#{gtin}?version=#{API_VERSION}")
17
+ if response.success?
18
+ Item.new(JSON.parse(response.body))
19
+ end
20
+ end
21
+
22
+ def update_item(gtin, attributes)
23
+ response = http_client.put("/api/items/#{gtin}?version=#{API_VERSION}", attributes)
24
+ if response.success?
25
+ Item.new(JSON.parse(response.body))
26
+ end
27
+ end
28
+
29
+ def add_image(gtin, image, image_type)
30
+ params = {
31
+ gtin: gtin,
32
+ image: image,
33
+ perspective: image_type
34
+ }
35
+ response = http_client.post("/api/items/#{gtin}/images?version=#{API_VERSION}", params)
36
+ if response.success?
37
+ JSON.parse(response.body)
38
+ end
39
+ end
40
+
41
+ def items(params = {})
42
+ response = http_client.get("/api/items?version=#{API_VERSION}", params)
43
+ if response.success?
44
+ JSON.parse(response.body).map do |item|
45
+ Item.new(item)
46
+ end
47
+ end
48
+ end
49
+
50
+ def paginated_items(params, &block)
51
+ response = http_client.get("/api/items?version=#{API_VERSION}", params)
52
+ begin
53
+ links = {}
54
+ if response.success?
55
+ JSON.parse(response.body).map do |item|
56
+ yield Item.new(item)
57
+ end
58
+
59
+ # https://gist.github.com/davidcelis/5896686
60
+ response.headers['Link'].to_s.split(',').each do |link|
61
+ link.strip!
62
+ parts = link.match(/<(.+)>; *rel="(.+)"/)
63
+ links[parts[2]] = parts[1]
64
+ end
65
+ end
66
+ # p links
67
+ end while links["next"] and (response = http_client.get(links["next"]))
68
+ end
69
+
70
+ protected
71
+
72
+ def http_client
73
+ Faraday.new(url: API_HOST) do |conn|
74
+ conn.request :multipart
75
+ conn.request :url_encoded
76
+ conn.adapter :net_http
77
+ end
78
+ end
79
+
80
+ end
@@ -0,0 +1,4 @@
1
+ class Datakick
2
+ class Item < Hashie::Mash
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ class Datakick
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,49 @@
1
+ require_relative "test_helper"
2
+
3
+ class TestDatakick < Minitest::Test
4
+
5
+ def test_item
6
+ item = datakick.item("000000000000")
7
+ assert_equal "00000000000000", item.gtin14
8
+ end
9
+
10
+ def test_item_not_found
11
+ assert_nil datakick.item("000000000001")
12
+ end
13
+
14
+ def test_update_item
15
+ name = "Test #{rand(100)}"
16
+ item = datakick.update_item("000000000000", {name: name})
17
+ assert_equal name, item.name
18
+ end
19
+
20
+ def test_add_image
21
+ image = Faraday::UploadIO.new("/Users/andrew/Desktop/ice_cream.jpg", "image/jpeg")
22
+ assert datakick.add_image("000000000000", image, "photo")
23
+ end
24
+
25
+ def test_items
26
+ assert_kind_of Array, datakick.items
27
+ end
28
+
29
+ def test_items_query
30
+ assert_kind_of Array, datakick.items(query: "test")
31
+ end
32
+
33
+ def test_paginated_items
34
+ items = []
35
+ datakick.paginated_items(per_page: 1000) do |item|
36
+ # p item
37
+ items << item
38
+ end
39
+ # p items.size
40
+ assert_kind_of Array, items
41
+ end
42
+
43
+ protected
44
+
45
+ def datakick
46
+ Datakick.new
47
+ end
48
+
49
+ end
@@ -0,0 +1,4 @@
1
+ require "bundler/setup"
2
+ Bundler.require(:default)
3
+ require "minitest/autorun"
4
+ require "minitest/pride"
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: datakick
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Kane
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hashie
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Ruby client for Datakick - the open product database
84
+ email:
85
+ - andrew@datakick.org
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - datakick.gemspec
96
+ - lib/datakick.rb
97
+ - lib/datakick/item.rb
98
+ - lib/datakick/version.rb
99
+ - test/datakick_test.rb
100
+ - test/test_helper.rb
101
+ homepage: https://github.com/ankane/datakick-rb
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.2.0
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Ruby client for Datakick - the open product database
125
+ test_files:
126
+ - test/datakick_test.rb
127
+ - test/test_helper.rb