da_face 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +51 -0
- data/LICENSE.txt +22 -0
- data/README.md +114 -0
- data/Rakefile +2 -0
- data/da_face.gemspec +27 -0
- data/lib/da_face/api/adapter.rb +42 -0
- data/lib/da_face/api/adapters/base.rb +59 -0
- data/lib/da_face/api/adapters/em_http_request_adapter.rb +8 -0
- data/lib/da_face/api/adapters/excon_adapter.rb +33 -0
- data/lib/da_face/api/core.rb +0 -0
- data/lib/da_face/api/push.rb +77 -0
- data/lib/da_face/api/push_log.rb +11 -0
- data/lib/da_face/api/push_log_entry.rb +15 -0
- data/lib/da_face/api/push_subscription.rb +141 -0
- data/lib/da_face/configuration.rb +11 -0
- data/lib/da_face/datasift/da_object.rb +35 -0
- data/lib/da_face/datasift/demographic.rb +12 -0
- data/lib/da_face/datasift/errors.rb +9 -0
- data/lib/da_face/datasift/interaction.rb +33 -0
- data/lib/da_face/datasift/language.rb +14 -0
- data/lib/da_face/datasift/link.rb +34 -0
- data/lib/da_face/datasift/links.rb +38 -0
- data/lib/da_face/datasift/parser.rb +35 -0
- data/lib/da_face/datasift/salience.rb +16 -0
- data/lib/da_face/datasift/twitter.rb +46 -0
- data/lib/da_face/datasift/twitter_delete_notification.rb +16 -0
- data/lib/da_face/datasift/twitter_user_status.rb +37 -0
- data/lib/da_face/errors.rb +7 -0
- data/lib/da_face/twitter/parser.rb +16 -0
- data/lib/da_face/twitter/tweet.rb +49 -0
- data/lib/da_face/twitter/user.rb +55 -0
- data/lib/da_face/utilities.rb +41 -0
- data/lib/da_face/version.rb +3 -0
- data/lib/da_face.rb +69 -0
- data/spec/da_face/api/adapter_spec.rb +58 -0
- data/spec/da_face/api/adapters/base_spec.rb +125 -0
- data/spec/da_face/api/push_log_spec.rb +34 -0
- data/spec/da_face/api/push_spec.rb +108 -0
- data/spec/da_face/api/push_subscription_spec.rb +220 -0
- data/spec/da_face/configuration_spec.rb +15 -0
- data/spec/da_face/datasift/da_object_spec.rb +191 -0
- data/spec/da_face/datasift/demographic_spec.rb +30 -0
- data/spec/da_face/datasift/interaction_spec.rb +93 -0
- data/spec/da_face/datasift/language_spec.rb +45 -0
- data/spec/da_face/datasift/link_spec.rb +80 -0
- data/spec/da_face/datasift/links_spec.rb +70 -0
- data/spec/da_face/datasift/parser_spec.rb +5 -0
- data/spec/da_face/datasift/salience_spec.rb +33 -0
- data/spec/da_face/datasift/twitter_delete_notification_spec.rb +45 -0
- data/spec/da_face/datasift/twitter_spec.rb +56 -0
- data/spec/da_face/datasift/twitter_user_status_spec.rb +36 -0
- data/spec/da_face/twitter/parser_spec.rb +16 -0
- data/spec/da_face/twitter/tweet_spec.rb +77 -0
- data/spec/da_face/twitter/user_spec.rb +116 -0
- data/spec/da_face/utilities_spec.rb +74 -0
- data/spec/da_face_spec.rb +120 -0
- data/spec/fixtures/api_responses/get_subscriptions.json +26 -0
- data/spec/fixtures/api_responses/log.json +126 -0
- data/spec/fixtures/api_responses/validate_success.json +4 -0
- data/spec/fixtures/demographic/simple.json +5 -0
- data/spec/fixtures/interaction/simple.json +28 -0
- data/spec/fixtures/interactions/collection.json +1 -0
- data/spec/fixtures/interactions/simple.json +102 -0
- data/spec/fixtures/language/simple.json +5 -0
- data/spec/fixtures/links/multiples.json +69 -0
- data/spec/fixtures/links/simple.json +65 -0
- data/spec/fixtures/links/simple_failing.json +65 -0
- data/spec/fixtures/notifications/delete.json +10 -0
- data/spec/fixtures/notifications/user_delete.json +16 -0
- data/spec/fixtures/notifications/user_protect.json +16 -0
- data/spec/fixtures/notifications/user_suspend.json +16 -0
- data/spec/fixtures/notifications/user_undelete.json +16 -0
- data/spec/fixtures/notifications/user_unprotect.json +16 -0
- data/spec/fixtures/notifications/user_unsuspend.json +16 -0
- data/spec/fixtures/salience/simple.json +5 -0
- data/spec/fixtures/twitter/retweet.json +70 -0
- data/spec/fixtures/twitter/simple.json +33 -0
- data/spec/integration/stress_spec.rb +16 -0
- data/spec/spec_helper.rb +23 -0
- data/spec/test_spec.rb +7 -0
- metadata +259 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c2cc7da25ff983a44979fc89589b3ba941ec8fe8
|
4
|
+
data.tar.gz: 027c7b27941c2022d1a2e3d1de208a5f82ded031
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: add136c3162345f1c338d70943513d6fa505abbaa2c139bc7bb64bb7e31c101df625baf93a408c84ee42654f29ba2a646adafe4b6457bb769d4f0a084561b068
|
7
|
+
data.tar.gz: 1a18b8987549558d183c855ed4efb8ad15102a747f8a7e3293d59b70e4efffaebf7f3c711b84c14780dd141ddd843528a40dac9a2792af0d93f6d1919ecc2730
|
data/.gitignore
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
2
|
+
#
|
3
|
+
# If you find yourself ignoring temporary files generated by your text editor
|
4
|
+
# or operating system, you probably want to add a global ignore instead:
|
5
|
+
# git config --global core.excludesfile ~/.gitignore_global
|
6
|
+
|
7
|
+
.bundle
|
8
|
+
.watchr
|
9
|
+
.redcar
|
10
|
+
.project
|
11
|
+
db/*.sqlite3
|
12
|
+
log/*.log*
|
13
|
+
tmp/*
|
14
|
+
*~
|
15
|
+
*#*#*
|
16
|
+
.DS_Store
|
17
|
+
*.swp
|
18
|
+
*.iml
|
19
|
+
.rvmrc
|
20
|
+
coverage
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
da_face
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.1.0
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
da_face (0.0.1)
|
5
|
+
excon
|
6
|
+
json
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
coderay (1.1.0)
|
12
|
+
diff-lcs (1.2.5)
|
13
|
+
docile (1.1.5)
|
14
|
+
excon (0.39.6)
|
15
|
+
json (1.8.1)
|
16
|
+
method_source (0.8.2)
|
17
|
+
multi_json (1.10.1)
|
18
|
+
pry (0.10.1)
|
19
|
+
coderay (~> 1.1.0)
|
20
|
+
method_source (~> 0.8.1)
|
21
|
+
slop (~> 3.4)
|
22
|
+
rake (10.3.2)
|
23
|
+
rspec (3.1.0)
|
24
|
+
rspec-core (~> 3.1.0)
|
25
|
+
rspec-expectations (~> 3.1.0)
|
26
|
+
rspec-mocks (~> 3.1.0)
|
27
|
+
rspec-core (3.1.5)
|
28
|
+
rspec-support (~> 3.1.0)
|
29
|
+
rspec-expectations (3.1.2)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.1.0)
|
32
|
+
rspec-mocks (3.1.2)
|
33
|
+
rspec-support (~> 3.1.0)
|
34
|
+
rspec-support (3.1.1)
|
35
|
+
simplecov (0.9.1)
|
36
|
+
docile (~> 1.1.0)
|
37
|
+
multi_json (~> 1.0)
|
38
|
+
simplecov-html (~> 0.8.0)
|
39
|
+
simplecov-html (0.8.0)
|
40
|
+
slop (3.6.0)
|
41
|
+
|
42
|
+
PLATFORMS
|
43
|
+
ruby
|
44
|
+
|
45
|
+
DEPENDENCIES
|
46
|
+
bundler (~> 1.6)
|
47
|
+
da_face!
|
48
|
+
pry
|
49
|
+
rake (~> 10.0)
|
50
|
+
rspec
|
51
|
+
simplecov
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Rayko
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
# DaFace
|
2
|
+
|
3
|
+
DaFace is an interface to interact with Datasift API. The good thing about DaFace is that it will present you with pretty data that you can use on your application.
|
4
|
+
|
5
|
+
# DaFace?
|
6
|
+
|
7
|
+
[Da]tasift Inter[face] -> DaFace
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
(It will probably be pretty much like this, but this is still WIP)
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'da_face'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install da_face
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
Configuration:
|
30
|
+
|
31
|
+
DaFace.configure do |config|
|
32
|
+
config.user = 'SomeUser'
|
33
|
+
config.api_key = 'some_long_key'
|
34
|
+
config.api_host = 'http://ds_custom_api_host'
|
35
|
+
config.api_path_prefix = '/v1'
|
36
|
+
config.adapter_class = 'DaFace::Api::Adapters::ExconAdapter'
|
37
|
+
end
|
38
|
+
|
39
|
+
This is all you can configure for DaFace. The required configurations are user and api_key, you can leave the rest because it has default values for those.
|
40
|
+
|
41
|
+
Configuration Details:
|
42
|
+
|
43
|
+
- config.user: username in Datasift to construct the Authorization header.
|
44
|
+
- config.api_key: the api key hash given to get acess to their API.
|
45
|
+
- config.api_host: the base host of where the API is, it defaults to http://api.datasift.com
|
46
|
+
- config.api_path_prefix: a prefix to pre-pend to outgoing requests, for now it's used to set API version. It defauls to '/v1'.
|
47
|
+
- config.adapter_class: tells DaFace which adapter to use. It defauls to Daface::Api::Adapters::ExconAdapter, and it's the only one available for now. You can write your own adapter for RestClient, or any other HTTP interface.
|
48
|
+
|
49
|
+
|
50
|
+
Push Interface:
|
51
|
+
|
52
|
+
You can use DaFace to interact with the Push endpoints of the API, and define or handle subscriptions.
|
53
|
+
You can create a subscription with:
|
54
|
+
|
55
|
+
subscription = Daface.create_subscription :name => 'Test', :output_params => {:auth => {:type => 'none'}, :url => 'http://somewhere.com', :delivery_frequency => 10}
|
56
|
+
|
57
|
+
You can then do other stuff with the subscription:
|
58
|
+
|
59
|
+
subscription.validate # performs a validation of the output params
|
60
|
+
subscription.create # creates the subscription
|
61
|
+
subscription.log # returns a list of recent events in the log
|
62
|
+
subscription.pause # sends a pause command with the subscription id
|
63
|
+
subscription.resume # sends a resume command
|
64
|
+
subscription.delete # deletes the subscription
|
65
|
+
|
66
|
+
|
67
|
+
You can for example get a list of subscriptions:
|
68
|
+
|
69
|
+
DaFace.get_subscriptions
|
70
|
+
|
71
|
+
And then perform any needed operation from the Array you get. All objects inside will be DaFace::Api::PushSubscription objects.
|
72
|
+
|
73
|
+
Parsers:
|
74
|
+
|
75
|
+
DaFace provides some parsers to deal with the push data comming from Datasift. The support of different Datasift objects is limited thought, but for each interaction you get, DaFace will transform it into a DaObject, which will provide you accessors to different data inside the interaction:
|
76
|
+
|
77
|
+
da_object.interaction # interaction information
|
78
|
+
da_object.twitter # twitter information
|
79
|
+
|
80
|
+
Each interaction sub-object, like twitter, will have more info inside:
|
81
|
+
|
82
|
+
da_object.twitter.tweet # a tweet
|
83
|
+
da_object.twitter.tweet.id # the id of the tweet
|
84
|
+
|
85
|
+
|
86
|
+
Notes:
|
87
|
+
|
88
|
+
As mentioned avobe, support is limited, so make sure DaFace suits your needs before implementing. There's still some work to be done, such as creating some more adapters, and make it easier to use DaFace internals.
|
89
|
+
|
90
|
+
Comments, complains, recommendations, ideas, are all welcome.
|
91
|
+
|
92
|
+
Supported Datasift Objects:
|
93
|
+
|
94
|
+
- Interaction
|
95
|
+
- Demographic
|
96
|
+
- Language
|
97
|
+
- Links
|
98
|
+
- Salience
|
99
|
+
- Twitter
|
100
|
+
- Twitter Delete Notifications
|
101
|
+
- Twitter User Notifications
|
102
|
+
|
103
|
+
For the case of Twitter objects, there's support for parsing a normal tweet or retweets. The "tweet" method of the Twitter object will always return the relevant tweet, which will be the normal tweet, or the retweet for the case of a retweet. The original tweet will be available in the "retweeted" method.
|
104
|
+
|
105
|
+
## Contributing
|
106
|
+
|
107
|
+
1. Fork it ( https://github.com/rayko/da_face/fork )
|
108
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
109
|
+
3. Write some specs or update existing ones
|
110
|
+
4. Write some code
|
111
|
+
5. Make sure all specs (new and older) pass
|
112
|
+
6. Commit your changes (`git commit -am 'Add some feature'`)
|
113
|
+
7. Push to the branch (`git push origin my-new-feature`)
|
114
|
+
8. Create a new Pull Request
|
data/Rakefile
ADDED
data/da_face.gemspec
ADDED
@@ -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 'da_face/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "da_face"
|
8
|
+
spec.version = DaFace::VERSION
|
9
|
+
spec.authors = ["Rayko"]
|
10
|
+
spec.email = ["rayko.drg@gmail.com"]
|
11
|
+
spec.summary = %q{DataSift Interface.}
|
12
|
+
spec.description = %q{Interface to interact with Datasift API and get pretty data from them.}
|
13
|
+
spec.homepage = "https://github.com/rayko/da_face"
|
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", "api"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "pry"
|
25
|
+
spec.add_dependency "json"
|
26
|
+
spec.add_dependency "excon"
|
27
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module DaFace
|
2
|
+
module Api
|
3
|
+
class Adapter
|
4
|
+
include DaFace::Utilities
|
5
|
+
|
6
|
+
attr_reader :connection
|
7
|
+
|
8
|
+
def initialize adapter_class = DaFace.configuration.adapter_class
|
9
|
+
@connection = adapter_class.new
|
10
|
+
end
|
11
|
+
|
12
|
+
# TODO Handle headers for rate limits
|
13
|
+
# TODO Handle status codes
|
14
|
+
|
15
|
+
# Performs a get operation
|
16
|
+
#
|
17
|
+
# params is a level 1 hash that should contain params for url
|
18
|
+
def get path, params={}
|
19
|
+
response = connection.get path, params
|
20
|
+
return symbolize_keys(response.keys, response)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Performs a post operation
|
24
|
+
#
|
25
|
+
# payload is a hash that will be parsed to json and sent as body
|
26
|
+
def post path, payload
|
27
|
+
response = connection.post path, payload
|
28
|
+
return true if response.empty?
|
29
|
+
return symbolize_keys(response.keys, response)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Performs a put operation
|
33
|
+
#
|
34
|
+
# payload is a hash that will be parsed to json and sent as body
|
35
|
+
def put path, payload
|
36
|
+
response = connection.put path, payload
|
37
|
+
return true if response.empty?
|
38
|
+
return symbolize_keys(response.keys, response)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module DaFace
|
2
|
+
module Api
|
3
|
+
module Adapters
|
4
|
+
class Base
|
5
|
+
attr_reader :host, :path_prefix, :user, :api_key
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@host = DaFace.configuration.api_host
|
9
|
+
@path_prefix = DaFace.configuration.api_path_prefix
|
10
|
+
@user = DaFace.configuration.user
|
11
|
+
@api_key = DaFace.configuration.api_key
|
12
|
+
|
13
|
+
raise DaFace::AdapterError.new('Missing user for authentication') unless @user
|
14
|
+
raise DaFace::AdapterError.new('Missing api_key for authentication') unless @api_key
|
15
|
+
end
|
16
|
+
|
17
|
+
# Constructs default headers for operations
|
18
|
+
def default_headers
|
19
|
+
{
|
20
|
+
'Authorization' => api_auth_header,
|
21
|
+
'Accept' => 'application/json'
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_headers
|
26
|
+
default_headers
|
27
|
+
end
|
28
|
+
|
29
|
+
def post_headers
|
30
|
+
default_headers.merge({'Content-Type' => 'application/x-www-form-urlencoded'})
|
31
|
+
end
|
32
|
+
|
33
|
+
def encode_form payload
|
34
|
+
URI.encode_www_form(payload)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Constructs the base api path for Datasift
|
38
|
+
def api_path
|
39
|
+
"#{self.host}#{self.path_prefix}"
|
40
|
+
end
|
41
|
+
|
42
|
+
# Constructs the auth header for Datasift
|
43
|
+
def api_auth_header
|
44
|
+
"#{self.user}:#{self.api_key}"
|
45
|
+
end
|
46
|
+
|
47
|
+
# Transforms a level 1 hash to valid url params
|
48
|
+
#
|
49
|
+
# This will return something like "?something=thing" to be appended
|
50
|
+
# to url path
|
51
|
+
def url_params params
|
52
|
+
'?' + URI.encode(params.collect{ |key, value| "#{key}=#{value}"}.join('&')) unless params.empty?
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module DaFace
|
2
|
+
module Api
|
3
|
+
module Adapters
|
4
|
+
class ExconAdapter < DaFace::Api::Adapters::Base
|
5
|
+
def connection
|
6
|
+
Excon.new self.api_path
|
7
|
+
end
|
8
|
+
|
9
|
+
def get path, params={}
|
10
|
+
response = connection.get :path => "#{path}#{url_params(params)}", :headers => get_headers
|
11
|
+
return parse_body(response.body)
|
12
|
+
end
|
13
|
+
|
14
|
+
def post path, payload
|
15
|
+
response = connection.post :path => path, :body => encode_form(payload), :headers => post_headers
|
16
|
+
return {} if response.body.empty?
|
17
|
+
return parse_body(response.body)
|
18
|
+
end
|
19
|
+
|
20
|
+
def put path, payload
|
21
|
+
response = connection.put :path => "#{path}", :body => encode_form(payload), :headers => post_headers
|
22
|
+
return {} if response.body.empty?
|
23
|
+
return parse_body(response.body)
|
24
|
+
end
|
25
|
+
|
26
|
+
def parse_body body
|
27
|
+
JSON.parse(body)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
File without changes
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module DaFace
|
2
|
+
module Api
|
3
|
+
class Push
|
4
|
+
# TODO Handle error cases in a better way (raise)
|
5
|
+
# TODO Handle empty responses
|
6
|
+
|
7
|
+
# Validates output_params for a subscription.
|
8
|
+
def self.validate output_config
|
9
|
+
response = connection.post 'push/validate', output_config
|
10
|
+
raise_push_error_if_error response
|
11
|
+
return response
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.create subscription
|
15
|
+
response = connection.post 'push/create', subscription
|
16
|
+
raise_push_error_if_error response
|
17
|
+
return response
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.update subscription
|
21
|
+
response = connection.put 'push/update', subscription
|
22
|
+
raise_push_error_if_error response
|
23
|
+
return response
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.pause subscription_id
|
27
|
+
response = connection.put 'push/pause', {:id => subscription_id}
|
28
|
+
raise_push_error_if_error response
|
29
|
+
return response
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.resume subscription_id
|
33
|
+
response = connection.put 'push/resume', {:id => subscription_id}
|
34
|
+
raise_push_error_if_error response
|
35
|
+
return response
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.stop subscription_id
|
39
|
+
response = connection.put 'push/stop', {:id => subscription_id}
|
40
|
+
raise_push_error_if_error response
|
41
|
+
return response
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.delete subscription_id
|
45
|
+
response = connection.put 'push/delete', {:id => subscription_id}
|
46
|
+
raise_push_error_if_error response
|
47
|
+
return response
|
48
|
+
end
|
49
|
+
|
50
|
+
# TODO Make this work with pagination
|
51
|
+
def self.log subscription_id
|
52
|
+
response = connection.get 'push/log', {:id => subscription_id}
|
53
|
+
raise_push_error_if_error response
|
54
|
+
return response
|
55
|
+
end
|
56
|
+
|
57
|
+
# TODO Make this work with pagination
|
58
|
+
def self.get params={}
|
59
|
+
response = connection.get 'push/get', params
|
60
|
+
raise_push_error_if_error response
|
61
|
+
return response
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.connection
|
65
|
+
DaFace::Api::Adapter.new
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
def self.raise_push_error_if_error response
|
70
|
+
if response.kind_of?(Hash) && response.keys.include?(:error)
|
71
|
+
raise DaFace::PushError.new(response[:message]) if response.keys.include? :message
|
72
|
+
raise DaFace::PushError.new(response[:error]) if response.keys.include? :error
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module DaFace
|
2
|
+
module Api
|
3
|
+
class PushLogEntry
|
4
|
+
attr_reader :request_time, :success, :subscription_id, :message
|
5
|
+
|
6
|
+
def initialize data
|
7
|
+
@request_time = Time.at data[:request_time]
|
8
|
+
@sucess = data[:success]
|
9
|
+
@subscription_id = data[:subscription_id]
|
10
|
+
@message = data[:message]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
module DaFace
|
2
|
+
module Api
|
3
|
+
class PushSubscription
|
4
|
+
include DaFace::Utilities
|
5
|
+
|
6
|
+
attr_accessor :name, :hash, :initial_status, :start, :end,
|
7
|
+
:output_params, :output_type, :playback_id
|
8
|
+
|
9
|
+
attr_reader :id, :created_at, :user_id, :hash_type, :status, :last_request,
|
10
|
+
:last_success, :remaining_bytes, :lost_data
|
11
|
+
|
12
|
+
def initialize data={}
|
13
|
+
assign_attributes! data
|
14
|
+
normalize_attributes!
|
15
|
+
return self
|
16
|
+
end
|
17
|
+
|
18
|
+
def assign_attributes! data
|
19
|
+
allowed_attributes.each do |attr|
|
20
|
+
unless data[attr].nil?
|
21
|
+
self.instance_variable_set("@#{attr}".to_sym, data[attr])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def update! data={}
|
27
|
+
params = data.delete :output_params
|
28
|
+
assign_attributes! data
|
29
|
+
self.output_params.merge! params
|
30
|
+
normalize_attributes!
|
31
|
+
return self
|
32
|
+
end
|
33
|
+
|
34
|
+
def allowed_attributes
|
35
|
+
[:name, :hash, :initial_status, :start, :end,
|
36
|
+
:output_params, :output_type, :playback_id,
|
37
|
+
:id, :created_at, :user_id, :hash_type, :status, :last_request,
|
38
|
+
:last_success, :remaining_bytes, :lost_data]
|
39
|
+
end
|
40
|
+
|
41
|
+
def normalize_attributes!
|
42
|
+
@last_request = parse_timestamp(@last_request) if @last_request
|
43
|
+
@last_success = parse_timestamp(@last_success) if @last_success
|
44
|
+
@start = (@start && @start > 0) ? parse_timestamp(@start) : nil
|
45
|
+
@end = (@end && @end > 0) ? parse_timestamp(@end) : nil
|
46
|
+
@created_at = parse_timestamp(@created_at) if @created_at
|
47
|
+
end
|
48
|
+
|
49
|
+
def generate_config
|
50
|
+
config = {}
|
51
|
+
config['name'] = self.name if self.name
|
52
|
+
config['output_type'] = self.output_type if self.output_type
|
53
|
+
config['initial_status'] = self.initial_status if self.initial_status
|
54
|
+
config['hash'] = self.hash if self.hash
|
55
|
+
config['playback_id'] = self.playback_id if self.playback_id
|
56
|
+
config['start'] = self.start.to_i if self.start
|
57
|
+
config['end'] = self.end.to_i if self.end
|
58
|
+
|
59
|
+
config.merge! self.output_params_config if self.output_params
|
60
|
+
config
|
61
|
+
end
|
62
|
+
|
63
|
+
def lost_data?
|
64
|
+
self.lost_data
|
65
|
+
end
|
66
|
+
|
67
|
+
def output_params_config
|
68
|
+
params = self.output_params.clone
|
69
|
+
auth = params.delete :auth
|
70
|
+
config = {}
|
71
|
+
params.each do |key, value|
|
72
|
+
config["output_params.#{key}"] = value
|
73
|
+
end
|
74
|
+
auth.each do |key, value|
|
75
|
+
config["output_params.auth.#{key}"] = value
|
76
|
+
end unless auth.nil?
|
77
|
+
|
78
|
+
config
|
79
|
+
end
|
80
|
+
|
81
|
+
def output_config
|
82
|
+
config = {}
|
83
|
+
config['output_type'] = self.output_type
|
84
|
+
config.merge! self.output_params_config
|
85
|
+
|
86
|
+
config
|
87
|
+
end
|
88
|
+
|
89
|
+
def update
|
90
|
+
params = {'id' => self.id, 'name' => self.name}.merge(self.output_config)
|
91
|
+
data = DaFace::Api::Push.update params
|
92
|
+
update! data
|
93
|
+
end
|
94
|
+
|
95
|
+
def validate
|
96
|
+
DaFace::Api::Push.validate self.output_config
|
97
|
+
end
|
98
|
+
|
99
|
+
def create
|
100
|
+
data = DaFace::Api::Push.create self.generate_config
|
101
|
+
return data if data.keys.include? :error
|
102
|
+
update! data
|
103
|
+
end
|
104
|
+
|
105
|
+
def pause
|
106
|
+
DaFace::Api::Push.pause self.id
|
107
|
+
end
|
108
|
+
|
109
|
+
def resume
|
110
|
+
DaFace::Api::Push.resume self.id
|
111
|
+
end
|
112
|
+
|
113
|
+
def stop
|
114
|
+
DaFace::Api::Push.stop self.id
|
115
|
+
end
|
116
|
+
|
117
|
+
def delete
|
118
|
+
DaFace::Api::Push.delete self.id
|
119
|
+
end
|
120
|
+
|
121
|
+
def log
|
122
|
+
data = DaFace::Api::Push.log self.id
|
123
|
+
return data if data.keys.include? :error
|
124
|
+
DaFace::Api::PushLog.new data
|
125
|
+
end
|
126
|
+
|
127
|
+
def get
|
128
|
+
data = DaFace::Api::Push.get :id => self.id
|
129
|
+
return data if data.keys.include? :error
|
130
|
+
update! data
|
131
|
+
end
|
132
|
+
|
133
|
+
def self.get_all
|
134
|
+
data = DaFace::Api::Push.get
|
135
|
+
return data if data.keys.include? :error
|
136
|
+
data[:subscriptions].collect{|d| new(d)}
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module DaFace
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :api_path_prefix, :api_host, :user, :api_key, :adapter_class
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@api_path_prefix = '/v1'
|
7
|
+
@api_host = 'http://api.datasift.com'
|
8
|
+
@adapter_class = DaFace::Api::Adapters::ExconAdapter
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|