Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.82 KB | None | 0 0
  1. require "../src/magicjson"
  2.  
  3. module SnowflakeConverter
  4.   # JSON payloads have 64 unsigned ints as strings
  5.   # to avoid losing precision
  6.  
  7.   def self.from_json(parser : JSON::PullParser)
  8.     parser.read_string.to_u64
  9.   end
  10.  
  11.   def self.to_json(value : UInt64, builder : JSON::Builder)
  12.     builder.scalar value.to_s
  13.   end
  14. end
  15.  
  16. module AutoIdentifiableConverter(T)
  17.   def self.from_chatprog_json(parser : JSON::PullParser, client : Client)
  18.     id = parser.read_string.to_u64
  19.     client.resolve_user id
  20.   end
  21.  
  22.   def self.to_json(value : User, builder : JSON::Builder)
  23.     value.id.to_s
  24.   end
  25. end
  26.  
  27. module APIObject
  28.   include MagicJSON
  29.  
  30.   json_defaults getter: true
  31.   json_api_config input_method_name: "from_chatprog_json"
  32.  
  33.   field client : Client, extra_field: true, getter: false
  34.  
  35.   module Identifiable
  36.     include APIObject
  37.  
  38.     field id : UInt64, converter: SnowflakeConverter
  39.   end
  40. end
  41.  
  42. struct User
  43.   include APIObject::Identifiable
  44.  
  45.   field username : String
  46.   field bot? : Bool = false, key: "bot"
  47. end
  48.  
  49. struct Message
  50.   include APIObject::Identifiable
  51.  
  52.   field content : String
  53.   field author : User, key: "author_id", converter: {
  54.     type:              AutoIdentifiableConverter(User),
  55.     input_method_name: "from_chatprog_json",
  56.     pass_extra_fields: [:client],
  57.   }
  58. end
  59.  
  60. class Client
  61.   @last_message : Message? = nil
  62.  
  63.   def initialize(@client_id : UInt64)
  64.     @users = {} of UInt64 => User
  65.     connect_to_chat_program
  66.   end
  67.  
  68.   def connect_to_chat_program
  69.     @users[@client_id] = User.from_chatprog_json %{{"id":"#{@client_id}","username":"Zatherz"}}, self
  70.     @last_message = Message.from_chatprog_json %{{"id":"1923515","author_id":"#{@client_id}","content":"Hello, world!"}}, self
  71.   end
  72.  
  73.   def resolve_user(id : UInt64)
  74.     @users[id] || raise "User with ID #{id} doesn't exist"
  75.   end
  76.  
  77.   def last_message
  78.     @last_message.as(Message)
  79.   end
  80. end
  81.  
  82. client = Client.new 123_u64
  83. msg = client.last_message
  84. pp client.last_message.content         # => "Hello, world!"
  85. pp client.last_message.author.id       # => 123
  86. pp client.last_message.author.username # => "Zatherz"
  87.  
  88. # Through the use of the MagicJSON extra_field feature, converters
  89. # and the pass_extra_fields option we were able to go directly from
  90. # a string ID on the message object to a full user object
  91. # using the client to resolve the ID into the user.
  92. #
  93. # Note that this is usually not what you want - because the User
  94. # object could change, you'd want to have a separate 'author' method
  95. # that takes '@author_id' and resolves it using the Client.
  96. #
  97. # Message and User both have full access to the Client object
  98. # under @client, and can use it for anything (a message might
  99. # for example have a channel ID, and you could have a 'channel'
  100. # method that returns something along the lines of
  101. # '@client.resolve_channel @channel_id'.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement