Ruby on Rails Best Practices For Third Party Integration Code Placement -
i've looked @ other postings , blog postings on it's best place third party integration code naturally, there's plethora of opinions , answers hoping more definitive answer scenario.
here's quick breakdown of user story:
when user on profile page, have ability ask refresh of twitter , / or facebook info.
models:
class user < activerecord::base has_one :twitter_account has_one :facebook_account end class twitteraccount < activerecord::base belongs_to :user end class facebookaccount < activerecord::base belongs_to :user end
what i'm looking for: when user wants update twitter account info, want able call method returns me fetched account info. somewhere handles auth, fetching, posting, , parsing of json response. want keep logic outside of model because it's going cluttered , methods behave little outside of norm mvc of app.
the current implementation i've been going making separate classes in /lib
each integration entities. /lib/twitter_api
, /lib/facebook_api
. in here keep connections (gets, posts) , extracting of data want responses.
maybe direction way go me wanted feedback before got far it.
using ruby 2.1.1 & rails 4.1.1
i don't know if can speak "best practice", here how approach third party integration.
i'm biased against using /lib
very much. if code directly relevant running application, want in /app
. if code relevant application , other applications, want in gem. generally, remains in /lib
code trivially easy or hasn't been made gem yet. example, if i'm still determining structure of code, find easier /lib
gem. avoid referencing items in /app
/lib
.
my first concern external api calls outside of main request/response flow. don't want poor response time external service reflect poorly on application. in rails, means wouldn't have api call third party service within controller action. instead, controller register request background job process later , inform them processed.
i place code background jobs in app/jobs
. haven't integrated twitter , facebook, suspect there gems handle majority of connection , data retrieval. job code retrieve data using gems, transform code, , update activerecord models.
i consider data transformation part of domain model. create directory under /app/models
code separated activerecord models. job reference code perform transformation.
i'm sure /lib/twitter_api
, lib/facebook_api
work well. as avoid /lib
, you're restricting items own directories cohesive in regard.
Comments
Post a Comment