datasift 3.1.5 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -2
  3. data/.yardopts +4 -0
  4. data/CHANGELOG.md +23 -0
  5. data/Gemfile +13 -0
  6. data/README.md +17 -16
  7. data/VERSION +1 -1
  8. data/datasift.gemspec +8 -14
  9. data/examples/account_identity_eg.rb +48 -0
  10. data/examples/account_identity_limit_eg.rb +68 -0
  11. data/examples/account_identity_token_eg.rb +70 -0
  12. data/examples/auth.rb +4 -4
  13. data/examples/core_api_eg.rb +1 -2
  14. data/examples/historics_eg.rb +7 -1
  15. data/examples/pylon_eg.rb +116 -0
  16. data/lib/account.rb +6 -0
  17. data/lib/account_identity.rb +73 -0
  18. data/lib/account_identity_limit.rb +85 -0
  19. data/lib/account_identity_token.rb +86 -0
  20. data/lib/api/api_resource.rb +14 -5
  21. data/lib/cli.rb +306 -132
  22. data/lib/datasift.rb +82 -47
  23. data/lib/errors.rb +21 -5
  24. data/lib/historics.rb +71 -40
  25. data/lib/historics_preview.rb +25 -8
  26. data/lib/managed_source.rb +69 -25
  27. data/lib/managed_source_auth.rb +21 -6
  28. data/lib/managed_source_resource.rb +21 -6
  29. data/lib/push.rb +132 -65
  30. data/lib/pylon.rb +122 -0
  31. data/test/datasift/core_api_test.rb +116 -90
  32. data/test/datasift/historics_preview_api_test.rb +27 -58
  33. data/test/datasift/push_api_test.rb +156 -141
  34. data/test/fixtures/cassettes/core/after_historic_dpu.json +1 -0
  35. data/test/fixtures/cassettes/core/balance_get.json +1 -0
  36. data/test/fixtures/cassettes/core/before_dpu.json +1 -0
  37. data/test/fixtures/cassettes/core/before_historic_dpu.json +1 -0
  38. data/test/fixtures/cassettes/core/compile_success.json +1 -0
  39. data/test/fixtures/cassettes/core/dpu_get_cost.json +1 -0
  40. data/test/fixtures/cassettes/core/dpu_throw_badrequest.json +1 -0
  41. data/test/fixtures/cassettes/core/historic_dpu.json +1 -0
  42. data/test/fixtures/cassettes/core/usage_success.json +1 -0
  43. data/test/fixtures/cassettes/core/validate_invalid_hash.json +1 -0
  44. data/test/fixtures/cassettes/core/validate_success_bool.json +1 -0
  45. data/test/fixtures/cassettes/core/validate_success_hash.json +1 -0
  46. data/test/fixtures/cassettes/preview/before_preview_create.json +1 -0
  47. data/test/fixtures/cassettes/preview/before_preview_get.json +1 -0
  48. data/test/fixtures/cassettes/preview/preview_create_success.json +1 -0
  49. data/test/fixtures/cassettes/preview/preview_get_success.json +1 -0
  50. data/test/fixtures/cassettes/push/after_push_create.json +1 -0
  51. data/test/fixtures/cassettes/push/after_push_get.json +1 -0
  52. data/test/fixtures/cassettes/push/after_push_log.json +1 -0
  53. data/test/fixtures/cassettes/push/after_push_pause.json +1 -0
  54. data/test/fixtures/cassettes/push/after_push_resume.json +1 -0
  55. data/test/fixtures/cassettes/push/after_push_stop.json +1 -0
  56. data/test/fixtures/cassettes/push/after_push_update.json +1 -0
  57. data/test/fixtures/cassettes/push/before_push_create.json +1 -0
  58. data/test/fixtures/cassettes/push/before_push_delete.json +1 -0
  59. data/test/fixtures/cassettes/push/before_push_get.json +1 -0
  60. data/test/fixtures/cassettes/push/before_push_log.json +1 -0
  61. data/test/fixtures/cassettes/push/before_push_pause.json +1 -0
  62. data/test/fixtures/cassettes/push/before_push_resume.json +1 -0
  63. data/test/fixtures/cassettes/push/before_push_stop.json +1 -0
  64. data/test/fixtures/cassettes/push/before_push_update.json +1 -0
  65. data/test/fixtures/cassettes/push/push_create.json +1 -0
  66. data/test/fixtures/cassettes/push/push_delete.json +1 -0
  67. data/test/fixtures/cassettes/push/push_get_by_id.json +1 -0
  68. data/test/fixtures/cassettes/push/push_log_with_id.json +1 -0
  69. data/test/fixtures/cassettes/push/push_pause.json +1 -0
  70. data/test/fixtures/cassettes/push/push_resume.json +1 -0
  71. data/test/fixtures/cassettes/push/push_stop.json +1 -0
  72. data/test/fixtures/cassettes/push/push_update.json +1 -0
  73. data/test/fixtures/cassettes/push/push_validate.json +1 -0
  74. data/test/test_helper.rb +31 -1
  75. metadata +64 -104
  76. data/examples/dynamic_list_eg.rb +0 -74
  77. data/examples/dynamic_list_replace_eg.rb +0 -45
  78. data/lib/dynamic_list.rb +0 -66
  79. data/lib/dynamic_list_replace.rb +0 -45
  80. data/test/fixtures/balance.json +0 -1
  81. data/test/fixtures/compile_csdl_invalid.json +0 -1
  82. data/test/fixtures/compile_csdl_valid.json +0 -1
  83. data/test/fixtures/dpu_valid.json +0 -1
  84. data/test/fixtures/preview_create_valid.json +0 -1
  85. data/test/fixtures/preview_get_running.json +0 -1
  86. data/test/fixtures/preview_get_succeeded.json +0 -1
  87. data/test/fixtures/push_create_valid.json +0 -1
  88. data/test/fixtures/push_get_list_by_hash_valid.json +0 -1
  89. data/test/fixtures/push_get_list_by_historics_id_valid.json +0 -1
  90. data/test/fixtures/push_get_list_valid.json +0 -1
  91. data/test/fixtures/push_get_valid.json +0 -1
  92. data/test/fixtures/push_log_valid.json +0 -1
  93. data/test/fixtures/push_pause_valid.json +0 -1
  94. data/test/fixtures/push_stop_valid.json +0 -1
  95. data/test/fixtures/push_validate_valid.json +0 -1
  96. data/test/fixtures/usage_current.json +0 -1
  97. data/test/fixtures/validate_csdl_invalid.json +0 -1
  98. data/test/fixtures/validate_csdl_valid.json +0 -1
@@ -0,0 +1,116 @@
1
+ require './auth'
2
+ class AnalysisApi < DataSiftExample
3
+ def initialize
4
+ super
5
+ run_analysis
6
+ end
7
+
8
+ def run_analysis
9
+ begin
10
+ puts "Create a new identity to make PYLON API calls"
11
+ identity = @datasift.account_identity.create(
12
+ "JASON_#{Time.now.to_i}",
13
+ "active",
14
+ false
15
+ )
16
+ identity_id = identity[:data][:id]
17
+ puts identity[:data].to_json
18
+
19
+ puts "\nCreate a Token for our Identity"
20
+ token = @datasift.account_identity_token.create(
21
+ identity_id,
22
+ 'facebook',
23
+ 'YOUR_TOKEN'
24
+ )
25
+ puts token[:data].to_json
26
+
27
+ puts "\nNow make PYLON API calls using the Identity's API key"
28
+ @config.merge!(api_key: identity[:data][:api_key])
29
+ @datasift = DataSift::Client.new(@config)
30
+
31
+ csdl = 'return { fb.content contains "data" }'
32
+
33
+ puts "Check this CSDL is valid: #{csdl}"
34
+ puts "Valid? #{@datasift.pylon.valid?(csdl)}"
35
+
36
+ puts "\nCompile my CSDL"
37
+ compiled = @datasift.pylon.compile csdl
38
+ hash = compiled[:data][:hash]
39
+ puts "Hash: #{hash}"
40
+
41
+ puts "\nStart recording filter with hash #{hash}"
42
+ filter = @datasift.pylon.start(
43
+ hash,
44
+ 'Facebook Pylon Test Filter'
45
+ )
46
+ puts filter[:data].to_json
47
+
48
+ puts "\nSleep for 10 seconds to record a little data"
49
+ sleep(10)
50
+
51
+ puts "\nGet details of our running recording"
52
+ puts @datasift.pylon.get(hash)[:data].to_json
53
+
54
+ puts "\nYou can also list running recordings"
55
+ puts @datasift.pylon.list[:data].to_json
56
+
57
+ puts "\nFrequency distribution analysis on fb.author.country"
58
+ params = {
59
+ analysis_type: 'freqDist',
60
+ parameters: {
61
+ threshold: 1,
62
+ target: 'fb.author.country'
63
+ }
64
+ }
65
+ puts @datasift.pylon.analyze(
66
+ hash,
67
+ params
68
+ )[:data].to_json
69
+
70
+ puts "\nFrequency distribution analysis on fb.author.age with filter"
71
+ params = {
72
+ analysis_type: 'freqDist',
73
+ parameters: {
74
+ threshold: 1,
75
+ target: 'fb.author.age'
76
+ }
77
+ }
78
+ filter = 'fb.content contains "starbucks"'
79
+ puts @datasift.pylon.analyze(
80
+ hash,
81
+ params,
82
+ filter
83
+ )[:data].to_json
84
+
85
+ puts "\nTime series analysis"
86
+ params = {
87
+ analysis_type: 'timeSeries',
88
+ parameters: {
89
+ interval: 'hour',
90
+ span: 12
91
+ }
92
+ }
93
+ filter = ''
94
+ start_time = Time.now.to_i - (60 * 60 * 12) # 7 days ago
95
+ end_time = Time.now.to_i
96
+ puts @datasift.pylon.analyze(
97
+ hash,
98
+ params,
99
+ filter,
100
+ start_time,
101
+ end_time
102
+ )[:data].to_json
103
+
104
+ puts "\nTags analysis"
105
+ puts @datasift.pylon.tags(hash)[:data].to_json
106
+
107
+ puts "\nStop recording filter with hash #{hash}"
108
+ puts @datasift.pylon.stop(hash)[:data].to_json
109
+
110
+ rescue DataSiftError => dse
111
+ puts dse.to_s
112
+ end
113
+ end
114
+ end
115
+
116
+ AnalysisApi.new
data/lib/account.rb ADDED
@@ -0,0 +1,6 @@
1
+ module DataSift
2
+ #
3
+ # Class for accessing DataSift's Account API Identities
4
+ class Account < DataSift::ApiResource
5
+ end
6
+ end
@@ -0,0 +1,73 @@
1
+ module DataSift
2
+ #
3
+ # Class for accessing DataSift's Account API Identities
4
+ class AccountIdentity < DataSift::ApiResource
5
+ # Creates a new Identity
6
+ #
7
+ # @param label [String] A unique identifier for this Identity
8
+ # @param status [String] (Optional, Default: "active") What status this
9
+ # Identity currently has. Possible values are 'active' and 'disabled'
10
+ # @param master [Boolean] (Optional, Default: false) Whether this is the
11
+ # master Identity for your account
12
+ # @return [Object] API reponse object
13
+ def create(label = '', status = 'active', master = '')
14
+ fail ArgumentError, 'label is missing' if label.empty?
15
+
16
+ params = { label: label }
17
+ params.merge!(status: status) unless status.empty?
18
+ params.merge!(master: master) if [TrueClass, FalseClass].include?(master.class)
19
+
20
+ DataSift.request(:POST, 'account/identity', @config, params)
21
+ end
22
+
23
+ # Gets a specific Identity by ID
24
+ #
25
+ # @param id [String] ID of the Identity you wish to return
26
+ # @return [Object] API reponse object
27
+ def get(id)
28
+ DataSift.request(:GET, "account/identity/#{id}", @config)
29
+ end
30
+
31
+ # Returns a list of Identities
32
+ #
33
+ # @param label [String] (Optional) Search by a given Identity label
34
+ # @param per_page [Integer] (Optional) How many Identities should be
35
+ # returned per page of results
36
+ # @param page [Integer] (Optional) Which page of results to return
37
+ # @return [Object] API reponse object
38
+ def list(label = '', per_page = '', page = '')
39
+ params = {}
40
+ params.merge!(label: label) unless label.empty?
41
+ params.merge!(per_page: per_page) unless per_page.empty?
42
+ params.merge!(page: page) unless page.empty?
43
+
44
+ DataSift.request(:GET, 'account/identity', @config, params)
45
+ end
46
+
47
+ # Updates a specific Identity by ID
48
+ #
49
+ # @param id [String] ID of the Identity you are updating
50
+ # @param label [String] (Optional) New label value
51
+ # @param status [String] (Optional) New status for this Identity
52
+ # @param master [Boolean] (Optional) Whether this Identity should be master
53
+ # @return [Object] API reponse object
54
+ def update(id = '', label = '', status = '', master = '')
55
+ fail ArgumentError, 'id is missing' if id.empty?
56
+
57
+ params = {}
58
+ params.merge!(label: label) unless label.empty?
59
+ params.merge!(status: status) unless status.empty?
60
+ params.merge!(master: master) if [TrueClass, FalseClass].include?(master.class)
61
+
62
+ DataSift.request(:PUT, "account/identity/#{id}", @config, params)
63
+ end
64
+
65
+ # Deletes a specific Identity by ID
66
+ #
67
+ # @param id [String] ID of the Identity you wish to delete
68
+ # @return [Object] API response object
69
+ def delete(id)
70
+ DataSift.request(:DELETE, "account/identity/#{id}", @config)
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,85 @@
1
+ module DataSift
2
+ #
3
+ # Class for accessing DataSift's Account API Identity Limits
4
+ class AccountIdentityLimit < DataSift::ApiResource
5
+ # Creates a Limit for an Identity
6
+ #
7
+ # @param identity_id [String] ID of the Identity for which you are creating
8
+ # a limit
9
+ # @param service [String] The service this limit will apply to. For example;
10
+ # 'facebook'
11
+ # @param total_allowance [Integer] The limit for this Identity
12
+ # @return [Object] API reponse object
13
+ def create(identity_id = '', service = '', total_allowance = nil)
14
+ fail BadParametersError, 'identity_id is required' if identity_id.empty?
15
+ fail BadParametersError, 'service is required' if service.empty?
16
+ fail BadParametersError, 'total_allowance can not be "nil"' if total_allowance.nil?
17
+ params = {
18
+ service: service,
19
+ total_allowance: total_allowance
20
+ }
21
+
22
+ DataSift.request(:POST, "account/identity/#{identity_id}/limit", @config, params)
23
+ end
24
+
25
+ # Get the Limit for a given Identity and Service
26
+ #
27
+ # @param identity_id [String] ID of the Identity you wish to return limits
28
+ # for
29
+ # @param service [String] Name of the service you are retreiving limits for
30
+ # @return [Object] API reponse object
31
+ def get(identity_id = '', service = '')
32
+ fail BadParametersError, 'identity_id is required' if identity_id.empty?
33
+ fail BadParametersError, 'service is required' if service.empty?
34
+
35
+ DataSift.request(:GET, "account/identity/#{identity_id}/limit/#{service}", @config)
36
+ end
37
+
38
+ # Returns a list Identities and their Limits for a given Service
39
+ #
40
+ # @param service [String] ID of the Identity we are fetching Limits for
41
+ # @param per_page [Integer] (Optional) How many Identities and Limits should
42
+ # be returned per page of results
43
+ # @param page [Integer] (Optional) Which page of results to return
44
+ # @return [Object] API reponse object
45
+ def list(service = '', per_page = '', page = '')
46
+ fail BadParametersError, 'service is required' if service.empty?
47
+
48
+ params = {}
49
+ params.merge!(per_page: per_page) unless per_page.empty?
50
+ params.merge!(page: page) unless page.empty?
51
+
52
+ DataSift.request(:GET, "account/identity/limit/#{service}", @config, params)
53
+ end
54
+
55
+ # Updates a Limit for an Identity by Service
56
+ #
57
+ # @param identity_id [String] ID of the Identity for which you are updating
58
+ # a limit
59
+ # @param service [String] The service this limit will apply to. For example;
60
+ # 'facebook'
61
+ # @param total_allowance [Integer] The new limit for this Identity
62
+ # @return [Object] API reponse object
63
+ def update(identity_id = '', service = '', total_allowance = nil)
64
+ fail BadParametersError, 'identity_id is required' if identity_id.empty?
65
+ fail BadParametersError, 'service is required' if service.empty?
66
+ fail BadParametersError, 'total_allowance can not be "nil"' if total_allowance.nil?
67
+ params = { total_allowance: total_allowance }
68
+
69
+ DataSift.request(:PUT, "account/identity/#{identity_id}/limit/#{service}", @config, params)
70
+ end
71
+
72
+ # Removes a Service Limit for an Identity
73
+ #
74
+ # @param identity_id [String] ID of the Identity for which you wish to
75
+ # remove the Limit
76
+ # @param service [String] Service from which you wish to remove the Limit
77
+ # @return [Object] API response object
78
+ def delete(identity_id = '', service = '')
79
+ fail BadParametersError, 'identity_id is required' if identity_id.empty?
80
+ fail BadParametersError, 'service is required' if service.empty?
81
+
82
+ DataSift.request(:DELETE, "account/identity/#{identity_id}/limit/#{service}", @config)
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,86 @@
1
+ module DataSift
2
+ #
3
+ # Class for accessing DataSift's Account API Identity Tokens
4
+ class AccountIdentityToken < DataSift::ApiResource
5
+ # Creates a new Identity Token
6
+ #
7
+ # @param identity_id [String] ID of the Identity for which you are creating
8
+ # a token
9
+ # @param service [String] The service this token will be used to access. For
10
+ # example; 'facebook'
11
+ # @param token [String] The token provided by the PYLON data provider
12
+ # @return [Object] API reponse object
13
+ def create(identity_id = '', service = '', token = '')
14
+ fail BadParametersError, 'identity_id is required' if identity_id.empty?
15
+ fail BadParametersError, 'service is required' if service.empty?
16
+ fail BadParametersError, 'token is required' if token.empty?
17
+ params = {
18
+ service: service,
19
+ token: token
20
+ }
21
+
22
+ DataSift.request(:POST, "account/identity/#{identity_id}/token", @config, params)
23
+ end
24
+
25
+ # Get a Token for a given Identity and Service
26
+ #
27
+ # @param identity_id [String] ID of the Identity you wish to return tokens
28
+ # for
29
+ # @param service [String] Name of the service you are retreiving tokens for
30
+ # @return [Object] API reponse object
31
+ def get(identity_id = '', service = '')
32
+ fail BadParametersError, 'identity_id is required' if identity_id.empty?
33
+ fail BadParametersError, 'service is required' if service.empty?
34
+
35
+ DataSift.request(:GET, "account/identity/#{identity_id}/token/#{service}", @config)
36
+ end
37
+
38
+ # Returns a list of Tokens for a given Identity
39
+ #
40
+ # @param identity_id [String] ID of the Identity we are fetching Tokens for
41
+ # @param per_page [Integer] (Optional) How many Tokens should be returned
42
+ # per page of results
43
+ # @param page [Integer] (Optional) Which page of results to return
44
+ # @return [Object] API reponse object
45
+ def list(identity_id = '', per_page = '', page = '')
46
+ params = { identity_id: identity_id }
47
+ requires params
48
+ params.merge!(per_page: per_page) unless per_page.empty?
49
+ params.merge!(page: page) unless page.empty?
50
+
51
+ DataSift.request(:GET, "account/identity/#{identity_id}/token", @config, params)
52
+ end
53
+
54
+ # Updates a specific Token by Identity ID and Service
55
+ #
56
+ # @param identity_id [String] ID of the Identity you are updating a token
57
+ # for
58
+ # @param service [String] The service this token will be used to access. For
59
+ # example; 'facebook'
60
+ # @param token [String] The token provided by the PYLON data provider
61
+ # @return [Object] API reponse object
62
+ def update(identity_id = '', service = '', token = '')
63
+ fail BadParametersError, 'identity_id is required' if identity_id.empty?
64
+ fail BadParametersError, 'service is required' if service.empty?
65
+ fail BadParametersError, 'token is required' if token.empty?
66
+ params = {
67
+ token: token
68
+ }
69
+
70
+ DataSift.request(:PUT, "account/identity/#{identity_id}/token/#{service}", @config, params)
71
+ end
72
+
73
+ # Deletes a specific Token by Identity and Service
74
+ #
75
+ # @param identity_id [String] ID of the Identity for which you wish to
76
+ # delete a token
77
+ # @param service [String] Service from which you wish to delete a token
78
+ # @return [Object] API response object
79
+ def delete(identity_id = '', service = '')
80
+ fail BadParametersError, 'identity_id is required' if identity_id.empty?
81
+ fail BadParametersError, 'service is required' if service.empty?
82
+
83
+ DataSift.request(:DELETE, "account/identity/#{identity_id}/token/#{service}", @config)
84
+ end
85
+ end
86
+ end
@@ -1,12 +1,17 @@
1
1
  module DataSift
