ruby_imgur 0.1.0
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.
- data/lib/imgur.rb +86 -0
 - data/lib/imgur.rb~ +11 -0
 - data/lib/imgur_rails.rb +44 -0
 - data/lib/test +0 -0
 - metadata +48 -0
 
    
        data/lib/imgur.rb
    ADDED
    
    | 
         @@ -0,0 +1,86 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'open-uri'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'net/http'
         
     | 
| 
      
 3 
     | 
    
         
            +
            require 'json'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'base64'
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            class Imgur
         
     | 
| 
      
 7 
     | 
    
         
            +
              
         
     | 
| 
      
 8 
     | 
    
         
            +
              # add an API key to use for uploads
         
     | 
| 
      
 9 
     | 
    
         
            +
              def self.api_key=( api_key )
         
     | 
| 
      
 10 
     | 
    
         
            +
                @@api_key = api_key.to_s.chomp
         
     | 
| 
      
 11 
     | 
    
         
            +
              end
         
     | 
| 
      
 12 
     | 
    
         
            +
              
         
     | 
| 
      
 13 
     | 
    
         
            +
              def self.upload( options = {} )
         
     | 
| 
      
 14 
     | 
    
         
            +
                data = options[:data] ||
         
     | 
| 
      
 15 
     | 
    
         
            +
                  options[:url] ||
         
     | 
| 
      
 16 
     | 
    
         
            +
                  Base64.encode64(open(options[:filename]).read)
         
     | 
| 
      
 17 
     | 
    
         
            +
                post_data = { :key => @@api_key, :image => data }
         
     | 
| 
      
 18 
     | 
    
         
            +
                post_url = generate_url :method => :upload
         
     | 
| 
      
 19 
     | 
    
         
            +
                post_uri = URI.parse post_url
         
     | 
| 
      
 20 
     | 
    
         
            +
                response = JSON (Net::HTTP.post_form post_uri, post_data).body
         
     | 
| 
      
 21 
     | 
    
         
            +
                { :hash => response["upload"]["image"]["hash"],
         
     | 
| 
      
 22 
     | 
    
         
            +
                  :delete_hash => response["upload"]["image"]["deletehash"],
         
     | 
| 
      
 23 
     | 
    
         
            +
                  :filetype => response["upload"]["image"]["type"].gsub("image\/","") }
         
     | 
| 
      
 24 
     | 
    
         
            +
              end
         
     | 
| 
      
 25 
     | 
    
         
            +
              
         
     | 
| 
      
 26 
     | 
    
         
            +
              def self.upload_file( filename )
         
     | 
| 
      
 27 
     | 
    
         
            +
                upload :filename => filename
         
     | 
| 
      
 28 
     | 
    
         
            +
              end
         
     | 
| 
      
 29 
     | 
    
         
            +
              
         
     | 
| 
      
 30 
     | 
    
         
            +
              def self.get( hash, options = {} )
         
     | 
| 
      
 31 
     | 
    
         
            +
                open url_for hash, options
         
     | 
| 
      
 32 
     | 
    
         
            +
              end
         
     | 
| 
      
 33 
     | 
    
         
            +
              
         
     | 
| 
      
 34 
     | 
    
         
            +
              def self.stats
         
     | 
| 
      
 35 
     | 
    
         
            +
                json_get :method => :stats
         
     | 
| 
      
 36 
     | 
    
         
            +
              end
         
     | 
| 
      
 37 
     | 
    
         
            +
              
         
     | 
| 
      
 38 
     | 
    
         
            +
              def self.delete( delete_hash )
         
     | 
| 
      
 39 
     | 
    
         
            +
                json_get( :hash => delete_hash, :method => :delete )["delete"]["message"] == "Success"
         
     | 
| 
      
 40 
     | 
    
         
            +
              end
         
     | 
| 
      
 41 
     | 
    
         
            +
              
         
     | 
| 
      
 42 
     | 
    
         
            +
              def self.album( hash, options = {} )
         
     | 
| 
      
 43 
     | 
    
         
            +
                json_get :hash => hash, :method => :album
         
     | 
| 
      
 44 
     | 
    
         
            +
              end
         
     | 
| 
      
 45 
     | 
    
         
            +
              
         
     | 
| 
      
 46 
     | 
    
         
            +
              def self.url_for( hash, options = {} )
         
     | 
| 
      
 47 
     | 
    
         
            +
                sizes = {
         
     | 
| 
      
 48 
     | 
    
         
            +
                  :small => :s,
         
     | 
| 
      
 49 
     | 
    
         
            +
                  :large => :l }
         
     | 
| 
      
 50 
     | 
    
         
            +
                if options[:filetype] == :detect
         
     | 
| 
      
 51 
     | 
    
         
            +
                  # fix this it's ugly
         
     | 
| 
      
 52 
     | 
    
         
            +
                  options[:filetype] = json_get( :hash => hash, :method => :image )["image"]["image"]["type"].gsub("image\/","")
         
     | 
| 
      
 53 
     | 
    
         
            +
                end
         
     | 
| 
      
 54 
     | 
    
         
            +
                "http://i.imgur.com/#{hash}#{sizes[options[:size]]}.#{options[:filetype] || :png}"
         
     | 
| 
      
 55 
     | 
    
         
            +
              end
         
     | 
| 
      
 56 
     | 
    
         
            +
              
         
     | 
| 
      
 57 
     | 
    
         
            +
              def self.debug=( debug_setting )
         
     | 
| 
      
 58 
     | 
    
         
            +
                @debug = debug_setting
         
     | 
| 
      
 59 
     | 
    
         
            +
              end
         
     | 
| 
      
 60 
     | 
    
         
            +
              
         
     | 
| 
      
 61 
     | 
    
         
            +
              class << self
         
     | 
| 
      
 62 
     | 
    
         
            +
                alias_method :[], :url_for
         
     | 
| 
      
 63 
     | 
    
         
            +
              end
         
     | 
| 
      
 64 
     | 
    
         
            +
              
         
     | 
| 
      
 65 
     | 
    
         
            +
              private
         
     | 
| 
      
 66 
     | 
    
         
            +
              
         
     | 
| 
      
 67 
     | 
    
         
            +
              def debug( thing_to_debug, message = "" )
         
     | 
| 
      
 68 
     | 
    
         
            +
                puts message + thing_to_debug.to_s
         
     | 
| 
      
 69 
     | 
    
         
            +
                thing_to_debug
         
     | 
| 
      
 70 
     | 
    
         
            +
              end
         
     | 
| 
      
 71 
     | 
    
         
            +
              
         
     | 
| 
      
 72 
     | 
    
         
            +
              def self.json_get( options = {} )
         
     | 
| 
      
 73 
     | 
    
         
            +
                JSON open(generate_url options).read
         
     | 
| 
      
 74 
     | 
    
         
            +
              end
         
     | 
| 
      
 75 
     | 
    
         
            +
              
         
     | 
| 
      
 76 
     | 
    
         
            +
              def self.generate_url( options = {} )
         
     | 
| 
      
 77 
     | 
    
         
            +
                hash = options[:hash]
         
     | 
| 
      
 78 
     | 
    
         
            +
                method = options[:method]
         
     | 
| 
      
 79 
     | 
    
         
            +
                url = "http://api.imgur.com/2/"
         
     | 
| 
      
 80 
     | 
    
         
            +
                url += method.to_s
         
     | 
| 
      
 81 
     | 
    
         
            +
                url += "/" + hash.to_s if hash
         
     | 
| 
      
 82 
     | 
    
         
            +
                url += ".json"
         
     | 
| 
      
 83 
     | 
    
         
            +
                url
         
     | 
| 
      
 84 
     | 
    
         
            +
              end
         
     | 
| 
      
 85 
     | 
    
         
            +
              
         
     | 
| 
      
 86 
     | 
    
         
            +
            end
         
     | 
    
        data/lib/imgur.rb~
    ADDED
    
    
    
        data/lib/imgur_rails.rb
    ADDED
    
    | 
         @@ -0,0 +1,44 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'imgur'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module ImgurRails
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
              def acts_as_imgur_image
         
     | 
| 
      
 6 
     | 
    
         
            +
                attr_accessor :image_data
         
     | 
| 
      
 7 
     | 
    
         
            +
                
         
     | 
| 
      
 8 
     | 
    
         
            +
                before_create do
         
     | 
| 
      
 9 
     | 
    
         
            +
                  response = Imgur.upload :filename => self.image_data.tempfile.path
         
     | 
| 
      
 10 
     | 
    
         
            +
                  self.image_hash = response[:hash]
         
     | 
| 
      
 11 
     | 
    
         
            +
                  self.delete_hash = response[:delete_hash] if self.respond_to? :delete_hash
         
     | 
| 
      
 12 
     | 
    
         
            +
                  self.filetype = response[:filetype] if self.respond_to? :filetype
         
     | 
| 
      
 13 
     | 
    
         
            +
                end
         
     | 
| 
      
 14 
     | 
    
         
            +
                
         
     | 
| 
      
 15 
     | 
    
         
            +
                before_destroy do
         
     | 
| 
      
 16 
     | 
    
         
            +
                  Imgur.delete self.delete_hash if self.respond_to? :delete_hash
         
     | 
| 
      
 17 
     | 
    
         
            +
                end
         
     | 
| 
      
 18 
     | 
    
         
            +
                
         
     | 
| 
      
 19 
     | 
    
         
            +
                include InstanceMethods
         
     | 
| 
      
 20 
     | 
    
         
            +
                
         
     | 
| 
      
 21 
     | 
    
         
            +
              end
         
     | 
| 
      
 22 
     | 
    
         
            +
              
         
     | 
| 
      
 23 
     | 
    
         
            +
              module InstanceMethods
         
     | 
| 
      
 24 
     | 
    
         
            +
              
         
     | 
| 
      
 25 
     | 
    
         
            +
                def url
         
     | 
| 
      
 26 
     | 
    
         
            +
                  filetype = ( self.respond_to? :filetype ? self.filetype : :any )
         
     | 
| 
      
 27 
     | 
    
         
            +
                  Imgur.url_for self.image_hash, :filetype => filetype
         
     | 
| 
      
 28 
     | 
    
         
            +
                end
         
     | 
| 
      
 29 
     | 
    
         
            +
                
         
     | 
| 
      
 30 
     | 
    
         
            +
                def thumbnail( size = :small )
         
     | 
| 
      
 31 
     | 
    
         
            +
                  filetype = ( self.respond_to? :filetype ? self.filetype : :any )
         
     | 
| 
      
 32 
     | 
    
         
            +
                  Imgur.url_for self.image_hash, :filetype => filetype, :size => size
         
     | 
| 
      
 33 
     | 
    
         
            +
                end
         
     | 
| 
      
 34 
     | 
    
         
            +
              
         
     | 
| 
      
 35 
     | 
    
         
            +
                def is_image?
         
     | 
| 
      
 36 
     | 
    
         
            +
                  true
         
     | 
| 
      
 37 
     | 
    
         
            +
                end
         
     | 
| 
      
 38 
     | 
    
         
            +
                alias_method :is_imgur_image?, :is_image?
         
     | 
| 
      
 39 
     | 
    
         
            +
                
         
     | 
| 
      
 40 
     | 
    
         
            +
              end
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
            end
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
            ActiveRecord::Base.extend ImgurRails
         
     | 
    
        data/lib/test
    ADDED
    
    | 
         Binary file 
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,48 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: ruby_imgur
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version
         
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.1.0
         
     | 
| 
      
 5 
     | 
    
         
            +
              prerelease: 
         
     | 
| 
      
 6 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 7 
     | 
    
         
            +
            authors:
         
     | 
| 
      
 8 
     | 
    
         
            +
            - Fraser Murray
         
     | 
| 
      
 9 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 10 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 11 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2012-05-22 00:00:00.000000000 Z
         
     | 
| 
      
 13 
     | 
    
         
            +
            dependencies: []
         
     | 
| 
      
 14 
     | 
    
         
            +
            description: 
         
     | 
| 
      
 15 
     | 
    
         
            +
            email: fraser.m.murray@gmail.com
         
     | 
| 
      
 16 
     | 
    
         
            +
            executables: []
         
     | 
| 
      
 17 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 18 
     | 
    
         
            +
            extra_rdoc_files: []
         
     | 
| 
      
 19 
     | 
    
         
            +
            files:
         
     | 
| 
      
 20 
     | 
    
         
            +
            - lib/imgur_rails.rb
         
     | 
| 
      
 21 
     | 
    
         
            +
            - lib/test
         
     | 
| 
      
 22 
     | 
    
         
            +
            - lib/imgur.rb~
         
     | 
| 
      
 23 
     | 
    
         
            +
            - lib/imgur.rb
         
     | 
| 
      
 24 
     | 
    
         
            +
            homepage: ''
         
     | 
| 
      
 25 
     | 
    
         
            +
            licenses: []
         
     | 
| 
      
 26 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 27 
     | 
    
         
            +
            rdoc_options: []
         
     | 
| 
      
 28 
     | 
    
         
            +
            require_paths:
         
     | 
| 
      
 29 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 30 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 31 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 32 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 33 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 34 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 35 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 36 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 37 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 38 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 39 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 40 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 41 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 42 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 43 
     | 
    
         
            +
            rubyforge_project: 
         
     | 
| 
      
 44 
     | 
    
         
            +
            rubygems_version: 1.8.22
         
     | 
| 
      
 45 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 46 
     | 
    
         
            +
            specification_version: 3
         
     | 
| 
      
 47 
     | 
    
         
            +
            summary: library for imgur api
         
     | 
| 
      
 48 
     | 
    
         
            +
            test_files: []
         
     |