Permalink
Browse files

preparing for initial release

  • Loading branch information...
0 parents commit ec6851afd8835630903206309927d82a6ee56c4f @danwrong committed Jul 9, 2009
Showing with 238 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 CHANGELOG
  3. +22 −0 LICENSE
  4. +7 −0 Manifest
  5. +45 −0 README
  6. +10 −0 Rakefile
  7. +76 −0 lib/multipart/post.rb
  8. +43 −0 lib/twaudio.rb
  9. +33 −0 twaudio-ruby.gemspec
@@ -0,0 +1 @@
+pkg
@@ -0,0 +1 @@
+v0.1 Initial release
@@ -0,0 +1,22 @@
+Copyright (c) 2009 Massive Robot Ltd.
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,7 @@
+CHANGELOG
+lib/multipart/post.rb
+lib/twaudio.rb
+LICENSE
+Rakefile
+README
+Manifest
@@ -0,0 +1,45 @@
+h1. twaud.io bindings for Ruby
+
+This gem provides bindings to http://twaud.io which allows you to post audio to twitter.
+
+It's crazy simple and depends on HTTParty:http://github.com/jnunemaker/httparty. I've not
+added it as a gem dependency because there are various forks of HTTParty about that you
+might want to use and I don't want to prescribe a certain one
+
+%
+
+require 'rubygems'
+require 'twaudio'
+
+twaudio = Twaudio.new 'twitter_user', 'twitter_pass'
+
+# get details of a clip
+
+twaudio.sound('4J')
+=> {"message"=>"testing twaudio ruby bindings", "user"=>"danwrong", "listens"=>3,
+"url"=>"http://twaud.io/4J", "audio_url"=>"http://twaud.io/audio/4J",
+"created_at"=>"Thu, 09 Jul 2009 09:58:48 +0000"}
+
+twaudio.user('danwrong')
+=> [{"message"=>"testing twaudio ruby bindings", "user"=>"danwrong",
+"listens"=>3, "url"=>"http://twaud.io/4J", "audio_url"=>"http://twaud.io/audio/4J",
+"created_at"=>"Thu, 09 Jul 2009 09:58:48 +0000"}, {"message"=>"Atmosphere @ Scala",
+"user"=>"danwrong", "listens"=>56, "url"=>"http://twaud.io/0k",
+"audio_url"=>"http://twaud.io/audio/0k",
+"created_at"=>"Wed, 24 Jun 2009 20:21:11 +0000"},
+{"message"=>"It always freaks me out when this comes on when I have iTunes on shuffle...",
+"user"=>"danwrong", "listens"=>69, "url"=>"http://twaud.io/sd",
+"audio_url"=>"http://twaud.io/audio/sd", "created_at"=>"Fri, 19 Jun 2009 10:56:25 +0000"},
+
+# ...and so on
+
+dope_funky_fresh_tune = File.new('mc_flaps.mp3')
+
+twaudio.upload('this shit is the flyest!!!!', dope_funky_fresh_tune)
+=> {"message"=>"this shit is the flyest!!!!", "user"=>"danwrong", "listens"=>0,
+"url"=>"http://twaud.io/34j4", "audio_url"=>"http://twaud.io/audio/34j4",
+"created_at"=>"Thu, 09 Jul 2009 09:58:48 +0000"}
+
+At the moment it's just a stupidly thin wrapper around HTTParty but it does the job.
+
+I'll probably beef it up a little as needed.
@@ -0,0 +1,10 @@
+require 'echoe'
+
+Echoe.new("twaudio-ruby") do |p|
+ p.author = "Dan Webb"
+ p.email = 'dan@danwebb.net'
+ p.summary = "Dirt simple API for twaud.io"
+ p.url = "http://github.com/danwrong/twaudio-ruby/"
+ p.retain_gemspec = true
+ p.runtime_dependencies = ['mime-types']
+end
@@ -0,0 +1,76 @@
+# From here: http://stackoverflow.com/questions/184178/ruby-how-to-post-a-file-via-http-as-multipart-form-data
+require 'mime/types'
+require 'uri'
+
+
+module Multipart
+ VERSION = "1.0.0" unless const_defined?(:VERSION)
+
+ class Post
+ attr_reader :headers, :body
+
+ DEFAULT_OPTIONS = {
+ :boundary => '0123456789RUBYRUBYRUBYRUBY9876543210',
+ :headers => {}
+ }
+
+ def initialize(params, options={})
+ @options = DEFAULT_OPTIONS.merge(options)
+ @params = params
+
+ prepare_query
+ end
+
+ protected
+
+ def prepare_query
+ parts = @params.collect do |k, v|
+ if v.respond_to?(:path) && v.respond_to?(:read)
+ FileParam.new(k, v.path, v.read)
+ else
+ StringParam.new(k, v)
+ end
+ end
+
+ @headers = {
+ 'Content-Type' => "multipart/form-data; boundary=#{@options[:boundary]}"
+ }.merge(@options[:headers])
+
+ @body = parts.collect do |part|
+ "--#{@options[:boundary]}\r\n#{part.to_multipart}"
+ end.join("")
+
+ @body << "--#{@options[:boundary]}--"
+ end
+
+ end
+
+ class StringParam
+ attr_accessor :k, :v
+
+ def initialize(k, v)
+ @k = k
+ @v = v
+ end
+
+ def to_multipart
+ return "Content-Disposition: form-data; name=\"#{URI::encode(k)}\"\r\n\r\n#{v}\r\n"
+ end
+ end
+
+ class FileParam
+ attr_accessor :k, :filename, :content
+
+ def initialize(k, filename, content)
+ @k = k
+ @filename = filename
+ @content = content
+ end
+
+ def to_multipart
+ mime_type = MIME::Types.type_for(filename)[0] || MIME::Types["application/octet-stream"][0]
+ return "Content-Disposition: form-data; name=\"#{URI::encode(k)}\"; filename=\"#{ filename }\"\r\n" +
+ "Content-Type: #{ mime_type.simplified }\r\n\r\n#{ content }\r\n"
+ end
+ end
+end
@@ -0,0 +1,43 @@
+require 'multipart/post'
+require 'httparty'
+
+class Twaudio
+ include HTTParty
+
+ base_uri 'twaud.io'
+
+ def initialize(username='', password='')
+ @username = username
+ @password = password
+ end
+
+ def sound(id)
+ self.class.get("/api/v1/#{id}.json")
+ end
+
+ def user(login)
+ self.class.get("/api/v1/users/#{login}.json")
+ end
+
+ def upload(message, file, tweet_this=false)
+ params = {
+ 'sound[message]' => message,
+ 'sound[file]' => file,
+ 'username' => @username,
+ 'password' => @password
+ }
+
+ params[:tweet_this] = 'true' if tweet_this
+
+ self.class.multipart_post("/api/v1/upload.json", :query => params)
+ end
+
+ def self.multipart_post(path, options={})
+ multipart_post = Multipart::Post.new(options.delete(:query))
+
+ options[:body] = multipart_post.body
+ options[:headers] = (options[:headers] || {}).merge(multipart_post.headers)
+
+ post path, options
+ end
+end
@@ -0,0 +1,33 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{twaudio-ruby}
+ s.version = "0.1"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Dan Webb"]
+ s.date = %q{2009-07-09}
+ s.description = %q{Dirt simple API for twaud.io}
+ s.email = %q{dan@danwebb.net}
+ s.extra_rdoc_files = ["CHANGELOG", "lib/multipart/post.rb", "lib/twaudio.rb", "LICENSE", "README"]
+ s.files = ["CHANGELOG", "lib/multipart/post.rb", "lib/twaudio.rb", "LICENSE", "Rakefile", "README", "Manifest", "twaudio-ruby.gemspec"]
+ s.homepage = %q{http://github.com/danwrong/twaudio-ruby/}
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Twaudio-ruby", "--main", "README"]
+ s.require_paths = ["lib"]
+ s.rubyforge_project = %q{twaudio-ruby}
+ s.rubygems_version = %q{1.3.4}
+ s.summary = %q{Dirt simple API for twaud.io}
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<mime-types>, [">= 0"])
+ else
+ s.add_dependency(%q<mime-types>, [">= 0"])
+ end
+ else
+ s.add_dependency(%q<mime-types>, [">= 0"])
+ end
+end

0 comments on commit ec6851a

Please sign in to comment.