sensit-client 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3a562328b0100d60525a23a958f1bdab7e450943
4
+ data.tar.gz: adfde68f7e6e0e448e113b6046e9c1836626a5e1
5
+ SHA512:
6
+ metadata.gz: 86b769430ff4a0995a3af5c4357cb1312b39a04b9c8cf80e9c7dde74266b0cd8ba81241275ce2417fe4a796ab2d32f0c3531099d63bf6a7a34ca4de379c01dd9
7
+ data.tar.gz: e002fa8bb5477c9e7b932cb2681cd8d14871db304e982c2b623cf72be9babb052f1dbdbe0d0b02f5b2e1305913617df4268e3a9a8a445844de335430887b625c
@@ -0,0 +1,45 @@
1
+ module Sensit
2
+
3
+ module Api
4
+
5
+ # Get the value of a specific field within a feed
6
+ #
7
+ # topic_id - The key for the parent topic
8
+ # feed_id - The id of the parent feed
9
+ # id - The key of the specific field
10
+ class Data
11
+
12
+ def initialize(topic_id, feed_id, id, client)
13
+ @topic_id = topic_id
14
+ @feed_id = feed_id
15
+ @id = id
16
+ @client = client
17
+ end
18
+
19
+ # Requires authorization of **read_any_data**, or **read_application_data**.
20
+ # '/topics/:topic_id/feeds/:feed_id/data/:id' GET
21
+ #
22
+ def find(options = {})
23
+ body = options.has_key?(:query) ? options[:query] : {}
24
+
25
+ response = @client.get "/topics/#{@topic_id}/feeds/#{@feed_id}/data/#{@id}", body, options
26
+
27
+ return response
28
+ end
29
+
30
+ # Update a specific value of a field within a feed with the data passed in. Requires authorization of **read_any_data**, or **read_application_data**.
31
+ # '/topics/:topic_id/feeds/:feed_id/data/:id' PUT
32
+ #
33
+ def update(options = {})
34
+ body = options.has_key?(:body) ? options[:body] : {}
35
+
36
+ response = @client.put "/topics/#{@topic_id}/feeds/#{@feed_id}/data/#{@id}", body, options
37
+
38
+ return response
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,80 @@
1
+ module Sensit
2
+
3
+ module Api
4
+
5
+ # Returns api instance to get auxilary information about Buffer useful when creating your app.
6
+ #
7
+ # topic_id - The key for the parent topic
8
+ # id - The id of the feed
9
+ class Feed
10
+
11
+ def initialize(topic_id, id, client)
12
+ @topic_id = topic_id
13
+ @id = id
14
+ @client = client
15
+ end
16
+
17
+ # Returns a list of feeds for a given topic. Requires authorization of **read_any_data**, or **read_application_data**.
18
+ # '/topics/:topic_id/feeds' GET
19
+ #
20
+ def list(options = {})
21
+ body = options.has_key?(:query) ? options[:query] : {}
22
+
23
+ response = @client.get "/topics/#{@topic_id}/feeds", body, options
24
+
25
+ return response
26
+ end
27
+
28
+ # Returns a specific feed for a topic. Requires authorization of **read_any_data**, or **read_application_data**.
29
+ # '/topics/:topic_id/feeds/:id' GET
30
+ #
31
+ def find(options = {})
32
+ body = options.has_key?(:query) ? options[:query] : {}
33
+
34
+ response = @client.get "/topics/#{@topic_id}/feeds/#{@id}", body, options
35
+
36
+ return response
37
+ end
38
+
39
+ # Create a feed on a given topic. Requires authorization of **read_any_data**, or **read_application_data**.
40
+ # '/topics/:topic_id/feeds' POST
41
+ #
42
+ # data - A hash of data to be stored
43
+ def create(data, options = {})
44
+ body = options.has_key?(:body) ? options[:body] : {}
45
+ body[:data] = data
46
+
47
+ response = @client.post "/topics/#{@topic_id}/feeds", body, options
48
+
49
+ return response
50
+ end
51
+
52
+ # Update an associated Feed to the Topic. Requires authorization of **read_any_data**, or **read_application_data**.
53
+ # '/topics/:topic_id/feeds/:id' PUT
54
+ #
55
+ # data - A hash of data to be stored
56
+ def update(data, options = {})
57
+ body = options.has_key?(:body) ? options[:body] : {}
58
+ body[:data] = data
59
+
60
+ response = @client.put "/topics/#{@topic_id}/feeds/#{@id}", body, options
61
+
62
+ return response
63
+ end
64
+
65
+ # Deletes the desired feed. Requires authorization of **read_any_data**, or **read_application_data**.
66
+ # '/topics/:topic_id/feeds/:id' DELETE
67
+ #
68
+ def delete(options = {})
69
+ body = options.has_key?(:body) ? options[:body] : {}
70
+
71
+ response = @client.delete "/topics/#{@topic_id}/feeds/#{@id}", body, options
72
+
73
+ return response
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+
80
+ end
@@ -0,0 +1,82 @@
1
+ module Sensit
2
+
3
+ module Api
4
+
5
+ # .
6
+ #
7
+ # topic_id - The key for the parent topic
8
+ # id - Username of the user
9
+ class Field
10
+
11
+ def initialize(topic_id, id, client)
12
+ @topic_id = topic_id
13
+ @id = id
14
+ @client = client
15
+ end
16
+
17
+ # Get all the fields associated with a topic. Requires authorization of **read_any_data**, or **read_application_data**
18
+ # '/topics/:topic_id/fields' GET
19
+ #
20
+ def list(options = {})
21
+ body = options.has_key?(:query) ? options[:query] : {}
22
+
23
+ response = @client.get "/topics/#{@topic_id}/fields", body, options
24
+
25
+ return response
26
+ end
27
+
28
+ # Get a Field of the associated a topic and Id. Requires authorization of **read_any_data**, or **read_application_data**
29
+ # '/topics/:topic_id/fields/:id' GET
30
+ #
31
+ def find(options = {})
32
+ body = options.has_key?(:query) ? options[:query] : {}
33
+
34
+ response = @client.get "/topics/#{@topic_id}/fields/#{@id}", body, options
35
+
36
+ return response
37
+ end
38
+
39
+ # Adds a new field that feed data can be added too. Requires authorization of **manage_any_data**, or **manage_application_data**
40
+ # '/topics/:topic_id/fields' POST
41
+ #
42
+ # name - The descriptive name of the field.
43
+ # key - The name that the is used to identify the field in a feed
44
+ def create(name, key, options = {})
45
+ body = options.has_key?(:body) ? options[:body] : {}
46
+ body[:name] = name
47
+ body[:key] = key
48
+
49
+ response = @client.post "/topics/#{@topic_id}/fields", body, options
50
+
51
+ return response
52
+ end
53
+
54
+ # Updates the Field data and makes the corresponding changes in the index. Requires authorization of **manage_any_data**, or **manage_application_data**
55
+ # '/api/topics/:topic_id/fields/:id' PUT
56
+ #
57
+ # name - The descriptive name of the field.
58
+ def update(name, options = {})
59
+ body = options.has_key?(:body) ? options[:body] : {}
60
+ body[:name] = name
61
+
62
+ response = @client.put "/api/topics/#{@topic_id}/fields/#{@id}", body, options
63
+
64
+ return response
65
+ end
66
+
67
+ # Deletes a field and the feed data in that field. Requires authorization of **manage_any_data**, or **manage_application_data**
68
+ # '/api/topics/:topic_id/fields/:id' DELETE
69
+ #
70
+ def delete(options = {})
71
+ body = options.has_key?(:body) ? options[:body] : {}
72
+
73
+ response = @client.delete "/api/topics/#{@topic_id}/fields/#{@id}", body, options
74
+
75
+ return response
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -0,0 +1,82 @@
1
+ module Sensit
2
+
3
+ module Api
4
+
5
+ # A **Percolator** is a reverse query much like a match rule which is run whenever a new feed is added. These can be used to create alerts by causing the sensit to publish the feed that was just added. A percolator query is defined by a `name` and and valid `query` according to the according the the [elasticsearch Query DSL](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html). For more information about Percolator queries please refer to the [elasticsearch percolator documentation](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-percolate.html).
6
+ #
7
+ # topic_id - The key for the parent topic
8
+ # id - The name of the percolator query
9
+ class Percolator
10
+
11
+ def initialize(topic_id, id, client)
12
+ @topic_id = topic_id
13
+ @id = id
14
+ @client = client
15
+ end
16
+
17
+ # Returns a list or percolators for a given topic. Requires authorization of **read_any_percolators**, or **read_application_percolators**.
18
+ # '/topics/:topic_id/percolators' GET
19
+ #
20
+ def list(options = {})
21
+ body = options.has_key?(:query) ? options[:query] : {}
22
+
23
+ response = @client.get "/topics/#{@topic_id}/percolators", body, options
24
+
25
+ return response
26
+ end
27
+
28
+ # Return a specific percolator of the associated Topic by Id. Requires authorization of **read_any_percolators**, or **read_application_percolators**.
29
+ # '/topics/:topic_id/percolators/:id' GET
30
+ #
31
+ def find(options = {})
32
+ body = options.has_key?(:query) ? options[:query] : {}
33
+
34
+ response = @client.get "/topics/#{@topic_id}/percolators/#{@id}", body, options
35
+
36
+ return response
37
+ end
38
+
39
+ # Create a percolator on the associated Topic with the specified name and query. Requires authorization of **manage_any_percolators**, or **manage_application_percolators**.
40
+ # '/topics/:topic_id/percolators' POST
41
+ #
42
+ # name - The time zone of the time. Defaults to UTC
43
+ # query - A hash of data to be stored
44
+ def create(name, query, options = {})
45
+ body = options.has_key?(:body) ? options[:body] : {}
46
+ body[:name] = name
47
+ body[:query] = query
48
+
49
+ response = @client.post "/topics/#{@topic_id}/percolators", body, options
50
+
51
+ return response
52
+ end
53
+
54
+ # Update the query for a specific percolator. Requires authorization of **manage_any_percolators**, or **manage_application_percolators**.
55
+ # '/topics/:topic_id/percolators/:id' PUT
56
+ #
57
+ # query - A hash of data to be stored
58
+ def update(query, options = {})
59
+ body = options.has_key?(:body) ? options[:body] : {}
60
+ body[:query] = query
61
+
62
+ response = @client.put "/topics/#{@topic_id}/percolators/#{@id}", body, options
63
+
64
+ return response
65
+ end
66
+
67
+ # Delete a percolator on the associated topic. Requires authorization of **manage_any_percolators**, or **manage_application_percolators**.
68
+ # '/topics/:topic_id/percolators/:id' DELETE
69
+ #
70
+ def delete(options = {})
71
+ body = options.has_key?(:body) ? options[:body] : {}
72
+
73
+ response = @client.delete "/topics/#{@topic_id}/percolators/#{@id}", body, options
74
+
75
+ return response
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -0,0 +1,88 @@
1
+ module Sensit
2
+
3
+ module Api
4
+
5
+ # Reports are stored filter and facet queries on the **Feed** data. A report is a assigned a `name` and the `query` is any elasticsearch query which filters only the desired data for the facets (See the [elasticsearch Query DSL](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-queries.html) for valid queries). A report can have many `facets` with each facet is referred to by a user defined `name`. Valid `type`'s of facet include **terms**, **range**, **histogram**, **filter**, **statistical**, **query**, **terms_stats**, or **geo_distance**. The `query` within a facet defines the field counts or statistics which the data is calculated over. See the [elasticsearch facet dsl](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets.html) for information about the various facet types and valid query fields.
6
+ #
7
+ # topic_id - The key for the parent topic
8
+ # id - The identifier of the report
9
+ class Report
10
+
11
+ def initialize(topic_id, id, client)
12
+ @topic_id = topic_id
13
+ @id = id
14
+ @client = client
15
+ end
16
+
17
+ # Get all reports for the associated Topic. Requires authorization of **read_any_reports**, or **read_application_reports**.
18
+ # '/topics/:topic_id/reports' GET
19
+ #
20
+ def list(options = {})
21
+ body = options.has_key?(:query) ? options[:query] : {}
22
+
23
+ response = @client.get "/topics/#{@topic_id}/reports", body, options
24
+
25
+ return response
26
+ end
27
+
28
+ # Retrieve a specific report on the associated topic by Id. Requires authorization of **read_any_reports**, or **read_application_reports**.
29
+ # '/topics/:topic_id/reports/:id' GET
30
+ #
31
+ def find(options = {})
32
+ body = options.has_key?(:query) ? options[:query] : {}
33
+
34
+ response = @client.get "/topics/#{@topic_id}/reports/#{@id}", body, options
35
+
36
+ return response
37
+ end
38
+
39
+ # Create a new report on the associated Topic which can be easily retrieved later using an id. Requires authorization of **manage_any_reports**, or **manage_application_reports**.
40
+ # '/topics/:topic_id/reports' POST
41
+ #
42
+ # name - The name of the report.
43
+ # query - The search query to filter the data for the facet
44
+ # facets - An array of facet hashes which each contain a `name` ad type of the facet along with its query hash.
45
+ def create(name, query, facets, options = {})
46
+ body = options.has_key?(:body) ? options[:body] : {}
47
+ body[:name] = name
48
+ body[:query] = query
49
+ body[:facets] = facets
50
+
51
+ response = @client.post "/topics/#{@topic_id}/reports", body, options
52
+
53
+ return response
54
+ end
55
+
56
+ # Update the query, facets or name of the report. Requires authorization of **manage_any_reports**, or **manage_application_reports**.
57
+ # '/topics/:topic_id/reports/:id' PUT
58
+ #
59
+ # name - The name of the report.
60
+ # query - The search query to filter the data for the facet
61
+ # facets - An array of facet hashes which each contain a `name` ad type of the facet along with its query hash.
62
+ def update(name, query, facets, options = {})
63
+ body = options.has_key?(:body) ? options[:body] : {}
64
+ body[:name] = name
65
+ body[:query] = query
66
+ body[:facets] = facets
67
+
68
+ response = @client.put "/topics/#{@topic_id}/reports/#{@id}", body, options
69
+
70
+ return response
71
+ end
72
+
73
+ # Remove a saved report on the associated Topic by Id. Requires authorization of **manage_any_reports**, or **manage_application_reports**.
74
+ # '/topics/:topic_id/reports/:id' DELETE
75
+ #
76
+ def delete(options = {})
77
+ body = options.has_key?(:body) ? options[:body] : {}
78
+
79
+ response = @client.delete "/topics/#{@topic_id}/reports/#{@id}", body, options
80
+
81
+ return response
82
+ end
83
+
84
+ end
85
+
86
+ end
87
+
88
+ end
@@ -0,0 +1,86 @@
1
+ module Sensit
2
+
3
+ module Api
4
+
5
+ # Subscriptions allows feed data to imported using a socket rather than just using the Feed REST API. By creating a subscription sensit will start to listen for feed data being imported using the specified `host` and while using the topic name as the `channel` name.
6
+ #
7
+ # id - The identifier for the subscription
8
+ class Subscription
9
+
10
+ def initialize(id, client)
11
+ @id = id
12
+ @client = client
13
+ end
14
+
15
+ # Get the list of all subscriptions for importing feed data to the associated topics. Requires authorization of **read_any_subscriptions**, or **read_application_subscriptions**.
16
+ # '/subscriptions' GET
17
+ #
18
+ def list(options = {})
19
+ body = options.has_key?(:query) ? options[:query] : {}
20
+
21
+ response = @client.get "/subscriptions", body, options
22
+
23
+ return response
24
+ end
25
+
26
+ # Get the information of a specific subscription. Requires authorization of **read_any_subscriptions**, or **read_application_subscriptions**.
27
+ # '/subscriptions/:id' GET
28
+ #
29
+ def find(options = {})
30
+ body = options.has_key?(:query) ? options[:query] : {}
31
+
32
+ response = @client.get "/subscriptions/#{@id}", body, options
33
+
34
+ return response
35
+ end
36
+
37
+ # Create a subscription which will connect to the server and listen for feed data for any of the associated topics. Requires authorization of **manage_any_subscriptions**, or **manage_application_subscriptions**.
38
+ # '/subscriptions' POST
39
+ #
40
+ # name - The channel or name to identify the subscription.
41
+ # host - The ip address or host of the connection
42
+ # protocol - the protocol to comminivate over
43
+ def create(name, host, protocol, options = {})
44
+ body = options.has_key?(:body) ? options[:body] : {}
45
+ body[:name] = name
46
+ body[:host] = host
47
+ body[:protocol] = protocol
48
+
49
+ response = @client.post "/subscriptions", body, options
50
+
51
+ return response
52
+ end
53
+
54
+ # Returns an object with the current configuration that Buffer is using, including supported services, their icons and the varying limits of character and schedules. Requires authorization of **manage_any_subscriptions**, or **manage_application_subscriptions**.
55
+ # '/subscriptions/:id' PUT
56
+ #
57
+ # name - The channel or name to identify the subscription.
58
+ # host - The ip address or host of the connection
59
+ # protocol - the protocol to comminivate over
60
+ def update(name, host, protocol, options = {})
61
+ body = options.has_key?(:body) ? options[:body] : {}
62
+ body[:name] = name
63
+ body[:host] = host
64
+ body[:protocol] = protocol
65
+
66
+ response = @client.put "/subscriptions/#{@id}", body, options
67
+
68
+ return response
69
+ end
70
+
71
+ # Delete the subscription and stop listening for feed data for the associated topics. Requires authorization of **manage_any_subscriptions**, or **manage_application_subscriptions**.
72
+ # '/subscriptions/:id' DELETE
73
+ #
74
+ def delete(options = {})
75
+ body = options.has_key?(:body) ? options[:body] : {}
76
+
77
+ response = @client.delete "/subscriptions/#{@id}", body, options
78
+
79
+ return response
80
+ end
81
+
82
+ end
83
+
84
+ end
85
+
86
+ end
@@ -0,0 +1,76 @@
1
+ module Sensit
2
+
3
+ module Api
4
+
5
+ # A topic is root that data is attached to. It is the equivalent of a source in searchlight/solink and acts as a table which has columns(Fields) and rows(Feeds).
6
+ #
7
+ class Topic
8
+
9
+ def initialize(client)
10
+ @client = client
11
+ end
12
+
13
+ # Requires authorization of **read_any_data**, or **read_application_data**.
14
+ # '/topics' GET
15
+ #
16
+ def list(options = {})
17
+ body = options.has_key?(:query) ? options[:query] : {}
18
+
19
+ response = @client.get "/topics", body, options
20
+
21
+ return response
22
+ end
23
+
24
+ # Requires authorization of **read_any_data**, or **read_application_data**.
25
+ # '/topics/:id' GET
26
+ #
27
+ def find(options = {})
28
+ body = options.has_key?(:query) ? options[:query] : {}
29
+
30
+ response = @client.get "/topics/:id", body, options
31
+
32
+ return response
33
+ end
34
+
35
+ # Requires authorization of **manage_any_data**, or **manage_application_data**.
36
+ # '/topics' POST
37
+ #
38
+ # name - The name and id of the topic.
39
+ def create(name, options = {})
40
+ body = options.has_key?(:body) ? options[:body] : {}
41
+ body[:name] = name
42
+
43
+ response = @client.post "/topics", body, options
44
+
45
+ return response
46
+ end
47
+
48
+ # Requires authorization of **manage_any_data**, or **manage_application_data**.
49
+ # '/topics/:id' PUT
50
+ #
51
+ # name - The name and id of the topic.
52
+ def update(name, options = {})
53
+ body = options.has_key?(:body) ? options[:body] : {}
54
+ body[:name] = name
55
+
56
+ response = @client.put "/topics/:id", body, options
57
+
58
+ return response
59
+ end
60
+
61
+ # Requires authorization of **manage_any_data**, or **manage_application_data**.
62
+ # '/topics/:id' DELETE
63
+ #
64
+ def delete(options = {})
65
+ body = options.has_key?(:body) ? options[:body] : {}
66
+
67
+ response = @client.delete "/topics/:id", body, options
68
+
69
+ return response
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+
76
+ end
@@ -0,0 +1,28 @@
1
+ module Sensit
2
+
3
+ module Api
4
+
5
+ # <no value>
6
+ #
7
+ class User
8
+
9
+ def initialize(client)
10
+ @client = client
11
+ end
12
+
13
+ # <no value>
14
+ # '/user' GET
15
+ #
16
+ def profile(options = {})
17
+ body = options.has_key?(:query) ? options[:query] : {}
18
+
19
+ response = @client.get "/user", body, options
20
+
21
+ return response
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,83 @@
1
+ require "faraday"
2
+ require "json"
3
+
4
+ require "sensit/api/user"
5
+ require "sensit/api/topic"
6
+ require "sensit/api/feed"
7
+ require "sensit/api/data"
8
+ require "sensit/api/percolator"
9
+ require "sensit/api/report"
10
+ require "sensit/api/subscription"
11
+ require "sensit/api/field"
12
+
13
+ module Sensit
14
+
15
+ class Client
16
+
17
+ def initialize(auth = {}, options = {})
18
+ @http_client = Sensit::HttpClient::HttpClient.new auth, options
19
+ end
20
+
21
+ # <no value>
22
+ #
23
+ def user()
24
+ Sensit::Api::User.new @http_client
25
+ end
26
+
27
+ # A topic is root that data is attached to. It is the equivalent of a source in searchlight/solink and acts as a table which has columns(Fields) and rows(Feeds).
28
+ #
29
+ def topic()
30
+ Sensit::Api::Topic.new @http_client
31
+ end
32
+
33
+ # Returns api instance to get auxilary information about Buffer useful when creating your app.
34
+ #
35
+ # topic_id - The key for the parent topic
36
+ # id - The id of the feed
37
+ def feed(topic_id, id)
38
+ Sensit::Api::Feed.new topic_id, id, @http_client
39
+ end
40
+
41
+ # Get the value of a specific field within a feed
42
+ #
43
+ # topic_id - The key for the parent topic
44
+ # feed_id - The id of the parent feed
45
+ # id - The key of the specific field
46
+ def data(topic_id, feed_id, id)
47
+ Sensit::Api::Data.new topic_id, feed_id, id, @http_client
48
+ end
49
+
50
+ # A **Percolator** is a reverse query much like a match rule which is run whenever a new feed is added. These can be used to create alerts by causing the sensit to publish the feed that was just added. A percolator query is defined by a `name` and and valid `query` according to the according the the [elasticsearch Query DSL](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html). For more information about Percolator queries please refer to the [elasticsearch percolator documentation](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-percolate.html).
51
+ #
52
+ # topic_id - The key for the parent topic
53
+ # id - The name of the percolator query
54
+ def percolator(topic_id, id)
55
+ Sensit::Api::Percolator.new topic_id, id, @http_client
56
+ end
57
+
58
+ # Reports are stored filter and facet queries on the **Feed** data. A report is a assigned a `name` and the `query` is any elasticsearch query which filters only the desired data for the facets (See the [elasticsearch Query DSL](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-queries.html) for valid queries). A report can have many `facets` with each facet is referred to by a user defined `name`. Valid `type`'s of facet include **terms**, **range**, **histogram**, **filter**, **statistical**, **query**, **terms_stats**, or **geo_distance**. The `query` within a facet defines the field counts or statistics which the data is calculated over. See the [elasticsearch facet dsl](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets.html) for information about the various facet types and valid query fields.
59
+ #
60
+ # topic_id - The key for the parent topic
61
+ # id - The identifier of the report
62
+ def report(topic_id, id)
63
+ Sensit::Api::Report.new topic_id, id, @http_client
64
+ end
65
+
66
+ # Subscriptions allows feed data to imported using a socket rather than just using the Feed REST API. By creating a subscription sensit will start to listen for feed data being imported using the specified `host` and while using the topic name as the `channel` name.
67
+ #
68
+ # id - The identifier for the subscription
69
+ def subscription(id)
70
+ Sensit::Api::Subscription.new id, @http_client
71
+ end
72
+
73
+ # .
74
+ #
75
+ # topic_id - The key for the parent topic
76
+ # id - Username of the user
77
+ def field(topic_id, id)
78
+ Sensit::Api::Field.new topic_id, id, @http_client
79
+ end
80
+
81
+ end
82
+
83
+ end
@@ -0,0 +1,18 @@
1
+ module Sensit
2
+
3
+ module Error
4
+
5
+ class ClientError < ::StandardError
6
+
7
+ attr_reader :code
8
+
9
+ def initialize(message, code)
10
+ @code = code
11
+ super message
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,9 @@
1
+ require "sensit/error/client_error"
2
+
3
+ module Sensit
4
+
5
+ module Error
6
+
7
+ end
8
+
9
+ end
@@ -0,0 +1,91 @@
1
+ require "base64"
2
+
3
+ module Sensit
4
+
5
+ module HttpClient
6
+
7
+ # AuthHandler takes care of devising the auth type and using it
8
+ class AuthHandler < Faraday::Middleware
9
+
10
+ URL_SECRET = 2
11
+ URL_TOKEN = 3
12
+
13
+ def initialize(app, auth = {}, options = {})
14
+ @auth = auth
15
+ super(app)
16
+ end
17
+
18
+ def call(env)
19
+ if !@auth.empty?
20
+ auth = get_auth_type
21
+ flag = false
22
+
23
+ if auth == URL_SECRET
24
+ env = url_secret env
25
+ flag = true
26
+ end
27
+
28
+ if auth == URL_TOKEN
29
+ env = url_token env
30
+ flag = true
31
+ end
32
+
33
+ if !flag
34
+ raise StandardError.new "Unable to calculate authorization method. Please check"
35
+ end
36
+ end
37
+
38
+ @app.call(env)
39
+ end
40
+
41
+ # Calculating the Authentication Type
42
+ def get_auth_type()
43
+
44
+ if @auth.has_key?(:client_id) and @auth.has_key?(:client_secret)
45
+ return URL_SECRET
46
+ end
47
+
48
+ if @auth.has_key?(:access_token)
49
+ return URL_TOKEN
50
+ end
51
+
52
+ return -1
53
+ end
54
+
55
+ # OAUTH2 Authorization with client secret
56
+ def url_secret(env)
57
+ query = {
58
+ :client_id => @auth[:client_id],
59
+ :client_secret => @auth[:client_secret]
60
+ }
61
+
62
+ merge_query env, query
63
+ end
64
+
65
+ # OAUTH2 Authorization with access token
66
+ def url_token(env)
67
+ query = { :access_token => @auth[:access_token] }
68
+
69
+ merge_query env, query
70
+ end
71
+
72
+ def query_params(url)
73
+ if url.query.nil? or url.query.empty?
74
+ {}
75
+ else
76
+ Faraday::Utils.parse_query url.query
77
+ end
78
+ end
79
+
80
+ def merge_query(env, query)
81
+ query = query.update query_params(env[:url])
82
+
83
+ env[:url].query = Faraday::Utils.build_query query
84
+
85
+ return env
86
+ end
87
+ end
88
+
89
+ end
90
+
91
+ end
@@ -0,0 +1,51 @@
1
+ module Sensit
2
+
3
+ module HttpClient
4
+
5
+ # ErrorHanlder takes care of selecting the error message from response body
6
+ class ErrorHandler < Faraday::Middleware
7
+
8
+ def initialize(app)
9
+ super(app)
10
+ end
11
+
12
+ def call(env)
13
+ @app.call(env).on_complete do |env|
14
+ code = env[:response].status
15
+ type = env[:response].headers["content-type"]
16
+
17
+ case code
18
+ when 500...599
19
+ raise Sensit::Error::ClientError.new "Error #{code}", code
20
+ when 400...499
21
+ body = Sensit::HttpClient::ResponseHandler.get_body env[:response]
22
+ message = ""
23
+
24
+ # If HTML, whole body is taken
25
+ if body.is_a? String
26
+ message = body
27
+ end
28
+
29
+ # If JSON, a particular field is taken and used
30
+ if type.include?("json") and body.is_a?(Hash)
31
+ if body.has_key? "error"
32
+ message = body["error"]
33
+ else
34
+ message = "Unable to select error message from json returned by request responsible for error"
35
+ end
36
+ end
37
+
38
+ if message == ""
39
+ message = "Unable to understand the content type of response returned by request responsible for error"
40
+ end
41
+
42
+ raise Sensit::Error::ClientError.new message, code
43
+ end
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,36 @@
1
+ module Sensit
2
+
3
+ module HttpClient
4
+
5
+ # RequestHandler takes care of encoding the request body into format given by options
6
+ class RequestHandler
7
+
8
+ def self.set_body(options)
9
+ type = options.has_key?(:request_type) ? options[:request_type] : "form"
10
+
11
+ # Encoding request body into JSON format
12
+ if type == "json"
13
+ options[:body] = options[:body].to_json
14
+ options[:headers]["content-type"] = "application/json"
15
+ end
16
+
17
+ # Encoding body into form-urlencoded format
18
+ if type == "form"
19
+ options[:body] = Faraday::Utils::ParamsHash[options[:body]].to_query
20
+ options[:headers]["content-type"] = "application/x-www-form-urlencoded"
21
+ end
22
+
23
+ # Raw body
24
+ if type == "raw"
25
+ options[:body] = options[:body].is_a?(Hash) ? "" : options[:body]
26
+ options[:headers].delete "content-type"
27
+ end
28
+
29
+ return options
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,20 @@
1
+ module Sensit
2
+
3
+ module HttpClient
4
+
5
+ # Response object contains the response returned by the client
6
+ class Response
7
+
8
+ attr_accessor :body, :code, :headers
9
+
10
+ def initialize(body, code, headers)
11
+ @body = body
12
+ @code = code
13
+ @headers = headers
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,24 @@
1
+ module Sensit
2
+
3
+ module HttpClient
4
+
5
+ # ResponseHandler takes care of decoding the response body into suitable type
6
+ class ResponseHandler
7
+
8
+ def self.get_body(response)
9
+ type = response.headers["content-type"]
10
+ body = response.body
11
+
12
+ # Response body is in JSON
13
+ if type.include? "json"
14
+ body = JSON.parse body
15
+ end
16
+
17
+ body
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,123 @@
1
+ require "sensit/http_client/auth_handler"
2
+ require "sensit/http_client/error_handler"
3
+ require "sensit/http_client/request_handler"
4
+ require "sensit/http_client/response"
5
+ require "sensit/http_client/response_handler"
6
+
7
+ module Sensit
8
+
9
+ module HttpClient
10
+
11
+ # Main HttpClient which is used by Api classes
12
+ class HttpClient
13
+
14
+ attr_accessor :options, :headers
15
+
16
+ def initialize(auth = {}, options = {})
17
+
18
+ if auth.is_a? String
19
+ auth = { :access_token => auth }
20
+ end
21
+
22
+ @options = {
23
+ :base => "http://sensit.herokuapp.com/api",
24
+ :api_version => "1",
25
+ :user_agent => "alpaca/0.2.0 (https://github.com/pksunkara/alpaca)"
26
+ }
27
+
28
+ @options.update options
29
+
30
+ @headers = {
31
+ "user-agent" => @options[:user_agent]
32
+ }
33
+
34
+ if @options.has_key? :headers
35
+ @headers.update Hash[@options[:headers].map { |k, v| [k.downcase, v] }]
36
+ @options.delete :headers
37
+ end
38
+
39
+ @client = Faraday.new @options[:base] do |conn|
40
+ conn.use Sensit::HttpClient::AuthHandler, auth
41
+ conn.use Sensit::HttpClient::ErrorHandler
42
+
43
+ conn.adapter Faraday.default_adapter
44
+ end
45
+ end
46
+
47
+ def get(path, params = {}, options = {})
48
+ request path, nil, "get", options.merge({ :query => params })
49
+ end
50
+
51
+ def post(path, body = {}, options = {})
52
+ request path, body, "post", options
53
+ end
54
+
55
+ def patch(path, body = {}, options = {})
56
+ request path, body, "patch", options
57
+ end
58
+
59
+ def delete(path, body = {}, options = {})
60
+ request path, body, "delete", options
61
+ end
62
+
63
+ def put(path, body = {}, options = {})
64
+ request path, body, "put", options
65
+ end
66
+
67
+ # Intermediate function which does three main things
68
+ #
69
+ # - Transforms the body of request into correct format
70
+ # - Creates the requests with give parameters
71
+ # - Returns response body after parsing it into correct format
72
+ def request(path, body, method, options)
73
+ options = @options.merge options
74
+
75
+ options[:headers] = options[:headers] || {}
76
+ options[:headers] = @headers.merge Hash[options[:headers].map { |k, v| [k.downcase, v] }]
77
+
78
+ options[:body] = body
79
+
80
+ if method != "get"
81
+ options[:body] = options[:body] || {}
82
+ options = set_body options
83
+ end
84
+
85
+ response = create_request method, path, options
86
+
87
+ body = get_body response
88
+
89
+ Sensit::HttpClient::Response.new body, response.status, response.headers
90
+ end
91
+
92
+ # Creating a request with the given arguments
93
+ #
94
+ # If api_version is set, appends it immediately after host
95
+ def create_request(method, path, options)
96
+ version = options.has_key?(:api_version) ? "/#{options[:api_version]}" : ""
97
+
98
+ path = "#{version}#{path}"
99
+
100
+ instance_eval <<-RUBY, __FILE__, __LINE__ + 1
101
+ @client.#{method} path do |req|
102
+ req.body = options[:body]
103
+ req.headers.update(options[:headers])
104
+ req.params.update(options[:query]) if options[:query]
105
+ end
106
+ RUBY
107
+ end
108
+
109
+ # Get response body in correct format
110
+ def get_body(response)
111
+ Sensit::HttpClient::ResponseHandler.get_body response
112
+ end
113
+
114
+ # Set request body in correct format
115
+ def set_body(options)
116
+ Sensit::HttpClient::RequestHandler.set_body options
117
+ end
118
+
119
+ end
120
+
121
+ end
122
+
123
+ end
@@ -0,0 +1,8 @@
1
+ require "rubygems"
2
+
3
+ require "sensit/client"
4
+ require "sensit/error"
5
+ require "sensit/http_client"
6
+
7
+ module Sensit
8
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensit-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Waddington
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-18 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.8.8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.8
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.7.7
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.7.7
41
+ description: Official Sensit API library client for ruby
42
+ email: cwadding@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/sensit-client.rb
48
+ - lib/sensit/api/data.rb
49
+ - lib/sensit/api/feed.rb
50
+ - lib/sensit/api/field.rb
51
+ - lib/sensit/api/percolator.rb
52
+ - lib/sensit/api/report.rb
53
+ - lib/sensit/api/subscription.rb
54
+ - lib/sensit/api/topic.rb
55
+ - lib/sensit/api/user.rb
56
+ - lib/sensit/client.rb
57
+ - lib/sensit/error.rb
58
+ - lib/sensit/error/client_error.rb
59
+ - lib/sensit/http_client.rb
60
+ - lib/sensit/http_client/auth_handler.rb
61
+ - lib/sensit/http_client/error_handler.rb
62
+ - lib/sensit/http_client/request_handler.rb
63
+ - lib/sensit/http_client/response.rb
64
+ - lib/sensit/http_client/response_handler.rb
65
+ homepage: https://sensit.com
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.2.1
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Official Sensit API library client for ruby
89
+ test_files: []