1
0

stubhub_agent.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. module Agents
  2. class StubhubAgent < Agent
  3. cannot_receive_events!
  4. description <<-MD
  5. This StubHubAgent creates an event for a given StubHub Event. It can be used to track how many tickets are available for the event and the minimum and maximum price. All that is required is that you paste in the url from the actual event, e.g. http://www.stubhub.com/outside-lands-music-festival-tickets/outside-lands-music-festival-3-day-pass-san-francisco-golden-gate-park-polo-fields-8-8-2014-9020701/
  6. MD
  7. event_description <<-MD
  8. Events looks like this:
  9. {
  10. "url": "http://stubhub.com/valid-event-url"
  11. "name": "Event Name"
  12. "date": "2014-08-01"
  13. "max_price": "999.99"
  14. "min_price": "100.99"
  15. "total_postings": "50"
  16. "total_tickets": "150"
  17. "venue_name": "Venue Name"
  18. }
  19. MD
  20. default_schedule "every_1d"
  21. def working?
  22. event_created_within?(1) && !recent_error_logs?
  23. end
  24. def default_options
  25. { 'url' => 'http://stubhub.com/enter-your-event-here' }
  26. end
  27. def validate_options
  28. errors.add(:base, 'url is required') unless options['url'].present?
  29. end
  30. def url
  31. interpolated['url']
  32. end
  33. def check
  34. create_event :payload => fetch_stubhub_data(url)
  35. end
  36. def fetch_stubhub_data(url)
  37. StubhubFetcher.call(url)
  38. end
  39. class StubhubFetcher
  40. def self.call(url)
  41. new(url).fields
  42. end
  43. def initialize(url)
  44. @url = url
  45. end
  46. def event_id
  47. /(\d*)\/{0,1}\z/.match(url)[1]
  48. end
  49. def base_url
  50. 'http://www.stubhub.com/listingCatalog/select/?q='
  51. end
  52. def build_url
  53. base_url + "%2B+stubhubDocumentType%3Aevent%0D%0A%2B+event_id%3A#{event_id}%0D%0A&start=0&rows=10&wt=json"
  54. end
  55. def response
  56. uri = URI(build_url)
  57. Net::HTTP.get(uri)
  58. end
  59. def parse_response
  60. JSON.parse(response)
  61. end
  62. def fields
  63. stubhub_fields = parse_response['response']['docs'][0]
  64. {
  65. 'url' => url,
  66. 'name' => stubhub_fields['seo_description_en_US'],
  67. 'date' => stubhub_fields['event_date_local'],
  68. 'max_price' => stubhub_fields['maxPrice'].to_s,
  69. 'min_price' => stubhub_fields['minPrice'].to_s,
  70. 'total_postings' => stubhub_fields['totalPostings'].to_s,
  71. 'total_tickets' => stubhub_fields['totalTickets'].to_i.to_s,
  72. 'venue_name' => stubhub_fields['venue_name']
  73. }
  74. end
  75. private
  76. attr_reader :url
  77. end
  78. end
  79. end