ingenia_api 1.0.6

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,62 @@
1
+ class Ingenia::Bundle
2
+ include Ingenia::Api
3
+
4
+ PATH = '/bundles'
5
+
6
+ # These are known request params, all other params will go inside the json object
7
+ BUNDLE_KNOWN_PARAMS = %i{ full_text offset limit }
8
+
9
+ # Get a single bundle by id
10
+ def self.get id, params = {}
11
+ initialize_params params
12
+
13
+ Ingenia::Api.verify_response do
14
+ Remote.get "#{PATH}/#{id}", @params
15
+ end
16
+ end
17
+
18
+ # Create a new bundle
19
+ def self.create params = {}
20
+ initialize_params params
21
+
22
+ Ingenia::Api.verify_response do
23
+ Remote.post(PATH, @params )
24
+ end
25
+ end
26
+
27
+ # Update an existing bundle
28
+ def self.update id, params = {}
29
+ initialize_params params
30
+
31
+ Ingenia::Api.verify_response do
32
+ Remote.put("#{PATH}/#{id}", @params )
33
+ end
34
+ end
35
+
36
+
37
+ # Index your bundles
38
+ def self.all params = {}
39
+ initialize_params params
40
+
41
+ Ingenia::Api.verify_response do
42
+ Remote.get(PATH, @params )
43
+ end
44
+ end
45
+
46
+ def self.destroy id
47
+ Ingenia::Api.verify_response do
48
+ Remote.delete("#{PATH}/#{id}", :params => { :api_key => Ingenia::Api.api_key} )
49
+ end
50
+ end
51
+
52
+ private
53
+ def self.initialize_params( params = {} )
54
+ # break params down into for json object and for request
55
+ request_params = params.select{ |k,v| BUNDLE_KNOWN_PARAMS.include?(k) }
56
+ json_params = params.select{ |k,v| not BUNDLE_KNOWN_PARAMS.include?(k) }
57
+
58
+ @params = { :api_key => Ingenia::Api.api_key }
59
+ @params.merge!( { :json => json_params.to_json } ) unless json_params.empty?
60
+ @params.merge! request_params
61
+ end
62
+ end
@@ -0,0 +1,24 @@
1
+ class Ingenia::HtmlExtractor
2
+ class MissingUrl < StandardError; end
3
+
4
+ include Ingenia::Api
5
+
6
+ PATH = '/html_extractor'
7
+
8
+ # Fetch the url html
9
+ def self.fetch( params = {} )
10
+ raise(MissingUrl) unless params.has_key?(:url)
11
+ initialize_params params
12
+
13
+ Ingenia::Api.verify_response do
14
+ Remote.get( "#{PATH}/fetch", @params )
15
+ end
16
+ end
17
+
18
+
19
+ private
20
+ def self.initialize_params( params = {} )
21
+ @params = params.clone
22
+ @params.merge!( { :api_key => Ingenia::Api.api_key } )
23
+ end
24
+ end
@@ -0,0 +1,99 @@
1
+ class Ingenia::Item
2
+ include Ingenia::Api
3
+
4
+ PATH = '/items'
5
+
6
+ # Get a single item by id
7
+ def self.get( id, params = {} )
8
+ initialize_params params
9
+
10
+ Ingenia::Api.verify_response do
11
+ Remote.get( "#{PATH}/#{id}", @params )
12
+ end
13
+ end
14
+
15
+ ##
16
+ # Create a new item
17
+ #
18
+ #
19
+ def self.create( params = {} )
20
+ initialize_params params
21
+
22
+ Ingenia::Api.verify_response do
23
+ Remote.post( PATH, @params )
24
+ end
25
+ end
26
+
27
+ ##
28
+ # Find or create a new item by text
29
+ #
30
+ #
31
+ def self.find_or_create_by_text( params = {} )
32
+ initialize_params params
33
+
34
+ Ingenia::Api.verify_response do
35
+ Remote.post( PATH, @params )
36
+ end
37
+ end
38
+
39
+ #
40
+ # Update an existing item
41
+ #
42
+ def self.update( id, params = {} )
43
+ initialize_params params
44
+
45
+ Ingenia::Api.verify_response do
46
+ Remote.put("#{PATH}/#{id}", @params )
47
+ end
48
+ end
49
+
50
+ #
51
+ # Scan your items
52
+ #
53
+ def self.scan params = {}
54
+ initialize_params params
55
+
56
+ Ingenia::Api.verify_response do
57
+ Remote.get( "#{PATH}/scan", @params )
58
+ end
59
+ end
60
+
61
+ #
62
+ # Index your items
63
+ #
64
+ def self.all params = {}
65
+ initialize_params params
66
+
67
+ Ingenia::Api.verify_response do
68
+ Remote.get( PATH, @params )
69
+ end
70
+ end
71
+
72
+ #
73
+ # Destroy an item
74
+ #
75
+ def self.destroy id
76
+ Ingenia::Api.verify_response do
77
+ Remote.delete("#{PATH}/#{id}", :params => { :api_key => Ingenia::Api.api_key} )
78
+ end
79
+ end
80
+
81
+ #
82
+ # Get similar items
83
+ #
84
+ def self.similar_to(id, mode='auto', params={})
85
+ initialize_params params.merge(mode: mode.to_s)
86
+
87
+ Ingenia::Api.verify_response do
88
+ Remote.get("#{PATH}/#{id}/similar_to", @params)
89
+ end
90
+ end
91
+
92
+ private
93
+ def self.initialize_params( params = {} )
94
+ @params = params.clone
95
+
96
+ @params[:json] = @params[:json].to_json if @params[:json]
97
+ @params.merge!( { :api_key => Ingenia::Api.api_key } )
98
+ end
99
+ end
@@ -0,0 +1,131 @@
1
+
2
+ module Ingenia
3
+ module Api
4
+
5
+ module Remote
6
+ ENDPOINT = 'api.ingeniapi.com'
7
+ DEFAULT_VERSION = 2.0
8
+
9
+ MAX_ATTEMPTS = 3
10
+
11
+ attr_reader :port
12
+
13
+ extend self
14
+
15
+ def get(path, opts = {})
16
+ check_params opts
17
+
18
+ handle_request do
19
+ uri = build_uri(path, opts)
20
+ JSON.parse( RestClient.get uri.to_s, :params => opts )
21
+ end
22
+ end
23
+
24
+
25
+ def post(path, opts = {})
26
+ check_params opts
27
+
28
+ handle_request do
29
+ uri = build_uri(path, opts)
30
+ JSON.parse( RestClient.post uri.to_s, opts )
31
+ end
32
+ end
33
+
34
+ def put(path, opts = {})
35
+ check_params opts
36
+
37
+ handle_request do
38
+ uri = build_uri(path, opts)
39
+ JSON.parse( RestClient.put uri.to_s, opts )
40
+ end
41
+ end
42
+
43
+ def delete(path, opts = {})
44
+ handle_request do
45
+ uri = build_uri(path, opts)
46
+ JSON.parse( RestClient.delete uri.to_s, opts )
47
+ end
48
+ end
49
+
50
+ def endpoint=(str)
51
+ if str[ ':' ]
52
+ parts = str.split(':')
53
+ @endpoint = parts[0]
54
+ @port = parts[1].to_i
55
+ return str
56
+ end
57
+
58
+ @endpoint = str
59
+ end
60
+
61
+ def version=(str)
62
+ # safety checking on version number
63
+ if str.class == String
64
+ raise 'Please set version to a float, eg 2.0'
65
+ end
66
+ @version = str
67
+ end
68
+
69
+ def endpoint
70
+ @endpoint || ENDPOINT
71
+ end
72
+
73
+ def version_string
74
+ "v#{version.round}"
75
+ end
76
+
77
+ def version
78
+ @version || DEFAULT_VERSION
79
+ end
80
+
81
+
82
+ private
83
+
84
+ def check_params param_hash
85
+ raise "missing API key" if (not param_hash.has_key?(:api_key)) || param_hash[:api_key].nil? || param_hash[:api_key].length == 0
86
+ end
87
+
88
+ def handle_request
89
+ loop do # until successful
90
+ begin
91
+ return yield
92
+
93
+ rescue RestClient::Conflict => e
94
+ return JSON.parse e.response
95
+
96
+ rescue RestClient::RequestFailed => e
97
+ if e.response.nil? || e.response.code != 429 # throttling
98
+ return { 'status' => 'error', 'message' => e.to_s }
99
+ end
100
+
101
+ rescue RestClient::BadRequest => e
102
+ if e.response.nil?
103
+ return { 'status' => 'error', 'message' => e.to_s }
104
+ else
105
+ return JSON.parse e.response
106
+ end
107
+
108
+ rescue RestClient::UnprocessableEntity => e
109
+ return JSON.parse e.response
110
+
111
+ rescue JSON::ParserError => e
112
+ # puts e.inspect
113
+
114
+ return { 'status' => 'error', 'message' => e.to_s }
115
+
116
+ end
117
+
118
+ end
119
+ end
120
+
121
+ def build_uri(path, opts = {})
122
+ path = "/#{version_string}" + path unless version.eql?(1.0)
123
+
124
+ url = URI::HTTP.build( :host => endpoint, :path => path )
125
+ url.port = port if port
126
+
127
+ url
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,72 @@
1
+ class Ingenia::Tag
2
+ include Ingenia::Api
3
+
4
+ PATH = '/tags'
5
+
6
+ # These are known request params, all other params will go inside the json object
7
+ TAG_KNOWN_PARAMS = %i{ full_text offset limit tag_ids }
8
+
9
+ # Get a single tag by id
10
+ def self.get id, params = {}
11
+ initialize_params params
12
+
13
+ Ingenia::Api.verify_response do
14
+ Remote.get "#{PATH}/#{id}", @params
15
+ end
16
+ end
17
+
18
+ # Create a new tag
19
+ def self.create params = {}
20
+ initialize_params params
21
+
22
+ Ingenia::Api.verify_response do
23
+ Remote.post(PATH, @params)
24
+ end
25
+ end
26
+
27
+ # Update an existing tag
28
+ def self.update id, params = {}
29
+ initialize_params params
30
+
31
+ Ingenia::Api.verify_response do
32
+ Remote.put("#{PATH}/#{id}", @params)
33
+ end
34
+ end
35
+
36
+ # Update an existing tag
37
+ def self.merge id, params = {}
38
+ # dirty hack
39
+ params[:tag_ids] = params[:tag_ids].to_json
40
+ initialize_params params
41
+
42
+ Ingenia::Api.verify_response do
43
+ Remote.post("#{PATH}/#{id}/merge", @params)
44
+ end
45
+ end
46
+
47
+ # Index your tags
48
+ def self.all params = {}
49
+ initialize_params params
50
+
51
+ Ingenia::Api.verify_response do
52
+ Remote.get(PATH, @params)
53
+ end
54
+ end
55
+
56
+ def self.destroy id
57
+ Ingenia::Api.verify_response do
58
+ Remote.delete("#{PATH}/#{id}", :params => { :api_key => Ingenia::Api.api_key })
59
+ end
60
+ end
61
+
62
+ private
63
+ def self.initialize_params(params = {})
64
+ # break params down into for json object and for request
65
+ request_params = params.select { |k, v| TAG_KNOWN_PARAMS.include?(k) }
66
+ json_params = params.select { |k, v| not TAG_KNOWN_PARAMS.include?(k) }
67
+
68
+ @params = { :api_key => Ingenia::Api.api_key }
69
+ @params.merge!({ :json => json_params.to_json }) unless json_params.empty?
70
+ @params.merge! request_params
71
+ end
72
+ end
@@ -0,0 +1,59 @@
1
+ class Ingenia::TagSet
2
+ include Ingenia::Api
3
+
4
+ PATH = '/tag_sets'
5
+ TAG_SET_KNOWN_PARAMS = %i{ offset limit }
6
+
7
+ # Get a single tag_set by id
8
+ def self.get id
9
+ Ingenia::Api.verify_response do
10
+ Remote.get "#{PATH}/#{id}", :api_key => Ingenia::Api.api_key
11
+ end
12
+ end
13
+
14
+ # Create a new tag_set
15
+ def self.create params = {}
16
+ initialize_params params
17
+
18
+ Ingenia::Api.verify_response do
19
+ Remote.post(PATH, @params )
20
+ end
21
+ end
22
+
23
+ # Update an existing tag_set
24
+ def self.update id, params = {}
25
+ initialize_params params
26
+
27
+ Ingenia::Api.verify_response do
28
+ Remote.put("#{PATH}/#{id}", @params )
29
+ end
30
+ end
31
+
32
+ # Index your tag_sets
33
+ def self.all params = {}
34
+ initialize_params params
35
+
36
+ Ingenia::Api.verify_response do
37
+ Remote.get(PATH, @params )
38
+ end
39
+ end
40
+
41
+ def self.destroy id
42
+ Ingenia::Api.verify_response do
43
+ Remote.delete("#{PATH}/#{id}", :params => { :api_key => Ingenia::Api.api_key} )
44
+ end
45
+ end
46
+
47
+
48
+ private
49
+ def self.initialize_params( params = {} )
50
+ # break params down into those for json object and those for request
51
+ request_params = params.select{ |k,v| TAG_SET_KNOWN_PARAMS.include?(k) }
52
+ json_params = params.select{ |k,v| not TAG_SET_KNOWN_PARAMS.include?(k) }
53
+
54
+
55
+ @params = { :api_key => Ingenia::Api.api_key }
56
+ @params.merge!( { :json => json_params.to_json } ) unless json_params.empty?
57
+ @params.merge! request_params
58
+ end
59
+ end