usabilla_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/usabilla_api.rb +104 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7488d9c6ee665d9fb6aebf993ffc3a44e7b9b16b
4
+ data.tar.gz: 1e82e57607c8d4f9e99884e0fe96aad38561ad16
5
+ SHA512:
6
+ metadata.gz: 305331d7c621b4bc0dff9e2e72a8213e037ee0363c14cfb0d435646789be3f585616aea065d1845b47531e2f4c7b3edd525d57adeb400c729fe2ef1bcfa8bce4
7
+ data.tar.gz: 38e1b2be7440606d13e42f9cb719523316766415c6cd87c60158529ea88e6e47c8fadeed6ab8cf2065b543a389d6e52db903b1929411d242f7b7dd363ce8dc54
@@ -0,0 +1,104 @@
1
+ require 'rest-client'
2
+ require 'openssl'
3
+
4
+ module Usabilla
5
+ Credentials = Struct.new(:client_key, :secret_key)
6
+
7
+ class APIClient
8
+ attr_accessor :method, :host, :host_protocol, :query_parameters, :credentials
9
+
10
+ def initialize(client_key, secret_key, params)
11
+ @method = 'GET'
12
+ @host = 'data.usabilla.com'
13
+ @host_protocol = 'https://'
14
+ @query_parameters = params
15
+ @credentials = Usabilla::Credentials.new(client_key, secret_key)
16
+ end
17
+
18
+ def get_signature_key(key, long_date)
19
+ k_date = OpenSSL::HMAC.digest('sha256', 'USBL1' + key, long_date)
20
+ k_signing = OpenSSL::HMAC.digest('sha256', k_date, 'usbl1_request')
21
+ k_signing
22
+ end
23
+
24
+
25
+ def set_query_parameters(params)
26
+ @query_parameters = params
27
+ end
28
+
29
+ def get_query_parameters
30
+ @query_parameters
31
+ end
32
+
33
+ def send_signed_request(scope)
34
+
35
+ if @credentials.client_key.nil? || @credentials.secret_key.nil?
36
+ raise 'Invalid Access Key. The Access Key supplied is invalid.'
37
+ end
38
+
39
+ t = DateTime.now
40
+ usbldate = t.strftime('%a, %d %b %Y %H:%M:%S GMT')
41
+ datestamp = t.strftime('%Y%m%d') # Date w/o time, used in credential scope
42
+ long_date = t.strftime('%Y%m%dT%H%M%SZ')
43
+
44
+ canonical_uri = scope
45
+ canonical_querystring = get_query_parameters
46
+ canonical_headers = "date:#{usbldate}\nhost:#{@host}\n"
47
+ signed_headers = 'date;host'
48
+ payload_hash = OpenSSL::Digest::SHA256.new.hexdigest
49
+
50
+ canonical_request = "#{@method}\n#{canonical_uri}\n#{canonical_querystring}\n#{canonical_headers}\n#{signed_headers}\n#{payload_hash}"
51
+
52
+ canonical_request_digest = OpenSSL::Digest::SHA256.new(canonical_request).hexdigest
53
+ algorithm = 'USBL1-HMAC-SHA256'
54
+ credential_scope = "#{datestamp}/usbl1_request"
55
+
56
+ string_to_sign = "#{algorithm}\n#{long_date}\n#{credential_scope}\n#{canonical_request_digest}"
57
+
58
+ signing_key = get_signature_key(@credentials.secret_key, datestamp)
59
+ signature = OpenSSL::HMAC.hexdigest('sha256', signing_key, string_to_sign)
60
+
61
+ authorization_header = "#{algorithm} Credential=#{@credentials.client_key}/#{credential_scope}, SignedHeaders=#{signed_headers}, Signature=#{signature}"
62
+ headers = {'date' => usbldate, 'Authorization' => authorization_header}
63
+
64
+ # Send the request.
65
+ request_url = @host + scope + '?' + canonical_querystring
66
+ response = RestClient.get(@host_protocol + request_url, headers)
67
+ print "#{request_url}"
68
+ response
69
+
70
+ end
71
+
72
+ def get_buttons
73
+ send_signed_request('/live/website/button')
74
+ end
75
+
76
+ def get_feedback_items(button_id='%2A')
77
+ if button_id.nil? || button_id.empty?
78
+ raise 'Invalid button ID'
79
+ end
80
+ if button_id == '*'
81
+ button_id = '%2A'
82
+ end
83
+ feedback_items = send_signed_request("/live/website/button/#{button_id}/feedback")
84
+ feedback_items
85
+
86
+ end
87
+
88
+ def get_campaigns
89
+ send_signed_request('/live/website/campaign')
90
+ end
91
+
92
+ def get_campaign_results(campaign_id=nil)
93
+
94
+ if campaign_id.nil? || campaign_id.empty?
95
+ raise 'Invalid campaign ID'
96
+ end
97
+
98
+ campaign_results = send_signed_request("/live/website/campaign/#{campaign_id}/results")
99
+ campaign_results
100
+
101
+ end
102
+
103
+ end
104
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: usabilla_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jon Molinaro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Ruby Usabilla API Gem that will allow you to connect to and use the
14
+ Usabilla API
15
+ email: Teirson@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/usabilla_api.rb
21
+ homepage: http://rubygems.org/gems/usabilla_api
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.2.2
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Ruby Usabilla API Gem.
45
+ test_files: []