2
+ # Base API class
2
3
  class ApiResource
3
4
  include DataSift
4
5
 
5
- def initialize (config)
6
+ # Initializer to create global @config object
7
+ #
8
+ # @param config [Hash] Pass config object, including your DataSift username,
9
+ # API key and any other custom config parameters
10
+ def initialize(config)
6
11
  @config = config
7
12
  config[:api_host] = 'api.datasift.com' unless config.has_key?(:api_host)
8
13
  config[:stream_host] = 'websocket.datasift.com' unless config.has_key?(:stream_host)
9
- config[:api_version] = 'v1' unless config.has_key?(:api_version)
14
+ config[:api_version] = 'v1.1' unless config.has_key?(:api_version)
10
15
  config[:enable_ssl] = true unless config.has_key?(:enable_ssl)
11
16
  # Only SSLv3, TLSv1 and TLSv1.2 currently supported, TLSv1.2 preferred
12
17
  # this is fixed in REST client and is scheduled for the 1.7.0 release
@@ -20,11 +25,15 @@ module DataSift
20
25
  ssl_default
21
26
 
22
27
  # max 320 seconds retry - http://dev.datasift.com/docs/streaming-api/reconnecting
23
- config[:max_retry_time] = 320 unless config.has_key?(:max_retry_time)
24
- config[:retry_timeout] = 0 unless config.has_key?(:retry_timeout)
28
+ config[:max_retry_time] = 320 unless config.key?(:max_retry_time)
29
+ config[:retry_timeout] = 0 unless config.key?(:retry_timeout)
25
30
  end
