Created
April 13, 2018 10:20
-
-
Save stevo/002a2803bd43d9f544b25eb907dd9d20 to your computer and use it in GitHub Desktop.
Client and Wrapper examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OMDBClient | |
HOST_URL = 'www.omdbapi.com' | |
# http://www.omdbapi.com/ | |
def get_movie(args = {}) | |
uri = URI::HTTP.build( | |
host: HOST_URL, | |
query: { | |
apikey: ENV.fetch('OMDB_API_KEY') | |
}.merge(args).to_query | |
) | |
HTTParty.get(uri) | |
end | |
end | |
# OMDBClient.new.get_movie(t: 'The Rock') => | |
# {"Title"=>"The Rock", "Year"=>"1996", "Rated"=>"R", "Released"=>"07 Jun 1996", "Runtime"=>"136 min", "Genre"=>"Action, Adventure, Thriller", "Director"=>"Michael Bay", "Writer"=>"David Weisberg (story), Douglas Cook (story), David Weisberg (screenplay), Douglas Cook (screenplay), Mark Rosner (screenplay)", "Actors"=>"Sean Connery, Nicolas Cage, Ed Harris, John Spencer", "Plot"=>"A mild-mannered chemist and an ex-con must lead the counterstrike when a rogue group of military men, led by a renegade general, threaten a nerve gas attack from Alcatraz against San Francisco.", "Language"=>"English", "Country"=>"USA", "Awards"=>"Nominated for 1 Oscar. Another 9 wins & 8 nominations.", "Poster"=>"https://images-na.ssl-images-amazon.com/images/M/MV5BZDJjOTE0N2EtMmRlZS00NzU0LWE0ZWQtM2Q3MWMxNjcwZjBhXkEyXkFqcGdeQXVyNDk3NzU2MTQ@._V1_SX300.jpg", "Ratings"=>[{"Source"=>"Internet Movie Database", "Value"=>"7.4/10"}, {"Source"=>"Rotten Tomatoes", "Value"=>"66%"}, {"Source"=>"Metacritic", "Value"=>"58/100"}], "Metascore"=>"58", "imdbRating"=>"7.4", "imdbVotes"=>"279,143", "imdbID"=>"tt0117500", "Type"=>"movie", "DVD"=>"02 Dec 1997", "BoxOffice"=>"N/A", "Production"=>"Disney", "Website"=>"N/A", "Response"=>"True"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OMDBWrapper | |
include Singleton | |
class << self | |
delegate :get_movie_by_title, to: :instance | |
end | |
def initialize | |
@omdb_client = OMDBClient.new | |
end | |
def get_movie_by_title(title) | |
movie_attributes = omdb_client. | |
get_movie(t: title). | |
deep_transform_keys(&:underscore) | |
Movie.new(movie_attributes) | |
end | |
private | |
attr_reader :omdb_client | |
end | |
class OMDBWrapper::Movie | |
include Virtus.model | |
attribute :title, String | |
attribute :year, Integer | |
attribute :runtime, Integer | |
end | |
# OMDBWrapper.get_movie_by_title('The Rock') # => | |
# <OMDBWrapper::Movie:0x00007fc6f5b7b290 @runtime="136 min", @title="The Rock", @year=1996> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment