papertime_client 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/papertime_client.rb +108 -0
- metadata +79 -0
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'httpclient'
|
2
|
+
require 'json'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
# Use by creating a client given the domain where papertime is to be found
|
6
|
+
# e.g. client = PapertimeClient.new('http://localhost:3001')
|
7
|
+
#
|
8
|
+
# then making requests like so:
|
9
|
+
#
|
10
|
+
# client.get_publication_dates(:domain => 'op.no', :production_days => 6)
|
11
|
+
# client.get_deadline_date(:domain => 'amta.no', :publication_date => Date.today+9, :production_days => 4)
|
12
|
+
|
13
|
+
|
14
|
+
class PapertimeClient
|
15
|
+
def initialize(domain)
|
16
|
+
@base_uri = URI.parse(domain)+'api/v1/'
|
17
|
+
end
|
18
|
+
|
19
|
+
def base_url
|
20
|
+
return @base_uri.to_s
|
21
|
+
end
|
22
|
+
|
23
|
+
def host_from_url(url)
|
24
|
+
return unless url
|
25
|
+
host = URI.parse(url).host
|
26
|
+
host ||= url
|
27
|
+
host.gsub('www.', '')
|
28
|
+
end
|
29
|
+
|
30
|
+
# Get all viable publication dates after today given a required processing time
|
31
|
+
# of :production_days for the publication identified with the :domain
|
32
|
+
# e.g. client.get_publication_dates(:domain => 'op.no', :production_days => 6)
|
33
|
+
# returns a hash with the single key 'issues' which points to an array of Date-objects
|
34
|
+
def get_publication_dates(params)
|
35
|
+
params[:domain] = host_from_url(params[:domain])
|
36
|
+
uri = @base_uri+'publication_dates'
|
37
|
+
uri.query = "domain=#{params[:domain]}&production_days=#{params[:production_days]}"
|
38
|
+
result = content_from(uri)
|
39
|
+
result['issues'] = result['issues'].map{|date| Date.parse(date)}
|
40
|
+
result
|
41
|
+
end
|
42
|
+
|
43
|
+
# Compute the deadline for publication on :publication_date given a processing time
|
44
|
+
# of :production_days for the publication identified with the :domain
|
45
|
+
# e.g. client.get_deadline_date(:domain => 'amta.no', :publication_date => Date.today+9, :production_days => 4)
|
46
|
+
# returns the Date
|
47
|
+
def get_deadline_date(params)
|
48
|
+
params[:domain] = host_from_url(params[:domain])
|
49
|
+
uri = @base_uri+'deadline_date'
|
50
|
+
uri.query = "domain=#{params[:domain]}&publication_date=#{params[:publication_date]}&production_days=#{params[:production_days]}"
|
51
|
+
result = content_from(uri)
|
52
|
+
return Date.parse(result['deadline']) if result['deadline']
|
53
|
+
result
|
54
|
+
end
|
55
|
+
|
56
|
+
# Compute the nearest possible publication date from a :domain and :desired_publication_date
|
57
|
+
# e.g. client.get_nearest_publication_date(:domain => 'amta.no', :desired_publication_date => Date.today+9)
|
58
|
+
# returns the Date
|
59
|
+
def get_nearest_publication_date(params = {})
|
60
|
+
raise ArgumentError, "Cannot retrieve publication date without domain" unless params[:domain]
|
61
|
+
raise ArgumentError, "Cannot retrieve publication date without desired publication date" unless params[:desired_publication_date]
|
62
|
+
domain = host_from_url(params[:domain])
|
63
|
+
uri = @base_uri + 'nearest_publication_date'
|
64
|
+
uri.query = "domain=#{domain}&desired_publication_date=#{params[:desired_publication_date]}"
|
65
|
+
result = content_from(uri)
|
66
|
+
return if result.nil?
|
67
|
+
return Date.parse(result['publication_date']) if result['publication_date']
|
68
|
+
result
|
69
|
+
end
|
70
|
+
|
71
|
+
def find_publication_by_domain(domain)
|
72
|
+
raise ArgumentError, "Cannot retrieve publication without domain" unless domain
|
73
|
+
domain = host_from_url(domain)
|
74
|
+
uri = @base_uri + 'find_publication_by_domain'
|
75
|
+
uri.query = "domain=#{domain}"
|
76
|
+
content_from(uri)
|
77
|
+
end
|
78
|
+
|
79
|
+
def find_publications_by_lat_lon(lat,lon)
|
80
|
+
raise ArgumentError, "Cannot retrieve publications without lat/lon" unless (lat and lon)
|
81
|
+
uri = @base_uri + 'find_publications_by_lat_lon'
|
82
|
+
uri.query = "lat=#{lat}&lon=#{lon}"
|
83
|
+
content_from(uri)
|
84
|
+
end
|
85
|
+
|
86
|
+
def coverage_areas
|
87
|
+
uri = @base_uri + 'coverage_areas.json'
|
88
|
+
content_from(uri)
|
89
|
+
end
|
90
|
+
|
91
|
+
private
|
92
|
+
|
93
|
+
def content_from(uri)
|
94
|
+
begin
|
95
|
+
client = HTTPClient.new.get_content(uri)
|
96
|
+
return JSON.parse(client)
|
97
|
+
rescue Errno::ECONNREFUSED => e
|
98
|
+
puts("Could not connect to papertime server: #{e.message}")
|
99
|
+
puts e.inspect
|
100
|
+
puts e.backtrace
|
101
|
+
rescue Exception => e
|
102
|
+
puts("Something went wrong while calling papertime api: #{e.message}")
|
103
|
+
puts e.inspect
|
104
|
+
puts e.backtrace
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: papertime_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Origogruppen AS
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: httpclient
|
16
|
+
requirement: &70130659978060 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70130659978060
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: json
|
27
|
+
requirement: &70130659977060 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70130659977060
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
requirement: &70130659975100 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70130659975100
|
47
|
+
description: Papertime is a server giving details about Norwegian newspapers. This
|
48
|
+
is a client for connecting to that server.
|
49
|
+
email: post@origo.no
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- lib/papertime_client.rb
|
55
|
+
homepage: https://github.com/origo/papertime_client
|
56
|
+
licenses: []
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.8.10
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: For talking to Papertime server
|
79
|
+
test_files: []
|