twitpic-full 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/twitpic.rb ADDED
@@ -0,0 +1,16 @@
1
+ module TwitPic; end
2
+
3
+ # Helper for Rails < 1.9.x
4
+ def require_local(path)
5
+ require(File.expand_path(File.join(File.dirname(__FILE__), path)))
6
+ end
7
+
8
+ # Load the required libraries
9
+ require 'nestful'
10
+ require 'json'
11
+ require 'roauth'
12
+
13
+ # Load the TwitPic API library
14
+ require_local 'twitpic/config'
15
+ require_local 'twitpic/api'
16
+ require_local 'twitpic/client'
@@ -0,0 +1,115 @@
1
+ Dir.glob(File.dirname(__FILE__) + '/api/*') {|file| require file}
2
+
3
+ module TwitPic
4
+ module API
5
+ API_BASE = 'http://api.twitpic.com/2/'
6
+
7
+ class << self
8
+
9
+ public
10
+
11
+ def query(client, call, args)
12
+ self.validate(call, args)
13
+
14
+ if call[:method] == :get then
15
+ self.get(call[:endpoint], args)
16
+ else
17
+ self.post(client, call[:endpoint], args)
18
+ end
19
+ end
20
+
21
+ # Uploads an image to the TwitPic API
22
+ #
23
+ # File can be either the path to the image or
24
+ # a File object
25
+ def upload(client, file, args)
26
+ file = File.open(file) if file.instance_of? String
27
+
28
+ args[:media] = file
29
+
30
+ self.post(client, 'upload', args)
31
+ end
32
+
33
+ def tweet(client, media)
34
+ raise ArgumentError, "Invalid argument, must be an object returned from a photo upload" unless media.has_key? 'text'
35
+
36
+ tweet = "#{media['text'][0..114]} #{media['url']}"
37
+ url = 'http://api.twitter.com/1/statuses/update.json';
38
+ headers = TwitPic::API.build_header(client, tweet)
39
+
40
+ opts = {
41
+ :headers => headers,
42
+ :params => {
43
+ 'status' => tweet
44
+ }
45
+ }
46
+
47
+ data = Nestful.post(url, opts)
48
+
49
+ return media, JSON.parse(data)
50
+ end
51
+
52
+ protected
53
+
54
+ def get(endpoint, args = {})
55
+ url = API_BASE + endpoint + ".json"
56
+ data = Nestful.get(url, :params => args)
57
+ JSON.parse(data)
58
+ end
59
+
60
+ def post(client, endpoint, args = {})
61
+ url = API_BASE + endpoint + ".json"
62
+
63
+ # Add API key to arguments
64
+ args['key'] = client.config.api_key
65
+
66
+ headers = TwitPic::API.build_header(client)
67
+ opts = {
68
+ :headers => headers,
69
+ :params => args
70
+ }
71
+
72
+ if endpoint == 'upload' then
73
+ opts[:format] = :multipart
74
+ end
75
+
76
+ data = Nestful.post(url, opts)
77
+ JSON.parse(data)
78
+ end
79
+
80
+ def build_header(client, tweet=nil)
81
+ raise RuntimeError, "Missing application or OAuth credentials" unless client.write_enabled
82
+
83
+ info = {
84
+ :access_key => client.config.oauth_token,
85
+ :access_secret => client.config.oauth_secret,
86
+ :consumer_key => client.config.consumer_key,
87
+ :consumer_secret => client.config.consumer_secret,
88
+ :signature_method => "HMAC-SHA1"
89
+ }
90
+
91
+ if tweet then
92
+ oauth_url = "http://api.twitter.com/1/statuses/update.json"
93
+ result = ::ROAuth.header(info, oauth_url, {'status' => tweet}, :post)
94
+
95
+ headers = {
96
+ 'Authorization' => result
97
+ }
98
+ else
99
+ oauth_url = "https://api.twitter.com/1/account/verify_credentials.json"
100
+ result = ::ROAuth.header(info, oauth_url, {}, :get)
101
+
102
+ headers = {
103
+ 'X-Verify-Credentials-Authorization' => result
104
+ }
105
+ end
106
+ end
107
+
108
+ def validate(info, args)
109
+ info[:required].each do |item|
110
+ raise ArgumentError, "Missing required argument " + item + " for " + info[:endpoint] unless args.keys.member?(item)
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,27 @@
1
+ class TwitPic::Client
2
+ @@COMMENTS_API = {
3
+ :show => {
4
+ :endpoint => 'comments/show',
5
+ :method => :get,
6
+ :required => [:media_id, :page]
7
+ },
8
+
9
+ :create => {
10
+ :endpoint => 'comments/create',
11
+ :method => :post,
12
+ :required => [:media_id, :message]
13
+ },
14
+
15
+ :delete => {
16
+ :endpoint => 'comments/delete',
17
+ :method => :post,
18
+ :required => [:comment_id]
19
+ }
20
+ }
21
+
22
+ def comments(action, args)
23
+ raise ArgumentError, "Invalid API action given" unless @@COMMENTS_API.keys.member?(action)
24
+ TwitPic::API.query(self, @@COMMENTS_API[action], args)
25
+ end
26
+
27
+ end
@@ -0,0 +1,52 @@
1
+ class TwitPic::Client
2
+ @@EVENTS_API = {
3
+ :show => {
4
+ :endpoint => 'events/show',
5
+ :method => :get,
6
+ :required => [:user]
7
+ }
8
+ }
9
+
10
+ @@EVENT_API = {
11
+ :show => {
12
+ :endpoint => 'event/show',
13
+ :method => :get,
14
+ :required => [:id]
15
+ },
16
+
17
+ :create => {
18
+ :endpoint => 'event/create',
19
+ :method => :post,
20
+ :required => [:name]
21
+ },
22
+
23
+ :delete => {
24
+ :endpoint => 'event/delete',
25
+ :method => :post,
26
+ :required => [:event_id]
27
+ },
28
+
29
+ :add => {
30
+ :endpoint => 'event/add',
31
+ :method => :post,
32
+ :required => [:media_id, :event_id]
33
+ },
34
+
35
+ :remove => {
36
+ :endpoint => 'event/add',
37
+ :mehtod => :post,
38
+ :required => [:event_id, :media_id]
39
+ }
40
+ }
41
+
42
+ def events(action, args)
43
+ raise ArgumentError, "Invalid API action given" unless @@EVENTS_API.keys.member?(action)
44
+ TwitPic::API.query(self, @@EVENTS_API[action], args)
45
+ end
46
+
47
+ def event(action, args)
48
+ raise ArgumentError, "Invalid API action given" unless @@EVENT_API.keys.member?(action)
49
+ TwitPic::API.query(self, @@EVENT_API[action], args)
50
+ end
51
+
52
+ end
@@ -0,0 +1,33 @@
1
+ class TwitPic::Client
2
+ @@FACES_API = {
3
+ :show => {
4
+ :endpoint => 'faces/show',
5
+ :method => :post,
6
+ :required => [:user]
7
+ },
8
+
9
+ :create => {
10
+ :endpoint => 'faces/create',
11
+ :method => :post,
12
+ :required => [:media_id, :top_coord, :left_coord]
13
+ },
14
+
15
+ :edit => {
16
+ :endpoint => 'faces/edit',
17
+ :method => :post,
18
+ :required => [:tag_id]
19
+ },
20
+
21
+ :delete => {
22
+ :endpoint => 'faces/delete',
23
+ :method => :post,
24
+ :required => [:tag_id]
25
+ }
26
+ }
27
+
28
+ def faces(action, args)
29
+ raise ArgumentError, "Invalid API action given" unless @@FACES_API.keys.member?(action)
30
+ TwitPic::API.query(self, @@FACES_API[action], args)
31
+ end
32
+
33
+ end
@@ -0,0 +1,17 @@
1
+ ### Added by Clipflakes
2
+
3
+ class TwitPic::Client
4
+ @@MEDIA_API = {
5
+ :show => {
6
+ :endpoint => 'media/show',
7
+ :method => :get,
8
+ :required => [:id]
9
+ }
10
+ }
11
+
12
+ def media(action, args)
13
+ raise ArgumentError, "Invalid API action given" unless @@MEDIA_API.keys.member?(action)
14
+ TwitPic::API.query(self, @@MEDIA_API[action], args)
15
+ end
16
+
17
+ end
@@ -0,0 +1,28 @@
1
+ class TwitPic::Client
2
+ @@PLACE_API = {
3
+ :show => {
4
+ :endpoint => 'place/show',
5
+ :method => :get,
6
+ :required => [:id]
7
+ }
8
+ }
9
+
10
+ @@PLACES_API = {
11
+ :show => {
12
+ :endpoint => 'places/show',
13
+ :method => :get,
14
+ :required => [:user]
15
+ }
16
+ }
17
+
18
+ def place(action, args)
19
+ raise ArgumentError, "Invalid API action given" unless @@PLACE_API.keys.member?(action)
20
+ TwitPic::API.query(self, @@PLACE_API[action], args)
21
+ end
22
+
23
+ def places(action, args)
24
+ raise ArgumentError, "Invalid API action given" unless @@PLACES_API.keys.member?(action)
25
+ TwitPic::API.query(self, @@PLACES_API[action], args)
26
+ end
27
+
28
+ end
@@ -0,0 +1,27 @@
1
+ class TwitPic::Client
2
+ @@TAGS_API = {
3
+ :show => {
4
+ :endpoint => 'tags/show',
5
+ :method => :get,
6
+ :required => [:tag]
7
+ },
8
+
9
+ :create => {
10
+ :endpoint => 'tags/create',
11
+ :method => :post,
12
+ :required => [:media_id, :tags]
13
+ },
14
+
15
+ :delete => {
16
+ :endpoint => 'tags/delete',
17
+ :method => :post,
18
+ :required => [:media_id, :tag_id]
19
+ }
20
+ }
21
+
22
+ def tags(action, args)
23
+ raise ArgumentError, "Invalid API action given" unless @@TAGS_API.keys.member?(action)
24
+ TwitPic::API.query(self, @@TAGS_API[action], args)
25
+ end
26
+
27
+ end
@@ -0,0 +1,19 @@
1
+ class TwitPic::Client
2
+
3
+ # Uploads a photo to TwitPic
4
+ def upload(file, message)
5
+ message = "" unless message.strip.length > 0
6
+
7
+ TwitPic::API.upload(self, file, {:message => message})
8
+ end
9
+
10
+ def upload_and_post(file, message)
11
+ message = "" unless message.strip.length > 0
12
+
13
+ media = TwitPic::API.upload(self, file, {:message => message})
14
+ if media then
15
+ TwitPic::API.tweet(self, media)
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,15 @@
1
+ class TwitPic::Client
2
+ @@USERS_API = {
3
+ :show => {
4
+ :endpoint => 'users/show',
5
+ :method => :get,
6
+ :required => [:username]
7
+ }
8
+ }
9
+
10
+ def users(action, args)
11
+ raise ArgumentError, "Invalid API action given" unless @@USERS_API.keys.member?(action)
12
+ TwitPic::API.query(self, @@USERS_API[action], args)
13
+ end
14
+
15
+ end
@@ -0,0 +1,28 @@
1
+ module TwitPic
2
+ # The base interaction class. All of the API methods are called via
3
+ # the client class once a Client object is created.
4
+ class Client
5
+
6
+ attr_reader :config, :write_enabled
7
+
8
+ def initialize
9
+ @config = TwitPic::Config.new
10
+ end
11
+
12
+ # Lets you change configuration options
13
+ def configure(&block)
14
+ raise ArgumentError, "Block required in order to configure" unless block_given?
15
+
16
+ yield @config
17
+ end
18
+
19
+ # Determines whether all of the required information has been entered into the config
20
+ # so we know if we can make a write-enabled call or not. Is this too ghetto/unsafe?
21
+ def write_enabled
22
+ enabled = true
23
+ TwitPic::Config.instance_methods.grep(/\w=$/).each do |method|
24
+ enabled = false unless @config.send(method.to_s.chop).strip.length > 0
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,14 @@
1
+ module TwitPic
2
+ class Config
3
+ attr_accessor :api_key, :consumer_key, :consumer_secret, :oauth_token, :oauth_secret
4
+
5
+ # Setup the default configuration data
6
+ def initialize
7
+ @api_key = ''
8
+ @consumer_key = ''
9
+ @consumer_secret = ''
10
+ @oauth_token = ''
11
+ @oauth_secret = ''
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twitpic-full
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Ryan LeFevre
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-23 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: nestful
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: json
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :runtime
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: roauth
39
+ prerelease: false
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id003
48
+ description: Provides full and simple to use access to the TwitPic API, including photo uploads
49
+ email: meltingice8917@gmail.com
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ extra_rdoc_files: []
55
+
56
+ files:
57
+ - lib/twitpic/api/comments.rb
58
+ - lib/twitpic/api/events.rb
59
+ - lib/twitpic/api/faces.rb
60
+ - lib/twitpic/api/media.rb
61
+ - lib/twitpic/api/places.rb
62
+ - lib/twitpic/api/tags.rb
63
+ - lib/twitpic/api/upload.rb
64
+ - lib/twitpic/api/users.rb
65
+ - lib/twitpic/api.rb
66
+ - lib/twitpic/client.rb
67
+ - lib/twitpic/config.rb
68
+ - lib/twitpic.rb
69
+ homepage: https://github.com/meltingice/TwitPic-API-for-Ruby
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.3
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Provides full access to the TwitPic API, including photo uploads
96
+ test_files: []
97
+