Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Building API clients with Cistern
Search
Adam Holt
February 20, 2014
Programming
0
77
Building API clients with Cistern
@nwrug - github.com/lanej/cistern
Adam Holt
February 20, 2014
Tweet
Share
More Decks by Adam Holt
See All by Adam Holt
NWRUG - Celluloid
omgitsads
3
230
Other Decks in Programming
See All in Programming
[RubyKaigi 2024] Ruby Mixology 101: adding shots of PHP, Elixir, and more
palkan
1
400
slog登場に伴うloggerの取り回し手法の見直し / kamakura.go #6
arthur1
1
440
Findy - エンジニア向け会社紹介 / Findy Letter for Engineers
findyinc
2
76k
#kaigieffect LT 2024 - rexml-css_selector: A REXML extension for supporting CSS selector
makenowjust
1
180
JRuby 10: Ruby 3.3 on the Modern JVM
headius
0
140
The Final Frontier of Web Development: React Server Components vs Jakarta EE
ivargrimstad
0
360
Porting mruby/c for the SNES (Super Famicom) - RubyKaigi 2024
gedorinku
0
2.1k
計算量オーダーの話
tsudaahr
1
130
実践AI/ML ~よくあるユースケースとレコメンデーションの効果的な活用戦略~
cmnakamurashogo
0
190
見えないものに着目すると上手くいく、モデリングの勘所 / invisible-driven-design
minodriven
18
3.7k
Does Ruby Parser dream of highly expressive grammar?
ydah
3
510
新年度から始めるJava言語のファーストステップ2024
tatsuya1bm
0
150
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
321
23k
RailsConf 2023
tenderlove
9
610
Understanding Cognitive Biases in Performance Measurement
bluesmoon
12
1.1k
Fantastic passwords and where to find them - at NoRuKo
philnash
39
2.6k
BBQ
matthewcrist
80
8.8k
How to Ace a Technical Interview
jacobian
273
22k
Statistics for Hackers
jakevdp
790
220k
The Cost Of JavaScript in 2023
addyosmani
22
4k
GitHub's CSS Performance
jonrohan
1025
450k
Fashionably flexible responsive web design (full day workshop)
malarkey
399
65k
The Mythical Team-Month
searls
217
42k
The Straight Up "How To Draw Better" Workshop
denniskardys
228
130k
Transcript
BUILDING API CLIENTS WITH CISTERN
@adamholt
@lanejoshlane
WHAT IS CISTERN?
“Cistern helps you consistently build your API clients and facilitates
building mock support.”
None
SERVICE
class Twitter::Client < Cistern::Service ! model_path "twitter/models" request_path "twitter/requests" !
model :tweet collection :tweets request :create_tweet ! class Real def initialize(options={}) # Setup end end ! class Mock def self.data; @data ||= {}; end ! def initialize(options={}) # Setup Mock Data end end end
class Twitter::Client < Cistern::Service model_path "twitter/models" request_path "twitter/requests" ! ...
end
class Twitter::Client < Cistern::Service ... ! model :tweet collection :tweets
request :create_tweet ! ... end
class Twitter::Client < Cistern::Service ... ! class Real def initialize(options={})
# Setup end end ! class Mock def self.data; @data ||= {}; end ! def initialize(options={}) # Setup Mock Data end end end
MODELS
class Twitter::Client::Tweet < Cistern::Model identity :id ! attribute :user, type:
:hash attribute :tweet, type: :array ! def retweet requires :id ! new_attributes = connection.retweet_tweet(id: id).body merge_attributes(new_attributes) end end !
COLLECTIONS
class Twitter::Client::Tweets < Cistern::Collection ! model Twitter::Client::Tweet ! def timeline(params
= {}) response = connection.get_tweets(params) ! data = self.clone.load(response.body) ! collection.attributes.clear collection.merge_attributes(data) end ! def retweets(params={}) response = connection.get_retweets(params) ... end ! def get(id) new(connection.get_tweet("id" => id).body) end end
REQUESTS
module Twitter class Client class Real def create_tweet(options={}) request( :body
=> {status: options[:body]}, :method => :post, :path => '/statuses/update' ) end end # Real ! ... end # Client end # Twitter
module Twitter class Client ... ! class Mock def create_tweet(options={})
id = Foo.random_hex(6) ! tweet = { "id" => id, "text" => "Hello from @NWRUG", "user" => {"name": "omgitsads"}, }.merge(options) ! self.data[:tweets][id]= tweet ! response( :body => tweet, :status => 200, :path => '/statuses/user_timeline.json', ) end end # Mock end # Client end # Twitter
DRAWBACKS
QUESTIONS?