Permalink
Browse files

adding in mini-test. cleaning up some code

  • Loading branch information...
1 parent b3fafeb commit e23f696fdfcd1c9b280c11f3e3bbdc59bd7738f7 @cajun committed Jan 10, 2011
Showing with 86 additions and 83 deletions.
  1. +7 −3 Rakefile
  2. +29 −17 lib/smile/album.rb
  3. +0 −1 lib/smile/base.rb
  4. +9 −12 lib/smile/common.rb
  5. +1 −1 lib/smile/json.rb
  6. +16 −0 lib/smile/param_converter.rb
  7. +14 −10 lib/smile/photo.rb
  8. +10 −8 lib/smile/smug.rb
  9. +0 −28 spec/smile_spec.rb
  10. +0 −3 spec/spec_helper.rb
View
@@ -1,13 +1,17 @@
require 'bundler'
+require 'rake/testtask'
Bundler::GemHelper.install_tasks
+Rake::TestTask.new(:test) do |test|
+ test.libs << 'lib' << 'spec'
+ test.pattern = './spec/**/*_spec.rb'
+ test.verbose = true
+end
+
task :default => :test
desc "Open an irb session preloaded with this library"
task :console do
sh "irb -rubygems -r ./lib/smile.rb"
end
-task :test do
- sh "ruby #{Dir.glob('./spec/*_spec.rb').join(' ')}"
-end
View
@@ -252,10 +252,10 @@ def update( options )
def photos( options=nil )
json = web_method_call(
{
- :method => 'smugmug.images.get',
- :album_id => album_id,
+ :method => 'smugmug.images.get',
+ :album_id => album_id,
:album_key => key,
- :heavy => 1
+ :heavy => 1
},
options
)
@@ -270,7 +270,12 @@ def photos( options=nil )
# @option options [optional, 1 or 0] :heavy more details
def stats( options =nil )
json = web_method_call(
- { :method => 'smugmug.albums.getStats', :album_id => album_id, :month => Date.today.month, :year => Date.today.year },
+ {
+ :method => 'smugmug.albums.getStats',
+ :album_id => album_id,
+ :month => Date.today.month,
+ :year => Date.today.year
+ },
options
)
@@ -290,20 +295,21 @@ def add( image, options={} )
if( File.exists?( image ) )
options = Smile::ParamConverter.clean_hash_keys( options )
json = RestClient.put( UPLOAD + "/#{image}", File.read( image ),
- :content_length => File.size( image ),
- :content_md5 => MD5.hexdigest( File.read( image ) ),
- :x_smug_sessionid => self.session.id,
- :x_smug_version => VERSION,
+ :content_length => File.size( image ),
+ :content_md5 => MD5.hexdigest( File.read( image ) ),
+ :x_smug_sessionid => self.session.id,
+ :x_smug_version => VERSION,
:x_smug_responseType => "Smile::Json",
- :x_smug_albumid => album_id,
- :x_smug_filename => File.basename( image ),
- :x_smug_caption => options[:caption],
- :x_smug_keywords => options[:keywords],
- :x_smug_latitude => options[:latitude],
- :x_smug_longitude => options[:longitude],
- :x_smug_altitude => options[:altitude] ).body
+ :x_smug_albumid => album_id,
+ :x_smug_filename => File.basename( image ),
+ :x_smug_caption => options[:caption],
+ :x_smug_keywords => options[:keywords],
+ :x_smug_latitude => options[:latitude],
+ :x_smug_longitude => options[:longitude],
+ :x_smug_altitude => options[:altitude] ).body
image = Smile::Json.parse( json )
+
if( image && image["image"] && image["image"]["id"] )
Smile::Photo.find( :image_id => image["image"]["id"] )
else
@@ -316,7 +322,10 @@ def add( image, options={} )
# Want to get rid of that album? Call this guy and see what gets removed!
def delete!
- json = web_method_call( { :method => 'smugmug.albums.delete', :album_id => album_id })
+ json = web_method_call( {
+ :method => 'smugmug.albums.delete',
+ :album_id => album_id
+ })
nil
end
@@ -326,7 +335,10 @@ def delete!
# @option options [String] :by valid values: FileName, Caption, DateTime
# @option options [String] :direction valid values: ASC, DESC
def resort!( options =nil )
- json = web_method_call( { :method => 'smugmug.albums.reSort', :album_id => album_id }, options)
+ json = web_method_call( {
+ :method => 'smugmug.albums.reSort',
+ :album_id => album_id
+ }, options)
nil
end
View
@@ -3,7 +3,6 @@
# smile
#
# Created by Zac Kleinpeter on 2009-04-28.
-# Copyright 2009 Cajun Country. All rights reserved.
#
module Smile
class Base
View
@@ -21,22 +21,18 @@ def default_params
# web calls. Given a set of web options and other params
# call the web service and convert it to json
def web_method_call( web_options, options = {} )
- options = Smile::ParamConverter.clean_hash_keys( options )
- web_options = Smile::ParamConverter.clean_hash_keys( web_options )
-
- params = default_params.merge( web_options )
- params.merge!( options ) if( options )
-
- logger.info( params.inspect )
-
- json = RestClient.post( BASE, params ).body
- upper_hash_to_lower_hash(Smile::Json.parse( json ) )
+ base_web_method_call( web_options, options, BASE )
end
# This is the base work that will need to be done on ALL
# web calls. Given a set of web options and other params
# call the web service and convert it to json
def secure_web_method_call( web_options, options = {} )
+ base_web_method_call( web_options, options, BASE_SECURE )
+ end
+
+ # Call either the secure or the base web url
+ def base_web_method_call( web_options, options ={}, url )
options = Smile::ParamConverter.clean_hash_keys( options )
web_options = Smile::ParamConverter.clean_hash_keys( web_options )
@@ -45,9 +41,10 @@ def secure_web_method_call( web_options, options = {} )
logger.info( params.inspect )
- json = RestClient.post( BASE_SECURE, params ).body
+ json = RestClient.post( url, params ).body
upper_hash_to_lower_hash(Smile::Json.parse( json ) )
- end
+ end
+
# This converts a hash that has mixed case
# into all lower case
def upper_hash_to_lower_hash( upper )
View
@@ -4,7 +4,7 @@ class Json
class << self
def parse( text )
Smile::Base.logger.info( text )
- @result = Yajl::Parser.parse(text) # This is the default JSON parse
+ @result = Yajl::Parser.parse(text)
raise_exception! if has_error?
@result
end
@@ -1,6 +1,22 @@
module Smile::ParamConverter
module_function
+ # The smugmug api does not use all lowercase for params. Depending on the service call the
+ # case my be different. This converter will normalize the params that are passed to the smugmug
+ # api.
+ #
+ # Example:
+ # :popular_category # => popularCategory
+ # :geo_all # => geoAll
+ # :data # => Data
+ # :type # => Type
+ # :image_id # => ImageId
+ # :image_key # => ImageKey
+ # :exif # => EXIF
+ # :api_key # => APIKey
+ # :session_id # => SessionID
+ #
+ # By doing this conversion it will be easier for the developer to access the params for the web service.
def convert( param, value=nil )
key = nil
key = case param.to_s.downcase.to_sym
View
@@ -11,9 +11,9 @@ def from_json( json )
logger.info( json )
json["images"].map do |image_upper|
image = upper_hash_to_lower_hash( image_upper )
- image.merge!( :image_id => image["id"] )
+ image.merge!( :image_id => image["id"] )
image.merge!( :album_key => image["album"]["key"] )
- image.merge!( :album_id => image["album"]["id"] )
+ image.merge!( :album_id => image["album"]["id"] )
image.delete( 'album' )
Smile::Photo.new( image )
@@ -36,9 +36,9 @@ def find( options={} )
image = image['image']
logger.info( image )
- image.merge!( :image_id => image["id"] )
+ image.merge!( :image_id => image["id"] )
image.merge!( :album_key => image["album"]["key"] )
- image.merge!( :album_id => image["album"]["id"] )
+ image.merge!( :album_id => image["album"]["id"] )
image.delete( 'album' )
Smile::Photo.new( image )
@@ -88,9 +88,11 @@ def find( options={} )
# @option options [String] :password a password field
# @option options [String] :site_password site password field
def details( options =nil )
- image = web_method_call(
- { :method => "smugmug.images.getEXIF", :ImageID => self.image_id, :ImageKey => self.key },
- options
+ image = web_method_call( {
+ :method => "smugmug.images.getEXIF",
+ :ImageID => self.image_id,
+ :ImageKey => self.key
+ }, options
)
image.merge!( :image_id => image["id"] )
@@ -182,9 +184,11 @@ def info( options =nil )
# String "X3LargeURL" (if available)
# String "OriginalURL" (if available)
def urls( options =nil )
- image = web_method_call(
- { :method => "smugmug.images.getURLs", :ImageID => self.image_id, :ImageKey => self.key },
- options
+ image = web_method_call( {
+ :method => "smugmug.images.getURLs",
+ :ImageID => self.image_id,
+ :ImageKey => self.key
+ }, options
)
image.merge!( :image_id => image["id"] )
View
@@ -3,7 +3,6 @@
# smile
#
# Created by Zac Kleinpeter on 2009-04-28.
-# Copyright 2009 Cajun Country. All rights reserved.
#
module Smile
class Smug < Smile::Base
@@ -14,14 +13,17 @@ class Smug < Smile::Base
# @param [String] password The password for the SmugMug account
#
# @return [Smile::SmugMug.new] An Smug object that has been authenticated
- def auth( email, pass )
- json = secure_web_method_call(
- { :method => 'smugmug.login.withPassword', :EmailAddress => email, :Password => pass }
- )
+ def auth( email, pass )
+ json = secure_web_method_call( {
+ :method => 'smugmug.login.withPassword',
+ :EmailAddress => email,
+ :Password => pass
+ }
+ )
- self.session.id = json["login"]["session"]["id"]
- json
- end
+ self.session.id = json["login"]["session"]["id"]
+ json
+ end
# Login to SmugMug using an anonymously account
# This will allow you to execute many functions, but no user specific functions
View
@@ -8,8 +8,6 @@
Smile::Base.configure do |config|
config.logger_on = false
end
- @smug = Smile.auth_anonymously
- @album = @smug.albums(:nick_name => 'kleinpeter').first
end
end
@@ -40,30 +38,4 @@
end
end
-
- describe 'albums' do
- it 'should have albums on an nickname account' do
- VCR.use_cassette 'getting albums for a nickname', :record => :new_episodes do
- @smug.albums( :nick_name => 'kleinpeter' ).wont_be_empty
- end
- end
-
- describe 'have methods' do
- it 'should be able to be reloaded' do
- VCR.use_cassette 'reloading album', :record => :new_episodes do
- old_title = @album.title
- @album.title = 'foo'
- @album.reload!
- @album.title.must_equal old_title
- end
- end
-
- it 'should have photos' do
- VCR.use_cassette 'checking photos', :record => :new_episodes do
- @album.photos.wont_be_empty
- end
- end
-
- end
- end
end
View
@@ -8,10 +8,7 @@
require 'smile'
require 'vcr'
-
VCR.config do |c|
c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
c.http_stubbing_library = :fakeweb # or :webmock
end
-
-MiniTest::Unit.autorun

0 comments on commit e23f696

Please sign in to comment.