ingenia_api 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+
2
+ $: << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'yaml'
5
+ require 'ingenia_api'
6
+
7
+ Ingenia::Api.api_key = "API_KEY"
8
+
9
+ puts Ingenia::Api.classify "What kind of cheese is the best cheese?"
@@ -0,0 +1,13 @@
1
+ $: << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require 'ingenia_api'
3
+
4
+
5
+ #
6
+ # Setup
7
+ #
8
+ # Set API key to the test user for this gem
9
+ Ingenia::Api.api_key = "API_KEY"
10
+
11
+ require 'irb'
12
+ ARGV.clear
13
+ IRB.start
@@ -0,0 +1,15 @@
1
+ $: << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require 'colorize'
3
+ require 'rest_client'
4
+
5
+ # log rest client activity
6
+ # RestClient.log = STDOUT
7
+
8
+
9
+ def example title
10
+ puts "|| #{title} ||\n".yellow
11
+
12
+ yield
13
+
14
+ puts "\n\n"
15
+ end
@@ -0,0 +1,124 @@
1
+ require './helper'
2
+
3
+ ##
4
+ # Run a full set of API calls related to items
5
+ ##
6
+
7
+
8
+ # Set API key to the test user for this gem
9
+ require 'ingenia_api'
10
+
11
+ Ingenia::Api.api_key = "API_KEY"
12
+ Ingenia::Api.version = 2.0
13
+ ##
14
+ # Create/Train
15
+ #
16
+ # - Creating items with (optional) tags is the same as training.
17
+ # - All new items belong to your first bundle unless specified with the bundle_id param
18
+ # - If the same text is sent twice, the old record is overwritten unless the 'update_existing' param is set to false
19
+ #
20
+ example "Create/train" do
21
+ big_text = "I like cake " * 1000
22
+ new_item = Ingenia::Item.create :json => { :text => big_text, :tags => [ 'food', 'cake', 'obsession'] }
23
+ @new_item_id = new_item['id']
24
+
25
+ puts "new item: "
26
+ puts "#{new_item}".green
27
+
28
+ # Tidy up
29
+ Ingenia::Item.destroy(@new_item_id)
30
+
31
+ # request full text response
32
+ big_text = "I like cake " * 1001
33
+ new_item = Ingenia::Item.create :json => { :text => big_text, :tags => [ 'food', 'cake', 'obsession'] }, :full_text => true
34
+ @new_item_id = new_item['id']
35
+
36
+ puts "new item with full text: "
37
+ puts "#{new_item}".green
38
+
39
+
40
+ # create without updating existing
41
+ response = Ingenia::Item.create :json => { :text => new_item['text'], :tags => [ 'not', 'changed' ] }, :update_existing => false
42
+ puts 'unchaged item'
43
+ puts "#{response}".green
44
+ end
45
+
46
+
47
+ ##
48
+ # Index normal
49
+ #
50
+ example "Index normal" do
51
+ items = Ingenia::Item.all
52
+
53
+ puts "got #{items.length} items"
54
+ puts "\n First Item: "
55
+ puts "#{items.first}".green
56
+
57
+ @test_item_id = items.first['id']
58
+ end
59
+
60
+ ##
61
+ # Index full text
62
+ #
63
+ example "Index full text" do
64
+ items = Ingenia::Item.all(:full_text => "true")
65
+
66
+ puts "got #{items.length} items"
67
+ puts "\n First Item: "
68
+ puts "#{items.first}".green
69
+
70
+ @test_item_id = items.first['id']
71
+ end
72
+
73
+ ##
74
+ # Show
75
+ #
76
+ example "Show" do
77
+ test_item = Ingenia::Item.get(@new_item_id, :full_text => false )
78
+ puts "Quick view: "
79
+ puts "#{test_item}".green
80
+
81
+ # Get it with full text
82
+ @test_item = Ingenia::Item.get(@new_item_id, :full_text => true )
83
+ puts "\n Full text view: "
84
+ puts "#{@test_item}".green
85
+ end
86
+
87
+
88
+ ##
89
+ # Update
90
+ #
91
+ example "Update" do
92
+ response = Ingenia::Item.update(@test_item_id, :json => { :text => "this is some test update text for testing the API" })
93
+
94
+ puts "updated item:"
95
+ puts "#{response}".green
96
+
97
+ # Update its tags
98
+ response = Ingenia::Item.update(@test_item_id, :json => { :text => "this is some test update text for testing the API", :tags => ['api', 'testing'] })
99
+ puts "#{response}".green
100
+ end
101
+
102
+
103
+ ##
104
+ # Destroy
105
+ #
106
+ example "Destroy" do
107
+ # Remove this new item
108
+ response = Ingenia::Item.destroy(@test_item_id)
109
+ puts "#{response}".green
110
+ end
111
+
112
+ ##
113
+ # Similar to - using auto matching
114
+ #
115
+ example "similar to" do
116
+ response = Ingenia::Item.similar_to(@test_item_id)
117
+
118
+ puts "#{response}".green
119
+ end
120
+ example "similar to - matching on words" do
121
+ response = Ingenia::Item.similar_to(@test_item_id, :words)
122
+
123
+ puts "#{response}".green
124
+ end
@@ -0,0 +1,7 @@
1
+ # Run all the examples
2
+
3
+ Dir.foreach('./') do |item|
4
+ next if ['.', '..', 'console.rb'].include? item
5
+
6
+ require("./#{item}")
7
+ end
@@ -0,0 +1,23 @@
1
+ require './helper'
2
+
3
+
4
+ # RestClient.log = STDOUT
5
+ require 'ingenia_api'
6
+ Ingenia::Api.api_key = "API_KEY"
7
+
8
+ # similar to text
9
+ # Moved to Ingenia::Item.similar_to
10
+
11
+ # similar to item
12
+ example "similar to item" do
13
+ response = Ingenia::Api.similar_to :item_id => Ingenia::Item.all.first['id']
14
+
15
+ puts "#{response}".green
16
+ end
17
+
18
+ # similar to tags
19
+ example "similar to tags" do
20
+ response = Ingenia::Api.similar_to :tag_ids => [ 1, 3, 5 ]
21
+
22
+ puts "#{response}".green
23
+ end
@@ -0,0 +1,11 @@
1
+ require './helper'
2
+
3
+ require 'ingenia_api'
4
+ Ingenia::Api.api_key = "API_KEY"
5
+
6
+ # summarise some text
7
+ example "summarize" do
8
+ Ingenia::Api.summarize :text => "What kind of cheese is the best cheese?"
9
+ end
10
+
11
+
@@ -0,0 +1,75 @@
1
+ require './helper'
2
+
3
+ ##
4
+ # Run a full set of API calls on tag_sets
5
+ ##
6
+
7
+
8
+ #
9
+ # Setup
10
+ #
11
+ # Set API key to the test user for this gem
12
+ require 'ingenia_api'
13
+ Ingenia::Api.api_key = "API_KEY"
14
+ Ingenia::Api.version = 2.0
15
+
16
+ ##
17
+ # Create
18
+ #
19
+ example "Create" do
20
+ # Create a new tag_set
21
+ @new_test_tag_set = Ingenia::TagSet.create(:name => "new tag set name")
22
+ puts "\n created a new tag_set:"
23
+ puts "#{@new_test_tag_set}".green
24
+ end
25
+
26
+
27
+ ##
28
+ # Index
29
+ #
30
+ example "Index" do
31
+ # Get a list of all your tag_sets
32
+ tag_sets = Ingenia::TagSet.all
33
+ puts "got #{tag_sets.length} tag_sets".green
34
+
35
+ # Get the first tag_set
36
+ @test_tag_set = tag_sets.first
37
+
38
+ puts "\n First TagSet: ".green
39
+ puts "#{@test_tag_set}".green
40
+
41
+ @test_tag_set_id = @test_tag_set['id']
42
+ @test_tag_set_name = @test_tag_set['name']
43
+ end
44
+
45
+
46
+ ##
47
+ # Show
48
+ #
49
+ example "Show" do
50
+ # Get the updated tag_set, including it's text
51
+ @test_tag_set = Ingenia::TagSet.get(@test_tag_set_id)
52
+ puts "\n updated tag_set:"
53
+ puts "#{@test_tag_set}".green
54
+ end
55
+
56
+
57
+ ##
58
+ # Update
59
+ #
60
+ example "Update" do
61
+ # Update its text
62
+ response = Ingenia::TagSet.update(@test_tag_set_id, :name => "updated tag_set name new")
63
+ puts "#{response}".green
64
+ end
65
+
66
+
67
+ ##
68
+ # Destroy
69
+ #
70
+ example "Destroy" do
71
+ # Remove this new tag_set
72
+ response = Ingenia::TagSet.destroy(@new_test_tag_set['id'])
73
+ puts "#{response}".green
74
+ end
75
+
@@ -0,0 +1,80 @@
1
+ require './helper'
2
+
3
+ ##
4
+ # Run a full set of API calls on tags
5
+ ##
6
+
7
+
8
+ #
9
+ # Setup
10
+ #
11
+ # Set API key to the test user for this gem
12
+ require 'ingenia_api'
13
+ #local
14
+ Ingenia::Api.api_key = "API_KEY"
15
+ Ingenia::Api.version = 2.0
16
+
17
+
18
+ ##
19
+ # Create
20
+ #
21
+ example "Create" do
22
+ # Get your tag_set
23
+ @tag_set = Ingenia::TagSet.all.first
24
+
25
+ # Create a new tag
26
+ @new_test_tag = Ingenia::Tag.create(:name => "new tag", :tag_set_id => @tag_set['id'])
27
+ puts "\n created a new tag:"
28
+ puts "#{@new_test_tag}".green
29
+ end
30
+
31
+
32
+ ##
33
+ # Index
34
+ #
35
+ example "Index" do
36
+ # Get a list of all your tags
37
+ tags = Ingenia::Tag.all
38
+ puts "got #{tags.length} tags".green
39
+
40
+ # Get the first tag
41
+ @test_tag = tags.first
42
+
43
+ puts "\n First Tag: ".green
44
+ puts "#{@test_tag}".green
45
+
46
+ @test_tag_id = @test_tag['id']
47
+ @test_tag_name = @test_tag['name']
48
+ end
49
+
50
+
51
+ ##
52
+ # Show
53
+ #
54
+ example "Show" do
55
+ # Get the updated tag, including it's text
56
+ @test_tag = Ingenia::Tag.get(@test_tag_id)
57
+ puts "\n updated tag:"
58
+ puts "#{@test_tag}".green
59
+ end
60
+
61
+
62
+ ##
63
+ # Update
64
+ #
65
+ example "Update" do
66
+ # Update its text
67
+ response = Ingenia::Tag.update(@test_tag_id, :name => "updated tag name new", :description => "this is a testing tag", :tag_set_id => @tag_set['id'])
68
+ puts "#{response}".green
69
+ end
70
+
71
+
72
+ ##
73
+ # Destroy
74
+ #
75
+ example "Destroy" do
76
+ # Remove this new tag
77
+ response = Ingenia::Tag.destroy(@new_test_tag['id'])
78
+ puts "#{response}".green
79
+ end
80
+
@@ -0,0 +1,42 @@
1
+
2
+ $: << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'yaml'
5
+ require 'ingenia_api'
6
+
7
+ def demo(name)
8
+ puts "\n\n"
9
+ puts "#\n# #{name}\n#"
10
+
11
+ output = yield
12
+
13
+ puts "output:#{output.to_yaml}"
14
+ end
15
+
16
+ def main
17
+ Ingenia::Api.api_key = "API_KEY"
18
+
19
+ # Classify some text
20
+ demo "classify" do
21
+ Ingenia::Api.classify "What kind of cheese is the best cheese?"
22
+ end
23
+
24
+ # Train text onto the default tagset
25
+ demo "train simple" do
26
+ Ingenia::Api.train "I like cheeses", [ 'food', 'preference', 'cheese' ]
27
+ end
28
+
29
+ # Train into two specific tagsets
30
+ demo "train complex" do
31
+ Ingenia::Api.train "Is it safe to eat cheese from goats?", { :Cookery => [ 'cheese', 'safety' ], :post_type => [ 'question' ] }
32
+ end
33
+
34
+ demo "find items similar to your first item" do
35
+ first_item = Ingenia::Item.all.first
36
+
37
+ puts Ingenia::Api.similar_to(:id => first_item['id'])
38
+ end
39
+
40
+ end
41
+
42
+ main if $0 == __FILE__
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'ingenia_api'
5
+ gem.authors = ['Retechnica Team']
6
+ gem.email = %w(ingenia-devs@retechnica.com)
7
+ gem.summary = 'A Ruby API client for Ingenia'
8
+ gem.description = 'A Ruby API client for Ingenia'
9
+ gem.homepage = 'http://www.ingeniapi.com'
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.test_files = gem.files.grep(%r{^(spec)/})
12
+ gem.require_paths = %w(lib)
13
+ gem.version = '1.0.6'
14
+
15
+ gem.add_runtime_dependency 'rest-client', '>= 1.8.0'
16
+ gem.add_runtime_dependency 'json', '>= 1.8.3'
17
+ gem.add_runtime_dependency 'colorize', '>= 0.7.7'
18
+
19
+ gem.add_development_dependency 'rspec', '>= 3.3.0'
20
+ gem.add_development_dependency 'webmock', '>= 1.21.0'
21
+ end
@@ -0,0 +1,126 @@
1
+
2
+ require 'restclient'
3
+ require 'json'
4
+
5
+ require 'ostruct'
6
+
7
+ require 'ingenia_api/remote'
8
+ require 'ingenia_api/item'
9
+ require 'ingenia_api/html_extractor'
10
+ require 'ingenia_api/tag'
11
+ require 'ingenia_api/tag_set'
12
+ require 'ingenia_api/bundle'
13
+
14
+ module Ingenia
15
+ module Api
16
+
17
+ extend self
18
+
19
+ API_KNOWN_PARAMS = %w( limit text )
20
+
21
+ class CallFailed < StandardError
22
+ def initialize(output)
23
+ prefix = output['status']
24
+ message = output['message']
25
+ super "#{prefix}: #{message}"
26
+ end
27
+ end
28
+
29
+ # Status of your app
30
+ def status
31
+ debug { "status" }
32
+ verify_response { Remote.get('/status', :api_key => api_key ) }
33
+ end
34
+
35
+ # Classify some text
36
+ def classify(text)
37
+ debug { "classify" }
38
+ verify_response { Remote.post('/classify', :api_key => api_key, :text => text) }
39
+ end
40
+
41
+ # Deprecated train action, now creates an item
42
+ def train(text, tags = {})
43
+ debug { "train" }
44
+ if tags.is_a? Array
45
+ Item.create(:json => { :text => text, :tags => tags })
46
+
47
+ elsif tags.is_a? Hash
48
+ Item.create(:json => { :text => text, :tag_sets => tags })
49
+
50
+ else
51
+ raise "Ingenia::Api.train(text, tags) must be called with tags argument as either an Array or a Hash"
52
+ end
53
+ end
54
+
55
+ # Find similar items
56
+ def similar_to( params = {} )
57
+ debug { "similar_to" }
58
+
59
+ initialize_params params
60
+
61
+ if params.has_key? :text
62
+ verify_response { Remote.post("/similar_to_text", @params ) }
63
+ elsif params.has_key? :tag_ids
64
+ verify_response { Remote.get("/similar_to_tags", @params ) }
65
+ end
66
+ end
67
+
68
+ # Summarize some text
69
+ def summarize(params = {})
70
+ debug { "summarize" }
71
+ initialize_params params
72
+
73
+ verify_response { Remote.post("/summarise", @params ) }
74
+ end
75
+
76
+ def trained_tags
77
+ debug { "trained_tags" }
78
+ verify_response { Remote.get('/learnt_tags', :api_key => api_key) }
79
+ end
80
+
81
+ def endpoint=(ep)
82
+ debug { "endpoint=#{Remote.endpoint}" }
83
+ Remote.endpoint = ep
84
+ end
85
+
86
+ def version=(v)
87
+ debug { "version=#{Remote.version}" }
88
+ Remote.version = v
89
+ end
90
+
91
+ def api_key=(k)
92
+ debug { "api_key=#{k}" }
93
+ @api_key = k
94
+ end
95
+
96
+ def api_key
97
+ raise 'Ingenia::Api.api_key not set' if @api_key.nil?
98
+ @api_key
99
+ end
100
+
101
+ def debug=(dbg)
102
+ @debug = dbg
103
+ debug { "debug is on" }
104
+ end
105
+
106
+ def verify_response
107
+ output = yield
108
+
109
+ return output['data'] unless output['data'].nil?
110
+
111
+ raise CallFailed.new( output )
112
+ end
113
+
114
+ private
115
+
116
+
117
+ def self.initialize_params( params = {} )
118
+ @params = { :api_key => Ingenia::Api.api_key }.merge!(params)
119
+ end
120
+
121
+ def debug
122
+ puts "Ingenia::Api.debug: #{yield}" if @debug
123
+ end
124
+
125
+ end
126
+ end