Skip to content

Instantly share code, notes, and snippets.

@ajhaupt7
Created February 4, 2016 19:36
Show Gist options
  • Save ajhaupt7/74e7d9181952b5317e7f to your computer and use it in GitHub Desktop.
Save ajhaupt7/74e7d9181952b5317e7f to your computer and use it in GitHub Desktop.
Adapter Pattern
// Cross browser opacity:
// opacity: 0.9; Chrome 4+, FF2+, Saf3.1+, Opera 9+, IE9, iOS 3.2+, Android 2.1+
// filter: alpha(opacity=90); IE6-IE8
// Setting opacity
$( ".container" ).css( { opacity: .5 } );
// Getting opacity
var currentOpacity = $( ".container" ).css('opacity');
--------------------------------------------------------
// Adapter
get: function( elem, computed ) {
// IE uses filters for opacity
return ropacity.test( (
computed && elem.currentStyle ?
elem.currentStyle.filter : elem.style.filter) || "" ) ?
( parseFloat( RegExp.$1 ) / 100 ) + "" :
computed ? "1" : "";
},
set: function( elem, value ) {
var style = elem.style,
currentStyle = elem.currentStyle,
opacity = jQuery.isNumeric( value ) ?
"alpha(opacity=" + value * 100 + ")" : "",
filter = currentStyle && currentStyle.filter || style.filter || "";
// IE has trouble with opacity if it does not have layout
// Force it by setting the zoom level
style.zoom = 1;
// if setting opacity to 1, and no other filters
//exist - attempt to remove filter attribute #6652
if ( value >= 1 && jQuery.trim( filter.replace( ralpha, "" ) ) === "" ) {
// Setting style.filter to null, "" & " " still leave
// "filter:" in the cssText if "filter:" is present at all,
// clearType is disabled, we want to avoid this style.removeAttribute
// is IE Only, but so apparently is this code path...
style.removeAttribute( "filter" );
// if there there is no filter style applied in a css rule, we are done
if ( currentStyle && !currentStyle.filter ) {
return;
}
}
// otherwise, set new filter values
style.filter = ralpha.test( filter ) ?
filter.replace( ralpha, opacity ) :
filter + " " + opacity;
}
};
require 'rest_client'
require 'rspotify'
def search_spotify(artist)
base_url = "https://api.spotify.com/v1/search?q=#{artist}&type=artist"
unclean = RestClient.get(base_url)
artists = JSON.parse(unclean)
return artists.first
end
def search_spotify(artist)
artists = RSpotify::Artist.search(artist)
return artists.first
end
Adapter Pattern
- Translates one interface (an object's properties and methods) to another.
- Adapters allows programming components to work together that otherwise wouldn't because of mismatched interfaces.
Components
- Client: interacts with the adapter, makes calls to adapter to request a service
- Adapter: adapts (or wraps) the functionality of the adaptee to enable the client to use it
- Adaptee: contains functionality in a form that is unusable by the client
Broad Example
- Code utilizes some object or function, but we want to replace it with a different one.
- Problem is that the new object/function uses a different interface than the one we’re currently using.
- Instead of changing the code in every spot that is using the current object, use an adapter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment