twitter-request-headers 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7eb6fec8fecbd186132ee9ead43797f2b47c525d
4
+ data.tar.gz: 642a343866131b6bffd11e3c0fab3f8f98f339e0
5
+ SHA512:
6
+ metadata.gz: 34535137f9df376819d1c3f6908b20c45254b68bc40e69f839db21067b71ac5a6f93f018f9277edcf165389f150bf6360bef0b172e1ee5fd0e52cbc26eb882a6
7
+ data.tar.gz: 0bcf733afc3a6bb3a6c56cc6743985c49e95d83a6cfca4dfe102581967a6495435099afb70fa812840a14a1e96ada6b873f88f9e06b9e195295c524c72b0756b
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Dmitriy K.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # TwitterRequestHeaders
2
+
3
+ This gem is intended to easily format Twitter API's `Authorization` HTTP header.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'twitter-request-headers'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install twitter-request-headers
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ TwitterRequestHeaders.configure(
25
+ 'KWZqnnFc8PPMjHOmXUpYNAMlI', # App ID
26
+ '7HqOlwq7petgYbZwYIznWvaW6gxykFEoiWTliNMCNKosut5VXS' # App Secret
27
+ )
28
+
29
+ TwitterRequestHeaders.new(
30
+ '150425432-3dAJqsCUXstN1kKgMwpFQGy9JtW6KgBeD30C1b4R', # OAuth Token
31
+ 'YHwc8jkR2pKAISscibQdymNKaXR8dzDsAabQknaJdYs9l', # OAuth Secret
32
+ 'GET',
33
+ '/users/show.json',
34
+ {'user_id' => '110425482'}
35
+ ).header
36
+ ```
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "twitter_request_headers"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,45 @@
1
+ =begin
2
+ Twitter requires specific request encoding. More details at this link:
3
+ https://dev.twitter.com/oauth/overview/percent-encoding-parameters
4
+ =end
5
+
6
+ require 'uri'
7
+
8
+ class EscapeUriString
9
+ RESERVED_CHARACTERS = /[^a-zA-Z0-9\-\.\_\~]/
10
+
11
+ def initialize(string)
12
+ @string = string
13
+ validate
14
+ end
15
+
16
+ def escape
17
+ escaped = escape_uri(@string) || escape_uri(@string.force_encoding(Encoding::UTF_8))
18
+
19
+ unless escaped
20
+ fail ArgumentError, "Unable to escape string: #{@string}"
21
+ end
22
+
23
+ escaped
24
+ end
25
+
26
+ private
27
+
28
+ def validate
29
+ unless @string.kind_of?(String)
30
+ fail ArgumentError, 'Attempt to escape non-string'
31
+ end
32
+
33
+ if @string.nil? || @string.empty?
34
+ fail ArgumentError, 'Attempt to escape empty string'
35
+ end
36
+ end
37
+
38
+ def escape_uri(string)
39
+ begin
40
+ URI::escape(string, RESERVED_CHARACTERS)
41
+ rescue ArgumentError
42
+ rescue
43
+ end
44
+ end
45
+ end
data/lib/header.rb ADDED
@@ -0,0 +1,34 @@
1
+ class Header
2
+ require 'twitter_request_headers'
3
+ require 'escape_uri_string'
4
+
5
+ def self.key
6
+ 'Authorization'
7
+ end
8
+
9
+ def initialize(oauth_token, signature, nonce, epochtime)
10
+ @oauth_token = oauth_token
11
+ @signature = signature
12
+
13
+ @nonce = nonce
14
+ @epochtime = epochtime
15
+ end
16
+
17
+ def value
18
+ <<-EOF
19
+ OAuth #{escape('oauth_consumer_key')}="#{escape(TwitterRequestHeaders.consumer_key)}",
20
+ #{escape('oauth_nonce')}="#{escape(@nonce)}",
21
+ #{escape('oauth_signature')}="#{escape(@signature)}",
22
+ #{escape('oauth_signature_method')}="#{escape(TwitterRequestHeaders.oauth_cipher)}",
23
+ #{escape('oauth_timestamp')}="#{escape(@epochtime)}",
24
+ #{escape('oauth_token')}="#{escape(@oauth_token)}",
25
+ #{escape('oauth_version')}="#{escape(TwitterRequestHeaders.oauth_version)}"
26
+ EOF
27
+ end
28
+
29
+ private
30
+
31
+ def escape(string)
32
+ EscapeUriString.new(string).escape
33
+ end
34
+ end
data/lib/signature.rb ADDED
@@ -0,0 +1,40 @@
1
+ class Signature
2
+ require 'securerandom'
3
+ require 'base64'
4
+
5
+ require 'signing_key'
6
+ require 'signing_base'
7
+ require 'signature_params'
8
+
9
+ def initialize(oauth_token, oauth_secret, request_verb, request_path, request_params, nonce, epochtime)
10
+ @oauth_token = oauth_token
11
+ @oauth_secret = oauth_secret
12
+
13
+ @request_verb = request_verb
14
+ @request_path = request_path
15
+ @request_params = request_params
16
+
17
+ @nonce = nonce
18
+ @epochtime = epochtime
19
+ end
20
+
21
+ def digest
22
+ request_digest = OpenSSL::HMAC.hexdigest('sha1', signing_key, signing_base)
23
+
24
+ Base64.encode64(Array(request_digest).pack('H*')).chomp
25
+ end
26
+
27
+ private
28
+
29
+ def signing_key
30
+ SigningKey.new(@oauth_secret).key
31
+ end
32
+
33
+ def signing_base
34
+ SigningBase.new(@request_verb, @request_path, signature_params).base_string
35
+ end
36
+
37
+ def signature_params
38
+ SignatureParams.new(@oauth_token, @nonce, @epochtime, @request_params).params
39
+ end
40
+ end
@@ -0,0 +1,36 @@
1
+ class SignatureParams
2
+ require 'active_support'
3
+ require 'active_support/core_ext'
4
+
5
+ require 'twitter_request_headers'
6
+ require 'escape_uri_string'
7
+
8
+ def initialize(oauth_token, nonce, epochtime, request_params)
9
+ @oauth_token = oauth_token
10
+ @nonce = nonce
11
+ @epochtime = epochtime
12
+ @request_params = request_params.stringify_keys
13
+ end
14
+
15
+ def params
16
+ params_hash = {
17
+ 'oauth_consumer_key' => TwitterRequestHeaders.consumer_key,
18
+ 'oauth_signature_method' => TwitterRequestHeaders.oauth_cipher,
19
+ 'oauth_version' => TwitterRequestHeaders.oauth_version,
20
+ 'oauth_token' => @oauth_token,
21
+ 'oauth_nonce' => @nonce,
22
+ 'oauth_timestamp' => @epochtime
23
+ }
24
+
25
+ params_hash.merge!(@request_params)
26
+
27
+ params_array = params_hash.sort.to_h.map do |key, val|
28
+ escaped_key = EscapeUriString.new(key).escape
29
+ escaped_val = EscapeUriString.new(val).escape
30
+
31
+ "#{escaped_key}=#{escaped_val}"
32
+ end
33
+
34
+ params_array.join('&')
35
+ end
36
+ end
@@ -0,0 +1,19 @@
1
+ class SigningBase
2
+ require 'twitter_request_headers'
3
+ require 'escape_uri_string'
4
+
5
+ def initialize(request_verb, request_path, signature_params)
6
+ @twitter_api = TwitterRequestHeaders.twitter_api
7
+ @request_verb = request_verb.upcase
8
+ @request_path = request_path
9
+ @signature_params = signature_params
10
+ end
11
+
12
+ def base_string
13
+ request_uri = "#{@twitter_api}#{@request_path}"
14
+ escaped_uri = EscapeUriString.new(request_uri).escape
15
+ escaped_params = EscapeUriString.new(@signature_params).escape
16
+
17
+ "#{@request_verb}&#{escaped_uri}&#{escaped_params}"
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ class SigningKey
2
+ require 'twitter_request_headers'
3
+ require 'escape_uri_string'
4
+
5
+ def initialize(oauth_secret)
6
+ @consumer_secret = TwitterRequestHeaders.consumer_secret
7
+ @oauth_secret = oauth_secret
8
+ end
9
+
10
+ def key
11
+ consumer_secret = EscapeUriString.new(@consumer_secret).escape
12
+ oauth_secret = EscapeUriString.new(@oauth_secret).escape
13
+
14
+ "#{consumer_secret}&#{oauth_secret}"
15
+ end
16
+ end
@@ -0,0 +1,66 @@
1
+ class TwitterRequestHeaders
2
+ require 'signature'
3
+ require 'header'
4
+
5
+ OAUTH_VERSION = '1.0'
6
+ OAUTH_CIPHER = 'HMAC-SHA1'
7
+ TWITTER_API = 'https://api.twitter.com/1.1'
8
+
9
+ class << self
10
+ attr_reader :consumer_key, :consumer_secret, :oauth_version, :oauth_cipher, :twitter_api
11
+
12
+ def configure(consumer_key, consumer_secret, oauth_version = nil, oauth_cipher = nil, twitter_api = nil)
13
+ @consumer_key = consumer_key
14
+ @consumer_secret = consumer_secret
15
+
16
+ @oauth_version = oauth_version || OAUTH_VERSION
17
+ @oauth_cipher = oauth_cipher || OAUTH_CIPHER
18
+ @twitter_api = twitter_api || TWITTER_API
19
+
20
+ @@configured = true
21
+ end
22
+ end
23
+
24
+
25
+ def initialize(oauth_token, oauth_secret, request_verb, request_path, request_params = nil)
26
+ unless @@configured
27
+ fail StandardError, "Call #{self.class}.configure first!"
28
+ end
29
+
30
+ @oauth_token = oauth_token
31
+ @oauth_secret = oauth_secret
32
+
33
+ @request_verb = request_verb
34
+ @request_path = request_path
35
+ @request_params = request_params || {}
36
+
37
+ @nonce = nonce
38
+ @epochtime = epochtime
39
+ end
40
+
41
+ def header
42
+ signature = Signature.new(
43
+ @oauth_token,
44
+ @oauth_secret,
45
+ @request_verb,
46
+ @request_path,
47
+ @request_params,
48
+ @nonce,
49
+ @epochtime
50
+ ).digest
51
+
52
+ {
53
+ Header.key => Header.new(@oauth_token, signature, @nonce, @epochtime).value
54
+ }
55
+ end
56
+
57
+ private
58
+
59
+ def nonce
60
+ SecureRandom.hex(16)
61
+ end
62
+
63
+ def epochtime
64
+ Time.now.to_i.to_s
65
+ end
66
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "twitter-request-headers"
7
+ spec.version = '0.0.10'
8
+ spec.authors = ["Dmitrii Komaritckii"]
9
+ spec.email = ["lochnessathome@gmail.com"]
10
+
11
+ spec.summary = %q{Generates Twitter API's Authorization header}
12
+ spec.homepage = "https://github.com/lochnessathome/twitter-request-headers"
13
+ spec.license = "MIT"
14
+
15
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
16
+ # delete this section to allow pushing this gem to any host.
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
19
+ else
20
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
21
+ end
22
+
23
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.10'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'minitest', '~> 5.8'
31
+
32
+ spec.add_runtime_dependency 'activesupport', '~> 4.0'
33
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twitter-request-headers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.10
5
+ platform: ruby
6
+ authors:
7
+ - Dmitrii Komaritckii
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ description:
70
+ email:
71
+ - lochnessathome@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/escape_uri_string.rb
86
+ - lib/header.rb
87
+ - lib/signature.rb
88
+ - lib/signature_params.rb
89
+ - lib/signing_base.rb
90
+ - lib/signing_key.rb
91
+ - lib/twitter_request_headers.rb
92
+ - twitter-request-headers.gemspec
93
+ homepage: https://github.com/lochnessathome/twitter-request-headers
94
+ licenses:
95
+ - MIT
96
+ metadata:
97
+ allowed_push_host: https://rubygems.org
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.4.6
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Generates Twitter API's Authorization header
118
+ test_files: []
119
+ has_rdoc: