viki_utils 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # Viki Utils Gem
2
+
3
+ This is a gem for sharing Ruby version of VIKI's utils
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'viki-api-gem'
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ require 'openssl'
2
+ require 'addressable/uri'
3
+ require 'oj'
4
+
5
+ module Viki
6
+ class UriSigner
7
+
8
+ DIGEST = OpenSSL::Digest::Digest.new('sha1')
9
+
10
+ def initialize(secret)
11
+ @secret = secret
12
+ end
13
+
14
+ def sign_request(uri,body = nil)
15
+ Addressable::URI.parse(uri).tap do |signed_uri|
16
+ query = signed_uri.query_values || {}
17
+ query[:t] = Time.now.to_i
18
+
19
+ signed_uri.query_values = query
20
+ to_sign = [signed_uri.path, signed_uri.query].join('?')
21
+ to_sign += Oj.dump(body) if body
22
+
23
+ sig = OpenSSL::HMAC.hexdigest(DIGEST, @secret, to_sign)
24
+ query.merge!(sig: sig)
25
+ signed_uri.query_values = query
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module VikiUtils
2
+ VERSION = "0.0.1"
3
+ end
data/lib/viki_utils.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'viki_utils/version'
2
+ require 'viki/uri_signer'
@@ -0,0 +1,43 @@
1
+ require_relative "../../lib/viki/uri_signer"
2
+
3
+ require 'timecop'
4
+
5
+ describe Viki::UriSigner do
6
+ describe ".sign_request" do
7
+ let(:uri) { URI("http://example.com/endpoint") }
8
+ let(:secret) { "your_secret" }
9
+
10
+ it 'adds the `t` parameter with the current timestamp' do
11
+ time = Time.now
12
+ Timecop.freeze(time) do
13
+ Viki::UriSigner.new('').sign_request(uri).to_s.should include("t=#{time.to_i.to_s}")
14
+ end
15
+ end
16
+
17
+ it 'adds the `sig` parameter with the signed request' do
18
+ time = Time.now
19
+ Timecop.freeze(time) do
20
+ OpenSSL::HMAC.should_receive(:hexdigest).with(
21
+ Viki::UriSigner::DIGEST,
22
+ secret,
23
+ "/endpoint?t=#{time.to_i.to_s}"
24
+ ).and_return "signature"
25
+ Viki::UriSigner.new(secret).sign_request(uri).to_s.should include("sig=signature")
26
+ end
27
+ end
28
+
29
+ it 'includes the body as part of the signature' do
30
+ time = Time.now
31
+ body = {"body" => "THISISTHEBODY"}
32
+ Timecop.freeze(time) do
33
+ OpenSSL::HMAC.should_receive(:hexdigest).with(
34
+ Viki::UriSigner::DIGEST,
35
+ secret,
36
+ "/endpoint?t=#{time.to_i.to_s}#{Oj.dump(body)}"
37
+ ).and_return "signature"
38
+
39
+ Viki::UriSigner.new(secret).sign_request(uri, body).to_s.should include("sig=signature")
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/viki_utils/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Nuttanart Pornprasitsakul", "Albert Callarisa Roca"]
6
+ gem.email = ["engineering@viki.com"]
7
+ gem.description = %q{This gem provides necessary utilities for interacting with Viki's services.}
8
+ gem.summary = %q{Sharing Viki's utils}
9
+ gem.homepage = "http://dev.viki.com"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "viki_utils"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = VikiUtils::VERSION
17
+
18
+ gem.add_development_dependency "rspec", ">= 2.12.0"
19
+ gem.add_development_dependency "timecop", ">= 0.5.9"
20
+
21
+ gem.add_runtime_dependency "addressable", ">= 2.3"
22
+ gem.add_runtime_dependency "oj", "~> 1.4"
23
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: viki_utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nuttanart Pornprasitsakul
9
+ - Albert Callarisa Roca
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-01-16 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 2.12.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: 2.12.0
31
+ - !ruby/object:Gem::Dependency
32
+ name: timecop
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: 0.5.9
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: 0.5.9
47
+ - !ruby/object:Gem::Dependency
48
+ name: addressable
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '2.3'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '2.3'
63
+ - !ruby/object:Gem::Dependency
64
+ name: oj
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ version: '1.4'
71
+ type: :runtime
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: '1.4'
79
+ description: This gem provides necessary utilities for interacting with Viki's services.
80
+ email:
81
+ - engineering@viki.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - .rspec
88
+ - Gemfile
89
+ - README.md
90
+ - Rakefile
91
+ - lib/viki/uri_signer.rb
92
+ - lib/viki_utils.rb
93
+ - lib/viki_utils/version.rb
94
+ - spec/viki/uri_signer_spec.rb
95
+ - viki_utils.gemspec
96
+ homepage: http://dev.viki.com
97
+ licenses: []
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 1.8.24
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: Sharing Viki's utils
120
+ test_files:
121
+ - spec/viki/uri_signer_spec.rb