Class: PapertimeClient
- Inherits:
-
Object
- Object
- PapertimeClient
- Defined in:
- lib/papertime_client.rb
Instance Method Summary collapse
- #base_url ⇒ Object
- #coverage_areas ⇒ Object
- #find_publication_by_domain(domain) ⇒ Object
- #find_publications_by_lat_lon(lat, lon) ⇒ Object
-
#get_deadline_date(params) ⇒ Object
Compute the deadline for publication on :publication_date given a processing time of :production_days for the publication identified with the :domain e.g.
-
#get_nearest_publication_date(params = {}) ⇒ Object
Compute the nearest possible publication date from a :domain and :desired_publication_date e.g.
-
#get_publication_dates(params) ⇒ Object
Get all viable publication dates after today given a required processing time of :production_days for the publication identified with the :domain e.g.
- #host_from_url(url) ⇒ Object
-
#initialize(domain) ⇒ PapertimeClient
constructor
A new instance of PapertimeClient.
Constructor Details
#initialize(domain) ⇒ PapertimeClient
Returns a new instance of PapertimeClient.
15 16 17 |
# File 'lib/papertime_client.rb', line 15 def initialize(domain) @base_uri = URI.parse(domain)+'api/v1/' end |
Instance Method Details
#base_url ⇒ Object
19 20 21 |
# File 'lib/papertime_client.rb', line 19 def base_url return @base_uri.to_s end |
#coverage_areas ⇒ Object
86 87 88 89 |
# File 'lib/papertime_client.rb', line 86 def coverage_areas uri = @base_uri + 'coverage_areas.json' content_from(uri) end |
#find_publication_by_domain(domain) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/papertime_client.rb', line 71 def find_publication_by_domain(domain) raise ArgumentError, "Cannot retrieve publication without domain" unless domain domain = host_from_url(domain) uri = @base_uri + 'find_publication_by_domain' uri.query = "domain=#{domain}" content_from(uri) end |
#find_publications_by_lat_lon(lat, lon) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/papertime_client.rb', line 79 def find_publications_by_lat_lon(lat,lon) raise ArgumentError, "Cannot retrieve publications without lat/lon" unless (lat and lon) uri = @base_uri + 'find_publications_by_lat_lon' uri.query = "lat=#{lat}&lon=#{lon}" content_from(uri) end |
#get_deadline_date(params) ⇒ Object
Compute the deadline for publication on :publication_date given a processing time of :production_days for the publication identified with the :domain e.g. client.get_deadline_date(:domain => ‘amta.no’, :publication_date => Date.today+9, :production_days => 4) returns the Date
47 48 49 50 51 52 53 54 |
# File 'lib/papertime_client.rb', line 47 def get_deadline_date(params) params[:domain] = host_from_url(params[:domain]) uri = @base_uri+'deadline_date' uri.query = "domain=#{params[:domain]}&publication_date=#{params[:publication_date]}&production_days=#{params[:production_days]}" result = content_from(uri) return Date.parse(result['deadline']) if result['deadline'] result end |
#get_nearest_publication_date(params = {}) ⇒ Object
Compute the nearest possible publication date from a :domain and :desired_publication_date e.g. client.get_nearest_publication_date(:domain => ‘amta.no’, :desired_publication_date => Date.today+9) returns the Date
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/papertime_client.rb', line 59 def get_nearest_publication_date(params = {}) raise ArgumentError, "Cannot retrieve publication date without domain" unless params[:domain] raise ArgumentError, "Cannot retrieve publication date without desired publication date" unless params[:desired_publication_date] domain = host_from_url(params[:domain]) uri = @base_uri + 'nearest_publication_date' uri.query = "domain=#{domain}&desired_publication_date=#{params[:desired_publication_date]}" result = content_from(uri) return if result.nil? return Date.parse(result['publication_date']) if result['publication_date'] result end |
#get_publication_dates(params) ⇒ Object
Get all viable publication dates after today given a required processing time of :production_days for the publication identified with the :domain e.g. client.get_publication_dates(:domain => ‘op.no’, :production_days => 6) returns a hash with the single key ‘issues’ which points to an array of Date-objects
34 35 36 37 38 39 40 41 |
# File 'lib/papertime_client.rb', line 34 def get_publication_dates(params) params[:domain] = host_from_url(params[:domain]) uri = @base_uri+'publication_dates' uri.query = "domain=#{params[:domain]}&production_days=#{params[:production_days]}" result = content_from(uri) result['issues'] = result['issues'].map{|date| Date.parse(date)} result end |
#host_from_url(url) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/papertime_client.rb', line 23 def host_from_url(url) return unless url host = URI.parse(url).host host ||= url host.gsub('www.', '') end |