gunsru_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/gunsru_api.rb +228 -0
  3. metadata +58 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9bdccbe683acc94ea15f3cd42ec067e85d585ca1
4
+ data.tar.gz: 9e478acd329a6aebdf70a56d9714ff2cfe9c690b
5
+ SHA512:
6
+ metadata.gz: 11133cf817ba3f9bf4bd6a3d696fcff947ba612346d45a532191046fac56a686bbff3f1ceff13c5a3261eeb76a84cf26f85e142f15871134ea156d7ed9b74ae4
7
+ data.tar.gz: cea5f8913885cf7c2128d13b5de3284adaa00f7b815e3bcdee08ac216bd8681e907c451250019732ecefa29d537bdca6dde9f8f1182c93a31f520f3effe552a6
data/lib/gunsru_api.rb ADDED
@@ -0,0 +1,228 @@
1
+ require 'net/http'
2
+ #require 'logger'
3
+ require 'json'
4
+
5
+ ##
6
+ # This class allows to access forum.guns.ru via JSON API
7
+ #
8
+ # every instance method returns pair (HTTPResponse.code, Hash)
9
+ #
10
+ class GunsruAPI
11
+ ##
12
+ # Creates new JSON API accessor
13
+ #
14
+ def initialize(base_uri = "forum.guns.ru", api_uri = "/api/1.0/")
15
+ @base_uri = base_uri
16
+ @api_uri = api_uri
17
+ #@logger = Logger.new(STDOUT)
18
+ #@logger.formatter = proc do |sev, date, progname, msg|
19
+ #"#{msg}\n"
20
+ #end
21
+ @etags = {}
22
+ end
23
+
24
+ private
25
+
26
+ def getResponse(*args)
27
+ callerName = caller[0][/`.*'/][1..-2]
28
+ params = Hash[args.each_slice(2).to_a]
29
+ params[:format] = "json"
30
+ api_call = "#{@api_uri}#{callerName}?#{URI.encode_www_form(params)}"
31
+ http_req = Net::HTTP::Get.new(api_call)
32
+ http_req["If-None-Match"] = @etags[callerName]
33
+
34
+ #@logger.debug("sending HTTP::Get to #{api_call}, if-none-macth=#{@etags[callerName]}")
35
+ http = Net::HTTP.new(@base_uri)
36
+ http_resp = http.request(http_req)
37
+ #@logger.debug("got HTTPResponse, code = #{http_resp.code}, etag=#{http_resp["etag"]}")
38
+ @etags[callerName] = http_resp["etag"]
39
+ code = http_resp.code.to_i
40
+ # sometimes gunsru sends bad UTF-8, and it breaks JSON parser
41
+ # i tried Iconv and String.encode, but was not able to solve this problem
42
+ result = JSON.parse(http_resp.body.force_encoding('UTF-8').gsub(/[^[:print:]]/u, ''))["result"] if code == 200
43
+ return code, result
44
+ end
45
+
46
+ public
47
+
48
+ ##
49
+ # Arguments: none
50
+ # Cacheable: yes
51
+ # Returns: hash
52
+ # {
53
+ # timestamp: <unixtime>,
54
+ # categories: [ {
55
+ # type: "category",
56
+ # title: <string>,
57
+ # id: <integer>,
58
+ # sections: [ {
59
+ # type: <string>,
60
+ # title: <string>,
61
+ # id: <integer>,
62
+ # modificationTime: <unixtime>,
63
+ # categoryId: <integer>
64
+ # }]}]}
65
+ #
66
+ def getAllCategories()
67
+ getResponse()
68
+ end
69
+
70
+ ##
71
+ # Arguments: id, from, to
72
+ # Cacheable: yes
73
+ # Returns: hash
74
+ # {
75
+ # timestamp: <unixtime>,
76
+ # section_id: <unixtime>,
77
+ # topics: [ {
78
+ # type: "topic",
79
+ # topic_type: <string>,
80
+ # title: <string>,
81
+ # id: <integer>,
82
+ # element_number: <integer>,
83
+ # modificationTime: <unixtime>,
84
+ # postCount: <integer>,
85
+ # author: <string>,
86
+ # lastPost: <string> (author)
87
+ # }]}
88
+ #
89
+ def getTopicsBySectionId(id, from=0, to=20)
90
+ getResponse('id', id, 'from', from, 'to', to)
91
+ end
92
+
93
+ ##
94
+ # Arguments: from, to
95
+ # Cacheable: yes
96
+ # Returns: hash
97
+ # {
98
+ # timestamp: <unixtime>,
99
+ # topics: [ {
100
+ # type: "topic",
101
+ # title: <string>,
102
+ # id: <integer>,
103
+ # modificationTime: <unixtime>,
104
+ # postCount: <integer>,
105
+ # author: <string>,
106
+ # lastPost: <string> (author)
107
+ # }]}
108
+ #
109
+ def getActiveTopics(from=0, to=20)
110
+ getResponse('from', from, 'to', to)
111
+ end
112
+
113
+ ##
114
+ # Arguments: id, direction, count. Optional: number, start_msg_id
115
+ # Cacheable: yes
116
+ # Returns: hash
117
+ # {
118
+ # timestamp: <unixtime>,
119
+ # topic_id: <integer>,
120
+ # messages: [ {
121
+ # type: "post",
122
+ # body: <string>,
123
+ # id: <integer>,
124
+ # element_number: <integer>,
125
+ # createTime: <unixtime>,
126
+ # author: { [
127
+ # nickname: <string>,
128
+ # id: <integer>,
129
+ # userpic: [ { ... }],
130
+ # ]}
131
+ # }]}
132
+ #
133
+ def getPostsByTopicId(id, direction=1, count=1024)
134
+ getResponse('id', id, 'direction', direction, 'count', count)
135
+ end
136
+
137
+ def getLinkedSectionsById(*args)
138
+ return -1, {}
139
+ getResponse
140
+ end
141
+ def addTopicToFavourites()
142
+ return -1, {}
143
+ getResponse
144
+ end
145
+ def getLinkedTopicsById()
146
+ return -1, {}
147
+ getResponse
148
+ end
149
+ def addMessageToTopic()
150
+ return -1, {}
151
+ getResponse
152
+ end
153
+ def getSectionById()
154
+ return -1, {}
155
+ getResponse
156
+ end
157
+ def getTopicById()
158
+ return -1, {}
159
+ getResponse
160
+ end
161
+ def getPostById()
162
+ return -1, {}
163
+ getResponse
164
+ end
165
+
166
+
167
+ # news & last topics
168
+ def getLastTopics()
169
+ return -1, {}
170
+ getResponse
171
+ end
172
+ def getNews()
173
+ return -1, {}
174
+ getResponse
175
+ end
176
+
177
+ # personal messages
178
+ def getUserProfileById()
179
+ return -1, {}
180
+ getResponse
181
+ end
182
+ def getConversations()
183
+ return -1, {}
184
+ getResponse
185
+ end
186
+ def getConversationById()
187
+ return -1, {}
188
+ getResponse
189
+ end
190
+ def getFullMessageById()
191
+ return -1, {}
192
+ getResponse
193
+ end
194
+ def getNewMessageCount()
195
+ return -1, {}
196
+ getResponse
197
+ end
198
+
199
+ # favorite sections manipulation
200
+ def getFavoritesSections()
201
+ return -1, {}
202
+ getResponse
203
+ end
204
+ def getFavoriteTopics()
205
+ return -1, {}
206
+ getResponse
207
+ end
208
+ def removeTopicFromFavourites()
209
+ return -1, {}
210
+ getResponse
211
+ end
212
+ # push notifications
213
+ def registerIosDevice()
214
+ return -1, {}
215
+ getResponse
216
+ end
217
+ def unRegisterIosDevice()
218
+ return -1, {}
219
+ getResponse
220
+ end
221
+ def logout()
222
+ return -1, {}
223
+ getResponse
224
+ end
225
+
226
+ end
227
+
228
+
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gunsru_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Burkov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ description: only few functions are supported
28
+ email: a.p.burkov@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/gunsru_api.rb
34
+ homepage: http://github.com/burkov/gunsru_api
35
+ licenses:
36
+ - LGPL
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.1.8
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: API for guns.ru forum
58
+ test_files: []