oauth_twitter 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ N2MwNWI3NjEyYTBiNWE1MWRhODJiMjE2YTA3ODIyYzVlM2M2Mzk5MQ==
5
+ data.tar.gz: !binary |-
6
+ MjNiZmMxMjljMGY4ZDQxMTc1ZGU2YWI5NDZiNDJhNjM3YjFhNjc4Mg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YzM4MTcwMDY4ZjhkNzU0YzQzNmU1M2YwMTM3OWYxMDZjYzRjZTNjOGM0MTFm
10
+ ZGYwMmJkMzIyMTYyNDM1ZjhmYjVjYmQ4YjhiNmYyZWUwYWI5Y2EzMWM5YTU1
11
+ M2I4YjQ5YTMyYTAwM2FkYzgxZmY2MjBiN2VjNDY3NjQwNjc4MzE=
12
+ data.tar.gz: !binary |-
13
+ ZTAwZTAzYTE2MTQwNTk4MGFjZmQ1OTAyY2QxMWQ5OWFiZmRmNjNmOWJkZDdi
14
+ ZjdiMDUxMzQ1MzBmZGQwZjhkODg0Nzk3MmE5YmJhNzQ5NjIxZTY1MDIyYWNm
15
+ YjkwZjQ2Y2IyNDE4YjRmOTdjM2UxMjQxMzJmZTM3MDIzMjIzNTM=
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in oauth_twitter.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Daiwei Lu
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,29 @@
1
+ # OauthTwitter
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'oauth_twitter'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install oauth_twitter
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ module OauthTwitter
2
+ module API
3
+ module Friends
4
+ PATH = {
5
+ friends_ids: '/1.1/friends/ids.json'
6
+ }
7
+
8
+ def friends_ids(user_id)
9
+ oauth = oauth_params(true)
10
+ query = {
11
+ user_id: user_id,
12
+ count: 5000
13
+ }
14
+ return send_request(:GET, PATH[:friends_ids], query, oauth)
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ module OauthTwitter
2
+ module API
3
+ module Oauth
4
+ PATH = {
5
+ request_token: '/oauth/request_token',
6
+ access_token: '/oauth/access_token'
7
+ }
8
+
9
+ def request_token(oauth_callback=nil)
10
+ oauth = oauth_params(
11
+ false,
12
+ {oauth_callback: Config.oauth_callback})
13
+ return send_request(:POST, PATH[:request_token], nil, oauth)
14
+ end
15
+
16
+ def access_token(oauth_verifier)
17
+ oauth = oauth_params(true)
18
+ return send_request(:POST, PATH[:access_token], {oauth_verifier: oauth_verifier}, oauth)
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module OauthTwitter
2
+ module API
3
+ module Statuses
4
+ PATH = {
5
+ home_timeline: "/1.1/statuses/home_timeline.json"
6
+ }
7
+
8
+ def home_timeline(since_id=nil)
9
+ oauth = oauth_params(true)
10
+ query = {
11
+ count: 200,
12
+ trim_user: false,
13
+ exclude_replies: true,
14
+ contributor_details: false,
15
+ include_entities: true
16
+ }
17
+ query[:since_id] = since_id if since_id
18
+ return send_request(:GET, PATH[:home_timeline], query, oauth)
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,29 @@
1
+ module OauthTwitter
2
+ module API
3
+ module Users
4
+ PATH = {
5
+ users_lookup: "/1.1/users/lookup.json"
6
+ }
7
+
8
+ def users_lookup(id_array, include_entities=true)
9
+ oauth = oauth_params(true)
10
+ # slice id_array for multiple request
11
+ num_of_set = id_array.size / 100
12
+ num_of_set += 1 if id_array.size % 100 > 0
13
+ id_sets = num_of_set.times.map {|i| id_array.slice(i*100, (i+1)*100)}
14
+ # send request
15
+ result = []
16
+ id_sets.each do |set|
17
+ query = {
18
+ user_id: set.join(','),
19
+ include_entities: include_entities
20
+ }
21
+ method = set.size <= 10 ? :GET : :POST
22
+ result += send_request(method, PATH[:users_lookup], query, oauth)
23
+ end
24
+ return result
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,11 @@
1
+ module OauthTwitter
2
+ module Config
3
+ class << self
4
+ attr_accessor :consumer_key, :consumer_secret, :oauth_callback
5
+
6
+ def setup
7
+ yield self
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,88 @@
1
+ require "securerandom"
2
+ require "openssl"
3
+ require "base64"
4
+ require "uri"
5
+ require "net/http"
6
+ require "json"
7
+
8
+ module OauthTwitter
9
+ module Helper
10
+ def oauth_params(include_oauth_token=true, addional_oauth_params={})
11
+ oauth = {
12
+ oauth_consumer_key: Config.consumer_key,
13
+ oauth_nonce: SecureRandom.hex(21),
14
+ oauth_signature_method: "HMAC-SHA1",
15
+ oauth_timestamp: Time.now.to_i,
16
+ oauth_version: "1.0"
17
+ }
18
+ oauth[:oauth_token] = self.oauth_token if include_oauth_token
19
+ return oauth.merge(addional_oauth_params)
20
+ end
21
+
22
+ RESERVED_CHARS = /[^a-zA-Z0-9\-\.\_\~]/
23
+ def self.percent_encode(raw)
24
+ return URI.escape(raw.to_s, RESERVED_CHARS)
25
+ end
26
+
27
+ HOST = "https://api.twitter.com"
28
+ def send_request(method, path, query, oauth)
29
+ # Make base_str and signing_key
30
+ base_str = method.to_s.upcase << "&"
31
+ base_str << Helper.percent_encode(HOST + path) << "&"
32
+ hash = query ? oauth.merge(query) : oauth
33
+ array = hash.sort.map {|key, val| Helper.percent_encode(key) + "=" + Helper.percent_encode(val)}
34
+ base_str << Helper.percent_encode(array.join("&"))
35
+ # Sign
36
+ signing_key = String.new(Config.consumer_secret) << "&"
37
+ signing_key << self.oauth_token_secret if hash[:oauth_token]
38
+ signature = Helper.sign(base_str, signing_key)
39
+ signed_oauth = oauth.merge(oauth_signature: signature)
40
+ # Header
41
+ auth_header = Helper.auth_header(signed_oauth)
42
+ # HTTP request
43
+ uri = URI.parse(HOST + path)
44
+ https = Net::HTTP.new(uri.host, uri.port)
45
+ https.use_ssl = true
46
+ case
47
+ when method.to_s.upcase == "POST"
48
+ request = Net::HTTP::Post.new(uri.request_uri)
49
+ request.set_form_data(query) if query
50
+ when method.to_s.upcase == "GET"
51
+ uri.query = URI.encode_www_form(query) if query
52
+ request = Net::HTTP::Get.new(uri.request_uri)
53
+ end
54
+ request["Authorization"] = auth_header
55
+ # Response
56
+ response = https.request(request)
57
+ case response.code
58
+ when "200"
59
+ begin
60
+ return JSON.parse(response.body)
61
+ rescue JSON::ParserError
62
+ return Rack::Utils.parse_nested_query(response.body)
63
+ end
64
+ when "401"
65
+ return false
66
+ when "408"
67
+ return false
68
+ else
69
+ p response.code, response.body
70
+ raise "HTTP request failed."
71
+ end
72
+ end
73
+
74
+ def self.sign(base_str, signing_key)
75
+ hex_str = OpenSSL::HMAC.hexdigest(
76
+ OpenSSL::Digest::Digest.new('sha1'),
77
+ signing_key,
78
+ base_str)
79
+ binary_str = Base64.encode64( [hex_str].pack("H*") ).gsub(/\n/, "")
80
+ return Helper.percent_encode( binary_str )
81
+ end
82
+
83
+ def self.auth_header(signed_oauth)
84
+ params = signed_oauth.map { |key, val| "#{key}=\"#{val}\"" }
85
+ return "OAuth " << params.join(",")
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,3 @@
1
+ module OauthTwitter
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,15 @@
1
+ require "oauth_twitter/version"
2
+ require "oauth_twitter/config"
3
+ require "oauth_twitter/helper"
4
+ require "oauth_twitter/api/oauth"
5
+ require "oauth_twitter/api/statuses"
6
+ require "oauth_twitter/api/friends"
7
+ require "oauth_twitter/api/users"
8
+
9
+ module OauthTwitter
10
+ include Helper
11
+ include API::Oauth
12
+ include API::Statuses
13
+ include API::Friends
14
+ include API::Users
15
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'oauth_twitter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "oauth_twitter"
8
+ spec.version = OauthTwitter::VERSION
9
+ spec.authors = ["Daiwei Lu"]
10
+ spec.email = ["daiweilu123@gmail.com"]
11
+ spec.description = %q{Simple twitter oauth api to add methods to module for rails module.}
12
+ spec.summary = %q{Include module to add methods for rails module.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_dependency 'rack', '>= 1.4.5'
24
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oauth_twitter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daiwei Lu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-11 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.4.5
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.4.5
55
+ description: Simple twitter oauth api to add methods to module for rails module.
56
+ email:
57
+ - daiweilu123@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/oauth_twitter.rb
68
+ - lib/oauth_twitter/api/friends.rb
69
+ - lib/oauth_twitter/api/oauth.rb
70
+ - lib/oauth_twitter/api/statuses.rb
71
+ - lib/oauth_twitter/api/users.rb
72
+ - lib/oauth_twitter/config.rb
73
+ - lib/oauth_twitter/helper.rb
74
+ - lib/oauth_twitter/version.rb
75
+ - oauth_twitter.gemspec
76
+ homepage: ''
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.0.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Include module to add methods for rails module.
100
+ test_files: []