26
31
 
27
- def requires params
32
+ # Ensure parameters have been set
33
+ #
34
+ # @param params [Hash] Hash of parameters you need to check exist and are
35
+ # non-null values
36
+ def requires(params)
28
37
  params.each { |k, v|
29
38
  if v == nil || v.to_s.length == 0
30
39
  raise InvalidParamError.new "#{k} is a required parameter, it cannot be nil or empty"
data/lib/cli.rb CHANGED
@@ -6,12 +6,14 @@ require 'multi_json'
6
6
  require_relative '../lib/datasift'
7
7
 
8
8
  def to_output(r)
9
- MultiJson.dump({
10
- :status => r[:http][:status],
11
- :headers => r[:http][:headers],
12
- :body => r[:data]
13
- },
14
- :pretty => true)
9
+ MultiJson.dump(
10
+ {
11
+ :status => r[:http][:status],
12
+ :headers => r[:http][:headers],
13
+ :body => r[:data]
14
+ },
15
+ :pretty => true
16
+ )
15
17
  end
16
18
 
17
19
  def opt(val, default)
@@ -19,7 +21,7 @@ def opt(val, default)
19
21
  end
20
22
 
21
23
  def err(m)
22
- puts MultiJson.dump({:error => m})
24
+ puts MultiJson.dump(:error => m)
23
25
  end
24
26
 
25
27
  def parse(args)
@@ -34,160 +36,322 @@ def parse(args)
34
36
  opts.banner = 'Usage: cli.rb [-c] [--api] -a -e [-p*]'
35
37
  opts.separator 'Specific options:'
36
38
 
37
- opts.on('-a', '--auth AUTH', 'DataSift username:api_key') do |username|
38
- api_key = ARGV.length>0 && ARGV[0].index('-') == 0 ? '' : ARGV[0]
39
- if username == nil || api_key == nil || username.empty? || api_key.empty?
39
+ opts.on('-a', '--auth AUTH', 'DataSift username and API key (formatted as "username api_key")') do |username|
40
+ api_key = ARGV.length > 0 && ARGV[0].index('-') == 0 ? '' : ARGV[0]
41
+ if username.nil? || api_key.nil? || username.empty? || api_key.empty?
40
42
  err 'Unable to parse username and API key, they must be in the format username api_key'
41
43
  err parse(%w(-h))
42
44
  exit
43
45
  end
44
- options.auth = {:username => username, :api_key => api_key}
46
+ options.auth = { :username => username, :api_key => api_key }
45
47
  end
46
48
 
47
- opts.on('-e', '--endpoint ENDPOINT', 'Defaults to core, must be one of core,push,historics,preview,sources') do |e|
49
+ opts.on('-e', '--endpoint ENDPOINT', 'Defaults to core, must be one of core, push, historics, preview, sources, pylon') do |e|
48
50
  options.endpoint = e
49
51
  end
50
52
 
51
53
  opts.on('-c', '--command COMMAND', 'DataSift endpoint, depends on the endpoint') do |e|
52
- options.command = e|| 'core'
54
+ options.command = e || 'core'
53
55
  end
54
56
 
55
57
  opts.on('-p', '--param PARAM', 'Command specific parameters e.g. -p name value') do |k|
56
58
  # value is ARGV[0] unless ARGV[0] starts with a hyphen
57
- options.params[k] = ARGV.length>0 && ARGV[0].index('-') == 0 ? '' : ARGV[0]
59
+ options.params[k] = ARGV.length > 0 && ARGV[0].index('-') == 0 ? '' :
60
+ ARGV[0]
58
61
  end
59
62
 
60
63
  opts.on('-u', '--url API_HOSTNAME', 'Override the API URL') do |e|
61
64
  options.api = e
62
65
  end
63
66
 
67
+ opts.on('--no-ssl', 'Do not use SSL for API requests') do
68
+ options.enable_ssl = false
69
+ end
70
+
64
71
  opts.on_tail('-h', '--help', 'Show this message') do
65
- err opts
72
+ puts opts
66
73
  exit
67
74
  end
68
75
 
69
- opts.on_tail('--version', 'Show version') do
70
- err :: DataSift::VERSION
76
+ opts.on_tail('-v', '--version', 'DataSift client library version') do
77
+ puts DataSift::VERSION
71
78
  exit
72
79
  end
73
80
  end
74
81
 
75
82
  opt_parser.parse!(args)
76
- options #.marshal_dump
83
+ options
77
84
  end
78
85
 
79
- def run_core_command (c, command, p)
86
+ def run_core_command(c, command, p)
80
87
  case command
81
- when 'validate'
82
- c.valid?(p['csdl'], false)
83
- when 'compile'
84
- c.compile(p['csdl'])
85
- when 'usage'
86
- c.usage(usage = p['period'] ? p['period'].to_sym : :hour)
87
- when 'balance'
88
- c.balance
89
- when 'dpu'
90
- c.dpu(p['hash'])
91
- else
92
- err 'Unknown command for the core endpoint'
93
- exit
88
+ when 'validate'
89
+ c.valid?(p['csdl'], false)
90
+ when 'compile'
91
+ c.compile(p['csdl'])
92
+ when 'usage'
93
+ c.usage(p['period'] ? p['period'].to_sym : :hour)
94
+ when 'balance'
95
+ c.balance
96
+ when 'dpu'
97
+ c.dpu(opt(p['hash'], ''), opt(p['historics_id'], ''))
98
+ else
99
+ err 'Unknown command for the core endpoint'
100
+ exit
94
101
  end
95
102
  end
96
103
 
97
- def run_historics_command (c, command, p)
104
+ def run_historics_command(c, command, p)
98
105
  case command
99
- when 'prepare'
100
- c.historics.prepare(p['hash'], p['start'], p['end'], p['name'], opt(p['sources'], 'twitter'), opt(p['sample'], 10))
101
- when 'start'
102
- c.historics.start(p['id'])
103
- when 'stop'
104
- c.historics.stop(p['id'], opt(p['reason'], ''))
105
- when 'status'
106
- c.historics.status(p['start'], p['end'], opt(p['sources'], 'twitter'))
107
- when 'update'
108
- c.historics.update(p['id'], p['name'])
109
- when 'delete'
110
- c.historics.delete(p['id'])
111
- when 'get'
112
- c.historics.get(opt(p['max'], 20), opt(p['page'], 1), opt(p['with_estimate'], 1))
113
- else
114
- err 'Unknown command for the historics endpoint'
115
- exit
106
+ when 'prepare'
107
+ c.historics.prepare(p['hash'], p['start'], p['end'], p['name'], opt(p['sources'], 'twitter'), opt(p['sample'], 10))
108
+ when 'start'
109
+ c.historics.start(p['id'])
110
+ when 'stop'
111
+ c.historics.stop(p['id'], opt(p['reason'], ''))
112
+ when 'status'
113
+ c.historics.status(p['start'], p['end'], opt(p['sources'], 'twitter'))
114
+ when 'update'
115
+ c.historics.update(p['id'], p['name'])
116
+ when 'delete'
117
+ c.historics.delete(p['id'])
118
+ when 'get'
119
+ c.historics.get(opt(p['max'], 20), opt(p['page'], 1), opt(p['with_estimate'], 1))
120
+ else
121
+ err 'Unknown command for the historics endpoint'
122
+ exit
116
123
  end
117
124
  end
118
125
 
119
- def run_preview_command (c, command, p)
126
+ def run_preview_command(c, command, p)
120
127
  case command
121
- when 'create'
122
- c.historics_preview.create(p['hash'], p['parameters'], p['start'], opt(p['end'], nil))
123
- when 'get'
124
- c.historics_preview.get(p['id'])
125
- else
126
- err 'Unknown command for the historics preview endpoint'
127
- exit
128
+ when 'create'
129
+ c.historics_preview.create(p['hash'], p['parameters'], p['start'], opt(p['end'], nil))
130
+ when 'get'
131
+ c.historics_preview.get(p['id'])
132
+ else
133
+ err 'Unknown command for the historics preview endpoint'
134
+ exit
128
135
  end
129
136
  end
130
137
 
131
- def run_sources_command (c, command, p)
138
+ def run_sources_command(c, command, p)
132
139
  case command
133
- when 'create'
134
- c.managed_source.create(p['source_type'],p['name'], opt(p['parameters'], {}),
135
- opt(p['resources'], []), opt(p['auth'], []))
136
- when 'update'
137
- c.managed_source.update(p['id'], p['source_type'], p['name'], opt(p['parameters'], {}),
138
- opt(p['resources'], []),
139
- opt(p['auth'], []))
140
- when 'delete'
141
- c.managed_source.delete(p['id'])
142
- when 'stop'
143
- c.managed_source.stop(p['id'])
144
- when 'start'
145
- c.managed_source.start(p['id'])
146
- when 'log'
147
- c.managed_source.log(p['id'], opt(p['page'], 1), opt(p['per_page'], 20))
148
- when 'get'
149
- c.managed_source.get(opt(p['id'], nil), opt(p['source_type'], nil), opt(p['page'], 1), opt(p['per_page'], 20))
150
- else
151
- err 'Unknown command for the historics preview endpoint'
152
- exit
140
+ when 'create'
141
+ c.managed_source.create(p['source_type'],p['name'], opt(p['parameters'], {}),
142
+ opt(p['resources'], []), opt(p['auth'], []))
143
+ when 'update'
144
+ c.managed_source.update(p['id'], p['source_type'], p['name'], opt(p['parameters'], {}),
145
+ opt(p['resources'], []),
146
+ opt(p['auth'], []))
147
+ when 'delete'
148
+ c.managed_source.delete(p['id'])
149
+ when 'stop'
150
+ c.managed_source.stop(p['id'])
151
+ when 'start'
152
+ c.managed_source.start(p['id'])
153
+ when 'log'
154
+ c.managed_source.log(p['id'], opt(p['page'], 1), opt(p['per_page'], 20))
155
+ when 'get'
156
+ c.managed_source.get(opt(p['id'], nil), opt(p['source_type'], nil), opt(p['page'], 1), opt(p['per_page'], 20))
157
+ else
158
+ err 'Unknown command for the historics preview endpoint'
159
+ exit
153
160
  end
154
161
  end
155
162
 
156
- def run_push_command (c, command, p)
163
+ def run_push_command(c, command, p)
157
164
  case command
158
- when 'validate'
159
- c.push.valid? p, false
160
- when 'create'
161
- c.push.create p
162
- when 'pause'
163
- c.push.pause p['id']
164
- when 'resume'
165
- c.push.resume p['id']
166
- when 'update'
167
- c.push.update p
168
- when 'stop'
169
- c.push.stop p['id']
170
- when 'delete'
171
- c.push.delete p['id']
172
- when 'log'
173
- p['id'] ?
174
- c.push.logs_for(p['id'], opt(p['page'], 0), opt(p['per_page'], 20), opt(p['order_by'], :request_time), opt(p['order_dir'], :desc)) :
175
- c.push.logs(opt(p['page'], 0), opt(p['per_page'], 20), opt(p['order_by'], :request_time), opt(p['order_dir'], :desc))
176
- when 'get'
177
- if p['id']
178
- c.push.get_by_subscription(p['id'], opt(p['page'], 0), opt(p['per_page'], 20), opt(p['order_by'], :request_time))
179
- elsif p['hash']
180
- c.push.get_by_hash(p['hash'], opt(p['page'], 0), opt(p['per_page'], 20), opt(p['order_by'], :request_time), opt(p['order_dir'], :desc))
181
- elsif p['historics_id']
182
- c.push.get_by_historics_id(p['historics_id'], opt(p['page'], 0), opt(p['per_page'], 20), opt(p['order_by'], :request_time), opt(p['order_dir'], :desc))
183
- else
184
- c.push.get(opt(p['page'], 0), opt(p['per_page'], 20), opt(p['order_by'], :request_time), opt(p['order_dir'], :desc))
185
- end
186
- when 'pull'
187
- c.push.pull(p['id'], opt(p['size'], 20971520), opt(p['cursor'], ''))
165
+ when 'validate'
166
+ c.push.valid? p, false
167
+ when 'create'
168
+ c.push.create p
169
+ when 'pause'
170
+ c.push.pause p['id']
171
+ when 'resume'
172
+ c.push.resume p['id']
173
+ when 'update'
174
+ c.push.update p
175
+ when 'stop'
176
+ c.push.stop p['id']
177
+ when 'delete'
178
+ c.push.delete p['id']
179
+ when 'log'
180
+ p['id'] ?
181
+ c.push.logs_for(p['id'], opt(p['page'], 0), opt(p['per_page'], 20), opt(p['order_by'], :request_time), opt(p['order_dir'], :desc)) :
182
+ c.push.logs(opt(p['page'], 0), opt(p['per_page'], 20), opt(p['order_by'], :request_time), opt(p['order_dir'], :desc))
183
+ when 'get'
184
+ if p['id']
185
+ c.push.get_by_subscription(p['id'], opt(p['page'], 0), opt(p['per_page'], 20), opt(p['order_by'], :request_time))
186
+ elsif p['hash']
187
+ c.push.get_by_hash(p['hash'], opt(p['page'], 0), opt(p['per_page'], 20), opt(p['order_by'], :request_time), opt(p['order_dir'], :desc), opt(p['include_finished'], 0), opt(p['all'], false))
188
+ elsif p['historics_id']
189
+ c.push.get_by_historics_id(p['historics_id'], opt(p['page'], 0), opt(p['per_page'], 20), opt(p['order_by'], :request_time), opt(p['order_dir'], :desc), opt(p['include_finished'], 0), opt(p['all'], false))
188
190
  else
189
- err 'Unknown command for the core endpoint'
190
- exit
191
+ c.push.get(opt(p['page'], 0), opt(p['per_page'], 20), opt(p['order_by'], :request_time), opt(p['order_dir'], :desc), opt(p['include_finished'], 0), opt(p['all'], false))
192
+ end
193
+ when 'pull'
194
+ c.push.pull(p['id'], opt(p['size'], 20_971_520), opt(p['cursor'], ''))
195
+ else
196
+ err 'Unknown command for the core endpoint'
197
+ exit
198
+ end
199
+ end
200
+
201
+ def run_pylon_command(c, command, p)
202
+ case command
203
+ when 'validate'
204
+ c.pylon.valid?(
205
+ opt(p['csdl'], ''),
206
+ opt(p['boolResponse'], true)
207
+ )
208
+ when 'compile'
209
+ c.pylon.compile(opt(p['csdl'], ''))
210
+ when 'start'
211
+ c.pylon.start(
212
+ opt(p['hash'], ''),
213
+ opt(p['name'], '')
214
+ )
215
+ when 'stop'
216
+ c.pylon.stop(opt(p['hash'], ''))
217
+ when 'get'
218
+ c.pylon.get(opt(p['id'], ''))
219
+ when 'list'
220
+ c.pylon.list(
221
+ opt(p['page'], 0),
222
+ opt(p['per_page'], 20),
223
+ opt(p['order_by'], :created_at),
224
+ opt(p['order_dir'], :desc)
225
+ )
226
+ when 'analyze'
227
+ params = nil
228
+ if p['parameters']
229
+ params = MultiJson.load(p['parameters'])
230
+ end
231
+ c.pylon.analyze(
232
+ opt(p['hash'], ''),
233
+ params,
234
+ opt(p['filter'], ''),
235
+ opt(p['start'], ''),
236
+ opt(p['end'], '')
237
+ )
238
+ when 'tags'
239
+ c.pylon.tags(p['hash'])
240
+ else
241
+ err 'Unknown command for the pylon endpoint'
242
+ exit
243
+ end
244
+ end
245
+
246
+ def run_account_identity_command(c, command, p)
247
+ case command
248
+ when 'create'
249
+ c.account_identity.create(
250
+ opt(p['label'], ''),
251
+ opt(p['status'], ''),
252
+ to_bool(opt(p['master'], ''))
253
+ )
254
+ when 'get'
255
+ c.account_identity.get(opt(p['id'], ''))
256
+ when 'list'
257
+ c.account_identity.list(
258
+ opt(p['label'], ''),
259
+ opt(p['per_page'], ''),
260
+ opt(p['page'], '')
261
+ )
262
+ when 'update'
263
+ c.account_identity.update(
264
+ opt(p['id'], ''),
265
+ opt(p['label'], ''),
266
+ opt(p['status'], ''),
267
+ to_bool(opt(p['master'], ''))
268
+ )
269
+ when 'delete'
270
+ c.account_identity.delete(opt(p['id'], ''))
271
+ else
272
+ err 'Unknown command for the account/identity endpoint'
273
+ exit
274
+ end
275
+ end
276
+
277
+ def run_account_token_command(c, command, p)
278
+ case command
279
+ when 'create'
280
+ c.account_identity_token.create(
281
+ opt(p['identity_id'], ''),
282
+ opt(p['service'], ''),
283
+ opt(p['token'], '')
284
+ )
285
+ when 'get'
286
+ c.account_identity_token.get(
287
+ opt(p['identity_id'], ''),
288
+ opt(p['service'], '')
289
+ )
290
+ when 'list'
291
+ c.account_identity_token.list(
292
+ opt(p['identity_id'], ''),
293
+ opt(p['per_page'], ''),
294
+ opt(p['page'], '')
295
+ )
296
+ when 'update'
297
+ c.account_identity_token.update(
298
+ opt(p['identity_id'], ''),
299
+ opt(p['service'], ''),
300
+ opt(p['token'], '')
301
+ )
302
+ when 'delete'
303
+ c.account_identity_token.delete(
304
+ opt(p['identity_id'], ''),
305
+ opt(p['service'], '')
306
+ )
307
+ else
308
+ err 'Unknown command for the account/identity/token endpoint'
309
+ exit
310
+ end
311
+ end
312
+
313
+ def run_account_limit_command(c, command, p)
314
+ case command
315
+ when 'create'
316
+ c.account_identity_limit.create(
317
+ opt(p['identity_id'], ''),
318
+ opt(p['service'], ''),
319
+ opt(p['total_allowance'], nil)
320
+ )
321
+ when 'get'
322
+ c.account_identity_limit.get(
323
+ opt(p['identity_id'], ''),
324
+ opt(p['service'], '')
325
+ )
326
+ when 'list'
327
+ c.account_identity_limit.list(
328
+ opt(p['service'], ''),
329
+ opt(p['per_page'], ''),
330
+ opt(p['page'], '')
331
+ )
332
+ when 'update'
333
+ c.account_identity_limit.update(
334
+ opt(p['identity_id'], ''),
335
+ opt(p['service'], ''),
336
+ opt(p['total_allowance'], nil)
337
+ )
338
+ when 'delete'
339
+ c.account_identity_limit.delete(
340
+ opt(p['identity_id'], ''),
341
+ opt(p['service'], '')
342
+ )
343
+ else
344
+ err 'Unknown command for the account/identity/limit endpoint'
345
+ exit
346
+ end
347
+ end
348
+
349
+ def to_bool(str)
350
+ if ['true', 'false', '1', '0'].include?(str.to_s.downcase)
351
+ bool = true if ['true', '1'].include?str.to_s.downcase
352
+ bool = false if ['false', '0'].include?str.to_s.downcase
353
+
354
+ return bool
191
355
  end
192
356
  end
193
357
 
@@ -200,34 +364,44 @@ begin
200
364
  err parse(%w(-h))
201
365
  exit
202
366
  end
203
- config =
204
- {
205
- :username => options.auth[:username],
206
- :api_key => options.auth[:api_key],
207
- :api_host => options.api
208
- }
367
+
368
+ config = {
369
+ username: options.auth[:username],
370
+ api_key: options.auth[:api_key],
371
+ api_host: options.api
372
+ }
373
+ config.merge!(enable_ssl: options.enable_ssl) unless options.enable_ssl.nil?
209
374
  datasift = DataSift::Client.new(config)
210
375
 
211
376
  res = case options.endpoint
212
- when 'core'
213
- run_core_command(datasift, options.command, options.params)
214
- when 'historics'
215
- run_historics_command(datasift, options.command, options.params)
216
- when 'push'
217
- run_push_command(datasift, options.command, options.params)
218
- when 'preview'
219
- run_preview_command(datasift, options.command, options.params)
220
- when 'managed_sources'
221
- run_sources_command(datasift, options.command, options.params)
222
- else
223
- err 'Unsupported/Unknown endpoint'
224
- exit
377
+ when 'core'
378
+ run_core_command(datasift, options.command, options.params)
379
+ when 'historics'
380
+ run_historics_command(datasift, options.command, options.params)
381
+ when 'push'
382
+ run_push_command(datasift, options.command, options.params)
383
+ when 'preview'
384
+ run_preview_command(datasift, options.command, options.params)
385
+ when 'managed_sources'
386
+ run_sources_command(datasift, options.command, options.params)
387
+ when 'pylon'
388
+ run_pylon_command(datasift, options.command, options.params)
389
+ when 'identity'
390
+ run_account_identity_command(datasift, options.command, options.params)
391
+ when 'token'
392
+ run_account_token_command(datasift, options.command, options.params)
393
+ when 'limit'
394
+ run_account_limit_command(datasift, options.command, options.params)
395
+ else
396
+ err 'Unsupported/Unknown endpoint'
397
+ exit
225
398
  end
226
399
  puts to_output(res)
400
+
227
401
  rescue DataSiftError => e
228
402
  err e.message
229
403
  rescue OptionParser::InvalidOption, OptionParser::MissingArgument
230
- err $!.to_s
404
+ err $ERROR_INFO.to_s
231
405
  err parse(%w(-h))
232
406
  exit
233
407
  end