class GunsruAPI
This class allows to access forum.guns.ru via JSON API
every instance method returns pair (HTTPResponse.code, Hash)
Public Class Methods
new(base_uri = "forum.guns.ru", api_uri = "/api/1.0/")
click to toggle source
Creates new JSON API accessor
# File lib/gunsru_api.rb, line 14 def initialize(base_uri = "forum.guns.ru", api_uri = "/api/1.0/") @base_uri = base_uri @api_uri = api_uri #@logger = Logger.new(STDOUT) #@logger.formatter = proc do |sev, date, progname, msg| #"#{msg}\n" #end @etags = {} end
Public Instance Methods
addMessageToTopic()
click to toggle source
# File lib/gunsru_api.rb, line 149 def addMessageToTopic() return -1, {} getResponse end
addTopicToFavourites()
click to toggle source
# File lib/gunsru_api.rb, line 141 def addTopicToFavourites() return -1, {} getResponse end
getActiveTopics(from=0, to=20)
click to toggle source
Arguments: from, to Cacheable: yes Returns: hash
{ timestamp: <unixtime>, topics: [ { type: "topic", title: <string>, id: <integer>, modificationTime: <unixtime>, postCount: <integer>, author: <string>, lastPost: <string> (author) }]}
# File lib/gunsru_api.rb, line 109 def getActiveTopics(from=0, to=20) getResponse('from', from, 'to', to) end
getAllCategories()
click to toggle source
Arguments: none Cacheable: yes Returns: hash
{ timestamp: <unixtime>, categories: [ { type: "category", title: <string>, id: <integer>, sections: [ { type: <string>, title: <string>, id: <integer>, modificationTime: <unixtime>, categoryId: <integer> }]}]}
# File lib/gunsru_api.rb, line 66 def getAllCategories() getResponse() end
getConversationById()
click to toggle source
# File lib/gunsru_api.rb, line 186 def getConversationById() return -1, {} getResponse end
getConversations()
click to toggle source
# File lib/gunsru_api.rb, line 182 def getConversations() return -1, {} getResponse end
getFavoriteTopics()
click to toggle source
# File lib/gunsru_api.rb, line 204 def getFavoriteTopics() return -1, {} getResponse end
getFavoritesSections()
click to toggle source
favorite sections manipulation
# File lib/gunsru_api.rb, line 200 def getFavoritesSections() return -1, {} getResponse end
getFullMessageById()
click to toggle source
# File lib/gunsru_api.rb, line 190 def getFullMessageById() return -1, {} getResponse end
getLastTopics()
click to toggle source
news & last topics
# File lib/gunsru_api.rb, line 168 def getLastTopics() return -1, {} getResponse end
getLinkedSectionsById(*args)
click to toggle source
# File lib/gunsru_api.rb, line 137 def getLinkedSectionsById(*args) return -1, {} getResponse end
getLinkedTopicsById()
click to toggle source
# File lib/gunsru_api.rb, line 145 def getLinkedTopicsById() return -1, {} getResponse end
getNewMessageCount()
click to toggle source
# File lib/gunsru_api.rb, line 194 def getNewMessageCount() return -1, {} getResponse end
getNews()
click to toggle source
# File lib/gunsru_api.rb, line 172 def getNews() return -1, {} getResponse end
getPostById()
click to toggle source
# File lib/gunsru_api.rb, line 161 def getPostById() return -1, {} getResponse end
getPostsByTopicId(id, direction=1, count=1024)
click to toggle source
Arguments: id, direction, count. Optional: number, start_msg_id Cacheable: yes Returns: hash
{ timestamp: <unixtime>, topic_id: <integer>, messages: [ { type: "post", body: <string>, id: <integer>, element_number: <integer>, createTime: <unixtime>, author: { [ nickname: <string>, id: <integer>, userpic: [ { ... }], ]} }]}
# File lib/gunsru_api.rb, line 133 def getPostsByTopicId(id, direction=1, count=1024) getResponse('id', id, 'direction', direction, 'count', count) end
getSectionById()
click to toggle source
# File lib/gunsru_api.rb, line 153 def getSectionById() return -1, {} getResponse end
getTopicById()
click to toggle source
# File lib/gunsru_api.rb, line 157 def getTopicById() return -1, {} getResponse end
getTopicsBySectionId(id, from=0, to=20)
click to toggle source
Arguments: id, from, to Cacheable: yes Returns: hash
{ timestamp: <unixtime>, section_id: <unixtime>, topics: [ { type: "topic", topic_type: <string>, title: <string>, id: <integer>, element_number: <integer>, modificationTime: <unixtime>, postCount: <integer>, author: <string>, lastPost: <string> (author) }]}
# File lib/gunsru_api.rb, line 89 def getTopicsBySectionId(id, from=0, to=20) getResponse('id', id, 'from', from, 'to', to) end
getUserProfileById()
click to toggle source
personal messages
# File lib/gunsru_api.rb, line 178 def getUserProfileById() return -1, {} getResponse end
logout()
click to toggle source
# File lib/gunsru_api.rb, line 221 def logout() return -1, {} getResponse end
registerIosDevice()
click to toggle source
push notifications
# File lib/gunsru_api.rb, line 213 def registerIosDevice() return -1, {} getResponse end
removeTopicFromFavourites()
click to toggle source
# File lib/gunsru_api.rb, line 208 def removeTopicFromFavourites() return -1, {} getResponse end
unRegisterIosDevice()
click to toggle source
# File lib/gunsru_api.rb, line 217 def unRegisterIosDevice() return -1, {} getResponse end
Private Instance Methods
getResponse(*args)
click to toggle source
# File lib/gunsru_api.rb, line 26 def getResponse(*args) callerName = caller[0][/`.*'/][1..-2] params = Hash[args.each_slice(2).to_a] params[:format] = "json" api_call = "#{@api_uri}#{callerName}?#{URI.encode_www_form(params)}" http_req = Net::HTTP::Get.new(api_call) http_req["If-None-Match"] = @etags[callerName] #@logger.debug("sending HTTP::Get to #{api_call}, if-none-macth=#{@etags[callerName]}") http = Net::HTTP.new(@base_uri) http_resp = http.request(http_req) #@logger.debug("got HTTPResponse, code = #{http_resp.code}, etag=#{http_resp["etag"]}") @etags[callerName] = http_resp["etag"] code = http_resp.code.to_i # sometimes gunsru sends bad UTF-8, and it breaks JSON parser # i tried Iconv and String.encode, but was not able to solve this problem result = JSON.parse(http_resp.body.force_encoding('UTF-8').gsub(/[^[:print:]]/u, ''))["result"] if code == 200 return code, result end