TwitteRuby 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/twitteruby.rb +51 -0
- metadata +65 -0
data/lib/twitteruby.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
class TwitteRuby
|
2
|
+
require "oauth"
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
#pass in oauth credentials and base for API URIs
|
6
|
+
def initialize(oauth_consumer_key, oauth_consumer_secret, oauth_token, oauth_token_secret, api_base)
|
7
|
+
@api_base = api_base # the base of all URIs for this API
|
8
|
+
# Exchange our oauth_token and oauth_token secret for the AccessToken instance
|
9
|
+
@access_token = get_access_token(oauth_consumer_key, oauth_consumer_secret, oauth_token, oauth_token_secret)
|
10
|
+
end
|
11
|
+
|
12
|
+
#pass in desired HTTP method, API method to call, and parameters
|
13
|
+
def request(method, call, args)
|
14
|
+
call = "#{@api_base}#{call}.json?" #it's OK to have an extraneous ? symbol in case of no args
|
15
|
+
params = {} #parameters used in POST calls, but passed empty to GET
|
16
|
+
if(method == :get) #modify the URL for GET calls
|
17
|
+
args.each{|arg| arg.map{|k,v| call += "#{k}=#{v}&"}} #extraneous ampersands are OK
|
18
|
+
else #build full hash of all parameters for POST calls
|
19
|
+
args.each{|arg| params = params.merge(arg)}
|
20
|
+
end
|
21
|
+
return @access_token.request(method, call, params).body
|
22
|
+
end
|
23
|
+
|
24
|
+
#refactor all API calls to methods with method_missing
|
25
|
+
def method_missing(method_name, *args)
|
26
|
+
return request($1.to_sym, $2.gsub(/__/, '/'), args) if method_name =~ /^(post|get)_(.*)/ #only provide support for post_ and get_ missing methods
|
27
|
+
super #chain to constructor incase not a post_ or get_
|
28
|
+
end
|
29
|
+
|
30
|
+
#parse JSON into a Ruby data structure
|
31
|
+
def parseJSON(json)
|
32
|
+
return JSON.parse(json)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
# Exchange our oauth_token and oauth_token_secret for an AccessToken instance.
|
38
|
+
# Code written by Twitter Inc.
|
39
|
+
def get_access_token(oauth_consumer_key, oauth_consumer_secret, oauth_token, oauth_token_secret)
|
40
|
+
consumer = OAuth::Consumer.new(oauth_consumer_key, oauth_consumer_secret,
|
41
|
+
{ :site => "http://api.twitter.com",
|
42
|
+
:scheme => :header
|
43
|
+
})
|
44
|
+
# now create the access token object from passed values
|
45
|
+
token_hash = { :oauth_token => oauth_token,
|
46
|
+
:oauth_token_secret => oauth_token_secret
|
47
|
+
}
|
48
|
+
access_token = OAuth::AccessToken.from_hash(consumer, token_hash )
|
49
|
+
return access_token
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: TwitteRuby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jeremiah Johnson
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2014-02-17 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: A Ruby reflection based wrapper library for the Twitter API
|
22
|
+
email: jeremiah@jwjdev.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/twitteruby.rb
|
31
|
+
homepage:
|
32
|
+
licenses:
|
33
|
+
- MIT
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.21
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Twitter API Wrapper
|
64
|
+
test_files: []
|
65
|
+
|