se-api 0.0.0.beta0 → 0.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 5c80e52ec154387b371f16d32d7dcdec3b712e63
4
- data.tar.gz: bac9206adad39ec1d698ed02ffaaaa0e84203fda
2
+ SHA256:
3
+ metadata.gz: 39c9f55b4b8c90dec1f736ac5c07d1b0b45e0eebd9272eeb90d44077a5be139c
4
+ data.tar.gz: be515d58d9963644cfd9905c7f6aefe6730a8b4f3afc674bff38ecd80dfdef89
5
5
  SHA512:
6
- metadata.gz: a5aede1c3e69e8460aa41293f58462da56aab5e29181a5a90ea6626337929e11802f38ca8cd6dd9e223162e267b3974d01dfe9c5d451602aec8de29785f14df5
7
- data.tar.gz: 2ca20c8b2f5542adfff5596017178971b7c7b065577931931554d180873b8838e85c32d1a63233b363fbcf2a299d0de9d0950917a43eb7260dccd2329958cba8
6
+ metadata.gz: ecaa5d9b19d82802e1f1dcffb22787f434ec7fb11eefb3d2fb4536d7cc839955424e80b90b1d8b02b555493b79eafb14d6b84d9bcc675c630597ead9a377d960
7
+ data.tar.gz: 60ff0cca0fb7e438abd116287119e9241b6443a07c57f9b6b82c1209a7831b8a9ae31c3bafb31adf108d0634564ac8fd84619a637a81cde1365e0f875b2af503
@@ -0,0 +1,6 @@
1
+ module SE
2
+ module API
3
+ class Answer < Post
4
+ end
5
+ end
6
+ end
@@ -1,7 +1,7 @@
1
1
  module SE
2
2
  module API
3
3
  class Post
4
- attr_reader :body, :title, :link, :author, :score, :type
4
+ attr_reader :body, :title, :link, :author, :score, :type, :id, :last_activity_date
5
5
  attr_reader :json
6
6
 
7
7
  def initialize(item_json)
@@ -9,8 +9,10 @@ module SE
9
9
  @body = item_json["body"]
10
10
  @title = item_json["title"]
11
11
  @link = item_json["link"]
12
- @score = item_json["score"]
12
+ @score = item_json["score"].to_i
13
13
  @type = item_json["post_type"]
14
+ @id = item_json["id"].to_i
15
+ @last_activity_date = item_json["last_activity_date"]
14
16
  # @author = User.new(item_json["author"])
15
17
  end
16
18
  end
@@ -0,0 +1,12 @@
1
+ module SE
2
+ module API
3
+ class Question < Post
4
+ attr_reader :answers
5
+
6
+ def initialize(item_json)
7
+ super(item_json)
8
+ @answers = Array(item_json["answers"]).map { |i| Answer.new(i) }
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,5 +1,5 @@
1
1
  module SE
2
2
  module API
3
- VERSION = "0.0.0.beta0"
3
+ VERSION = "0.0.0.beta1"
4
4
  end
5
5
  end
data/lib/se/api.rb CHANGED
@@ -1,22 +1,29 @@
1
1
  require "se/api/version"
2
2
  require "se/api/types/post"
3
+ require "se/api/types/answer"
4
+ require "se/api/types/question"
3
5
 
4
6
  require "net/http"
5
7
  require "json"
6
8
  require "uri"
9
+ require "time"
7
10
 
8
11
  module SE
9
12
  module API
10
13
  class Client
11
14
  API_VERSION = 2.2
12
15
 
13
- attr_reader :quota
16
+ attr_reader :quota, :quota_used
14
17
  attr_accessor :params
15
18
 
16
19
  def initialize(key, **params)
17
20
  @key = key
18
- @params = params
21
+ @params = params.merge({filter: '!*1_).BnZb8pdvWlZpJYNyauMekouxK9-RzUNUrwiB'})
19
22
  @quota = nil
23
+ @quota_used = 0
24
+ @backoff = Time.now
25
+ @logger_raw = Logger.new 'api_raw.log'
26
+ @logger_json = Logger.new 'api_json.log'
20
27
  end
21
28
 
22
29
  def posts(ids = "", **params)
@@ -26,14 +33,46 @@ module SE
26
33
  end
27
34
  end
28
35
 
36
+ def questions(ids = "", **params)
37
+ return if ids == ""
38
+ json("questions/#{Array(ids).join(';')}", **params).map do |i|
39
+ Question.new(i)
40
+ end
41
+ end
42
+
43
+ def answers(ids = "", **params)
44
+ return if ids == ""
45
+ json("answers/#{Array(ids).join(';')}", **params).map do |i|
46
+ Answer.new(i)
47
+ end
48
+ end
49
+
29
50
  private
30
51
 
31
- def json(uri, site: nil, **params)
32
- throw "No site specified" if site.nil?
52
+ def json(uri, **params)
53
+ throw "No site specified" if params[:site].nil?
54
+ backoff_for = @backoff-Time.now
55
+ backoff_for = 0 if backoff_for <= 0
56
+ if backoff_for > 0
57
+ puts "Backing off for #{backoff_for}"
58
+ sleep backoff_for+2
59
+ puts "Finished backing off!"
60
+ end
33
61
  params = @params.merge(params).merge({key: @key}).map { |k,v| "#{k}=#{v}" }.join('&')
34
- resp_raw = Net::HTTP.get_response(URI("https://api.stackexchange.com/#{API_VERSION}/#{uri}?#{params}")).body
62
+ puts "Posting to https://api.stackexchange.com/#{API_VERSION}/#{uri}?#{params}"
63
+ begin
64
+ resp_raw = Net::HTTP.get_response(URI("https://api.stackexchange.com/#{API_VERSION}/#{uri}?#{params}")).body
65
+ rescue Net::OpenTimeout, SocketError => e
66
+ puts "Got timeout on API request (#{e}). Retrying..."
67
+ sleep 0.3
68
+ retry
69
+ end
70
+ @logger_raw.info "https://api.stackexchange.com/#{API_VERSION}/#{uri}?#{params} => #{resp_raw}"
35
71
  resp = JSON.parse(resp_raw)
72
+ @backoff = Time.now + resp["backoff"].to_i
73
+ @logger_json.info "https://api.stackexchange.com/#{API_VERSION}/#{uri}?#{params} => #{resp}"
36
74
  @quota = resp["quota_remaining"]
75
+ @quota_used += 1
37
76
  resp["items"]
38
77
  end
39
78
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: se-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0.beta0
4
+ version: 0.0.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - thesecretmaster
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-14 00:00:00.000000000 Z
11
+ date: 2017-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,7 +69,9 @@ files:
69
69
  - bin/console
70
70
  - bin/setup
71
71
  - lib/se/api.rb
72
+ - lib/se/api/types/answer.rb
72
73
  - lib/se/api/types/post.rb
74
+ - lib/se/api/types/question.rb
73
75
  - lib/se/api/version.rb
74
76
  - se-api.gemspec
75
77
  homepage: https://github.com/izwick-schachter/se-api
@@ -93,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
95
  version: 1.3.1
94
96
  requirements: []
95
97
  rubyforge_project:
96
- rubygems_version: 2.6.11
98
+ rubygems_version: 2.7.3
97
99
  signing_key:
98
100
  specification_version: 4
99
101
  summary: A ruby wrapper for the Stack Exchange api