seetemap-client 0.0.14 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,181 @@
1
+ $:.<< File.dirname(__FILE__)
2
+
3
+ require 'sinatra'
4
+ require 'httparty'
5
+ require 'fileutils'
6
+ require 'yaml'
7
+
8
+ require 'seetemap_client/version'
9
+
10
+ module SeetemapClient
11
+ SITEMAP_PATH = "tmp/sitemap.xml"
12
+
13
+ class Seetemap
14
+ include HTTParty
15
+ base_uri 'https://seetemap.com'
16
+
17
+ # hack: don't auto parse result
18
+ NO_PARSER = Proc.new { |body, format| body }
19
+
20
+ def self.config(auth_token, site_token)
21
+ @@auth_token = auth_token
22
+ @@site_token = site_token
23
+ end
24
+
25
+ # You must be certain that fetch! returns a valid status code before calling
26
+ # this function.
27
+ #
28
+ # @return [String] sitemap content
29
+ def self.sitemap
30
+ get("/fr/dashboard/websites/#{@@site_token}.xml",
31
+ :format => :xml,
32
+ :query => { :auth_token => @@auth_token },
33
+ :parser => NO_PARSER)
34
+ end
35
+
36
+ # Cache the audit list inside the Seetemap class. Must be called for each
37
+ # interaction with the server that need fresh information about the audits.
38
+ #
39
+ # This call returns an http code matching the current situation:
40
+ # - 404 : no remote audit found, check your api_key
41
+ # - 401 : your not authorized to get this site map, check your token
42
+ # - 500 : server error / malformed response
43
+ # - 204 : no audit is available
44
+ # - 200 : an audit is available
45
+ #
46
+ # @return[Integer] response code
47
+ def self.fetch!
48
+ @@response = get("/fr/dashboard/websites/#{@@site_token}/audits.json",
49
+ :query => { :auth_token => @@auth_token })
50
+
51
+ return @@response.code if @@response.nil? || @@response.code != 200
52
+
53
+ @@last_audit = @@response.parsed_response.first
54
+ @@last_finished_audit = @@response.parsed_response.find {|audit| audit["finished_at"]}
55
+
56
+ if @@last_audit.nil? or @@last_finished_audit.nil?
57
+ 204
58
+ else
59
+ 200
60
+ end
61
+ end
62
+
63
+ # Get the most recent timestamp that can be applyied to the audit when
64
+ # there is no fresher audit available.
65
+ #
66
+ # @param [Time] actual timestamp of the current cached sitemap
67
+ # @return [nil|Time] nil if there is a fresher audit available or the time to update
68
+ def self.fresh?(time)
69
+ if @@last_audit.nil? or @@last_finished_audit.nil?
70
+ nil
71
+ else
72
+ result = if @@last_audit["finished_at"].nil?
73
+ Time.parse(@@last_audit["requested_at"]) rescue time
74
+ else
75
+ Time.now
76
+ end
77
+ last_modified = Time.parse(@@last_finished_audit["finished_at"]) rescue time
78
+ time > last_modified ? result : nil
79
+ end
80
+ end
81
+
82
+ def self.ping_google(mount_point)
83
+ url = mount_point.chomp('/') << '/sitemap.xml'
84
+ escaped_url = URI.escape(url, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
85
+ HTTParty.get('http://www.google.com/webmasters/tools/ping?sitemap=' + escaped_url)
86
+ end
87
+ end
88
+
89
+ class Application < Sinatra::Base
90
+ get '/sitemap' do
91
+ content_type 'text/xml'
92
+ render_sitemap(params[:force_reload])
93
+ end
94
+
95
+ get '/sitemap.xml' do
96
+ content_type 'text/xml'
97
+ render_sitemap(params[:force_reload])
98
+ end
99
+
100
+ get '/seetemap/version' do
101
+ content_type 'application/json'
102
+ "{\"version\":\"#{SeetemapClient::VERSION}\"}"
103
+ end
104
+
105
+ get '/seetemap/purge' do
106
+ remove_sitemap_file
107
+ nil
108
+ end
109
+
110
+ get '/seetemap/ping' do
111
+ Seetemap.config(configuration["auth_token"], configuration["site_token"])
112
+ code = Seetemap.fetch!
113
+ unless code != 200
114
+ remove_sitemap_file
115
+ create_sitemap_file
116
+ if params[:fwd_google] && mount_point
117
+ Seetemap.ping_google(mount_point)
118
+ end
119
+ end
120
+ status code
121
+ end
122
+
123
+ private
124
+
125
+ def environment
126
+ @environment ||= ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
127
+ end
128
+
129
+ def configuration
130
+ @configuration ||= YAML.load(File.open("config/seetemap.yml"))[environment]
131
+ end
132
+
133
+ def mount_point
134
+ configuration["mount_point"]
135
+ end
136
+
137
+ def locally_fresh?(time)
138
+ time > (Time.now - configuration["keep_delay"] || 3600)
139
+ end
140
+
141
+ def render_sitemap(force_reload)
142
+ Seetemap.config(configuration["auth_token"], configuration["site_token"])
143
+ code = Seetemap.fetch!
144
+ case code
145
+ when 200
146
+ if File.exists?(SeetemapClient::SITEMAP_PATH)
147
+ time = File.mtime(SeetemapClient::SITEMAP_PATH)
148
+ if force_reload
149
+ create_sitemap_file
150
+ elsif locally_fresh?(time)
151
+ File.read(SeetemapClient::SITEMAP_PATH)
152
+ elsif (time_to_update = Seetemap.fresh?(time))
153
+ FileUtils.touch SeetemapClient::SITEMAP_PATH, :mtime => time_to_update
154
+ File.read(SeetemapClient::SITEMAP_PATH)
155
+ else
156
+ create_sitemap_file
157
+ end
158
+ else
159
+ create_sitemap_file
160
+ end
161
+ else
162
+ status code
163
+ nil
164
+ end
165
+ end
166
+
167
+ def remove_sitemap_file
168
+ if File.exists?(SeetemapClient::SITEMAP_PATH)
169
+ File.delete(SeetemapClient::SITEMAP_PATH)
170
+ end
171
+ end
172
+
173
+ def create_sitemap_file
174
+ base_path = File.dirname(SeetemapClient::SITEMAP_PATH)
175
+ Dir.mkdir(base_path) unless Dir.exists?(base_path)
176
+ sitemap = Seetemap.sitemap
177
+ File.open(SeetemapClient::SITEMAP_PATH, "w+") { |file| file.write(sitemap) }
178
+ sitemap.to_s
179
+ end
180
+ end
181
+ end
@@ -0,0 +1,3 @@
1
+ module SeetemapClient
2
+ VERSION = "0.0.15"
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seetemap-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14
4
+ version: 0.0.15
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-09 00:00:00.000000000 Z
12
+ date: 2013-01-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra
@@ -50,6 +50,8 @@ executables: []
50
50
  extensions: []
51
51
  extra_rdoc_files: []
52
52
  files:
53
+ - lib/seetemap_client.rb
54
+ - lib/seetemap_client/version.rb
53
55
  - README.md
54
56
  - CHANGELOG.md
55
57
  homepage: https://github.com/synbioz/seetemap-client