stubhub_agent.rb 2.6 KB

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