code with purpose
play

Code With Purpose @tomprats github.com/tomprats www.tomify.me - PowerPoint PPT Presentation

Code With Purpose @tomprats github.com/tomprats www.tomify.me Tom Prats Developer Extraordinaire @tomprats github.com/tomprats www.tomify.me API @tomprats github.com/tomprats www.tomify.me @tomprats github.com/tomprats www.tomify.me


  1. Code With Purpose @tomprats github.com/tomprats www.tomify.me

  2. Tom Prats Developer Extraordinaire @tomprats github.com/tomprats www.tomify.me

  3. API @tomprats github.com/tomprats www.tomify.me

  4. @tomprats github.com/tomprats www.tomify.me

  5. @tomprats github.com/tomprats www.tomify.me

  6. @tomprats github.com/tomprats www.tomify.me

  7. Code With Purpose @tomprats github.com/tomprats www.tomify.me

  8. Why? How? What? @tomprats github.com/tomprats www.tomify.me

  9. Why? @tomprats github.com/tomprats www.tomify.me

  10. Why do you code? @tomprats github.com/tomprats www.tomify.me

  11. Do you do what you love? @tomprats github.com/tomprats www.tomify.me

  12. Why I code @tomprats github.com/tomprats www.tomify.me

  13. He said to them, "Go into all the world and preach the good news to all creation.” - Mark 16:15 @tomprats github.com/tomprats www.tomify.me

  14. How? @tomprats github.com/tomprats www.tomify.me

  15. Teach @tomprats github.com/tomprats www.tomify.me

  16. Do @tomprats github.com/tomprats www.tomify.me

  17. Open Source • RapidFTR • SocialCoding4Good @tomprats github.com/tomprats www.tomify.me

  18. API Toolbox @tomprats github.com/tomprats www.tomify.me

  19. @tomprats github.com/tomprats www.tomify.me

  20. Creating an API @tomprats github.com/tomprats www.tomify.me

  21. Personify Your Public Profile • Single Sign On • Unified Public Profile • Advertising Profile • Privacy and Control • Demographic Search • Personal Alerts @tomprats github.com/tomprats www.tomify.me

  22. Models Applications Users Authentications • Public Key • Email • UID • Private Key • Name • Token • Approved • Authentications • Name • Data Data • Approved • Origin • Data @tomprats github.com/tomprats www.tomify.me

  23. Models Users • Email • Name • Birthday • Favorite Color @tomprats github.com/tomprats www.tomify.me

  24. Models create_table :users do |t| t.string :email t.string :name t.datetime :birthday t.string :favorite_color t.timestamps end @tomprats github.com/tomprats www.tomify.me

  25. Authentication aBcxYz123idk = Base64 Encode “public:secret” Authorization: Basic aBcxYz123idk @tomprats github.com/tomprats www.tomify.me

  26. Authentication http_basic_authenticate_with name: "tomify", password: "secret" HTTP Basic: Access denied. @tomprats github.com/tomprats www.tomify.me

  27. Endpoints /users /users/:id Params Examples Params Examples Pagination ?page=3 Extra Fields ?data=age Limit ?limit=4 ?data=favorite_color ?page=3&limit=4 Search ?name=tom ?email=tomprats @tomprats github.com/tomprats www.tomify.me

  28. Endpoints def index @users = search_users @users = @users.page(params[:page]) if params[:page] @users = @users.per(params[:limit]) if params[:limit] render json: @users, extra_attributes: params[:data] end def search_users users = User.all user_attributes.each do |attr| users = users.where("%s ILIKE %s", attr, "$$%#{params[attr]}%$$") if params[attr] end users end def user_attributes [:email, :name, :birthday, :favorite_color] end @tomprats github.com/tomprats www.tomify.me

  29. Endpoints def show @user = User.find(params[:id]) render json: @user, extra_attributes: params[:data] end @tomprats github.com/tomprats www.tomify.me

  30. Endpoints curl -u tomify:secret localhost:3000/users?data=age [{ 
 “id”: 1, “email”: “tom@tomprats.com”, “name”: “Tom Prats”, “birthday”: “1990-11-01T00:00:00.000Z”, “age”: 25 }] @tomprats github.com/tomprats www.tomify.me

  31. Consuming an API @tomprats github.com/tomprats www.tomify.me

  32. Personify Gem require "http" require "hashie" class Personify def initialize(public_key, private_key, base_url = "") @base_url = base_url @client = HTTP .basic_auth(user: public_key, pass: private_key) .headers(accept: :json, content_type: :json) end def get(path, params = nil) objectify @client.get("#{@base_url}#{path}", params: params).parse end private def objectify(hash) Hashie::Mash.new hash end end @tomprats github.com/tomprats www.tomify.me

  33. Basics require "http" personify = Personify.new(“http://localhost:3000") personify.get("/users") class Personify def initialize(base_url = "") @base_url = base_url @client = HTTP .headers(accept: :json, content_type: :json) end def get(path) @client.get("#{@base_url}#{path}") end end @tomprats github.com/tomprats www.tomify.me

  34. Authentication require "http" personify = Personify.new( “tomify”, “secret”, class Personify def initialize(public_key, private_key, base_url = "") “http://localhost:3000” @base_url = base_url ) @client = HTTP personify.get("/users") .basic_auth(user: public_key, pass: private_key) .headers(accept: :json, content_type: :json) end def get(path) @client.get("#{@base_url}#{path}") end end @tomprats github.com/tomprats www.tomify.me

  35. Parameters require "http" personify = Personify.new( “tomify”, “secret”, class Personify def initialize(public_key, private_key, base_url = "") “http://localhost:3000” @base_url = base_url ) @client = HTTP personify.get(“/users”, data: “age”) .basic_auth(user: public_key, pass: private_key) .headers(accept: :json, content_type: :json) end def get(path, params = nil) @client.get("#{@base_url}#{path}", params: params) end end @tomprats github.com/tomprats www.tomify.me

  36. Parsing require "http" personify = Personify.new( require "hashie" “tomify”, “secret”, class Personify “http://localhost:3000” def initialize(public_key, private_key, base_url = "") ) @base_url = base_url data = personify.get(“/users”, data: “age”) @client = HTTP data.users.first.age .basic_auth(user: public_key, pass: private_key) .headers(accept: :json, content_type: :json) => 25 end def get(path, params = nil) objectify @client.get("#{@base_url}#{path}", params: params).parse end private def objectify(hash) Hashie::Mash.new hash end end @tomprats github.com/tomprats www.tomify.me

  37. What? @tomprats github.com/tomprats www.tomify.me

  38. SupportNate.com @tomprats github.com/tomprats www.tomify.me

  39. Tom’s Missions missions.tomprats.com @tomprats github.com/tomprats www.tomify.me

  40. Problem • No standard • Not intuitive • Not customizable • High cost @tomprats github.com/tomprats www.tomify.me

  41. Let’s Check It Out! @tomprats github.com/tomprats www.tomify.me

  42. Models Users Images Trips Missions Favorites • Album • User • Album • User • User • Image • Trip • Country • Trip • Image • Email • Imgur ID • Start Date • Album • Name • Link • End Date • Username • Description • Admin • Bio Albums • Token • Imgur ID @tomprats github.com/tomprats www.tomify.me

  43. Models Many One Favorites Users Trips Images Albums Missions Imgur Image Imgur Album @tomprats github.com/tomprats www.tomify.me

  44. Imgur Base Album Image • api_get • all • create • api_post • get • delete • api_delete • create • refresh_token • add_images • token_expired? • delete • Helper Methods • Helper Methods ImgurError @tomprats github.com/tomprats www.tomify.me

  45. Imgur module Imgur class Base def self.api_get(url) tries ||= 2 refresh_token if token_expired? headers = { "Authorization" => "Bearer #{ENV["IMGUR_TOKEN"]}" } response = Typhoeus.get(url, headers: headers) raise ImgurError if response.response_code >= 400 response rescue ImgurError error = response.body["data"]["error"] refresh_token && (tries -= 1).zero? ? (raise ImgurError.new(error)) : retry end ... end ... end @tomprats github.com/tomprats www.tomify.me

  46. Imgur class Album < Base def self.all url = "https://api.imgur.com/3/account/#{ENV["IMGUR_USERNAME"]}/albums" JSON.parse(api_get(url).body)["data"] end ... end @tomprats github.com/tomprats www.tomify.me

  47. Imgur class Image < Base def self.create(options) url = "https://api.imgur.com/3/image" params = { image: options[:image].try(:tempfile), album: options[:album_id], type: options[:type] || "file", # "file" || "base64" || "URL" title: options[:title], description: options[:description] } JSON.parse(api_post(url, params).body)["data"] end ... end @tomprats github.com/tomprats www.tomify.me

  48. Imgur class Image < ActiveRecord::Base ... def self.with_imgur(options) trip_id = options.delete(:trip_id) user_id = options.delete(:user_id) image = Imgur::Image.create(options) create( imgur_id: image["id"], link: image["link"], trip_id: trip_id, user_id: user_id ) end ... end @tomprats github.com/tomprats www.tomify.me

Recommend


More recommend