Module: Sinatra::Couchpotato

Defined in:
lib/alexa_couchpotato.rb,
lib/alexa_couchpotato/version.rb

Constant Summary collapse

VERSION =
"1.0.10"

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/alexa_couchpotato.rb', line 14

def self.registered(app)
  app.before do
    if request.request_method == "POST"
      @data = request.body.read
      params.merge!(JSON.parse(@data))
      @echo_request = AlexaObjects::EchoRequest.new(JSON.parse(@data)) 
      @application_id = @echo_request.application_id
    end
  end

  app.post '/alexa_cp' do
    content_type :json

    # halt 400, "Invalid Application ID" unless @application_id == "your application id here"

    if @echo_request.launch_request?
      response = AlexaObjects::Response.new
      response.spoken_response = "I'm ready to download you movies."
      response.end_session = false
      response.without_card.to_json
      puts @echo_request.slots
    elsif @echo_request.intent_name == "ControlCouchpotato"
      count = 1
      begin
        case count
        when 1
          title = @echo_request.slots["movie"]
        when 2
          word_arr =  @echo_request.slots["movie"].split(' ')
          nums = word_arr.map {|x|x.in_numbers}
          title = ""
          word_arr.each_with_index do |x, index|
            title << (nums[index] != 0 ? "#{nums[index]}" : x)
            title << " " unless index == word_arr.length-1
          end
        end
        query = HTTParty.get(URI.escape("http://highasfuck.science/movies/api/550dfd233ffe453096c39293f4e38121/search?q=#{title}"))
        if query["movies"] && query["movies"].count == 1
          movie = query["movies"].first
        elsif query["movies"] && query["movies"].count > 1
          movie = query["movies"]
        end
        movie = false if count == 5 && movie.nil?
        count += 1      
      end until movie != nil
      response = AlexaObjects::Response.new

      if [movie].flatten(1).count == 1 && movie != false
        add_query = HTTParty.get(URI.escape("http://highasfuck.science/movies/api/550dfd233ffe453096c39293f4e38121/movie.add?title=#{movie["titles"].first}&identifier#{movie["imdb"]}"))
        if add_query["success"] = true
          response.end_session = true
          response.spoken_response = "okay, downloading #{movie["titles"].first}"
        elsif movie && add_query["success"] = false
          response.end_session = true
          response.spoken_response = "sorry, I wasn't able to find #{@echo_request.slots["movie"]} because it wouldn't add to the server"
        end
      elsif movie != false && [movie].flatten(1).count > 1
        @@movies = movie
        response.end_session = false
        response.reprompt_text = "Which movie would you like me to download? #{movie.map {|m| m["titles"].first }.to_sentence(:last_word_connector => ' or ')}"
      elsif movie == false
        response.end_session = true
        response.spoken_response = "sorry, I wasn't able to find #{@echo_request.slots["movie"]} because the search came up empty"
      else
        response.end_session = true
        response.spoken_response = "sorry, I wasn't able to find #{@echo_request.slots["movie"]} because the search came up empty"
      end

      response.without_card.to_json
    elsif @echo_request.intent_name == "RespondCouchpotato"
      ordinal = Chronic::Numerizer.numerize(@echo_request.slots["listvalue"]).split(' ').last
      number = ordinal[0]
      movie = @@movies[number.to_i+1]
      query = HTTParty.get(URI.escape("http://highasfuck.science/movies/api/550dfd233ffe453096c39293f4e38121/movie.add?title=#{movie["titles"].first}&identifier#{movie["imdb"]}"))
      response = AlexaObjects::Response.new
      if movie != false && query["success"] = true
        response.end_session = true
        response.spoken_response = "okay, downloading #{movie["titles"].first}"
      elsif movie == false
        response.end_session = true
        response.spoken_response = "sorry, I wasn't able to find #{@echo_request.slots["movie"]} because the search came up empty"
      elsif movie && query["success"] = false
        response.end_session = true
        response.spoken_response = "sorry, I wasn't able to find #{@echo_request.slots["movie"]} because it wouldn't add to the server"
      end
      response.without_card.to_json
    elsif @echo_request.intent_name == "EndSession"
      puts @echo_request.slots
      response = AlexaObjects::Response.new
      response.end_session = true
      response.spoken_response = "exiting couchpoato"
      response.without_card.to_json
    elsif @echo_request.session_ended_request?
      response = AlexaObjects::Response.new
      response.end_session = true
      response.without_card.to_json
    end
  end
end