Class: MemeGenerator::GroupMe

Inherits:
Object
  • Object
show all
Defined in:
lib/meme_generator/groupme.rb

Constant Summary collapse

MEMEGEN_PATH =
File.expand_path("~/.memegen")
CONFIG_PATH =
File.join(MEMEGEN_PATH, ".groupme")

Class Method Summary collapse

Class Method Details

.auth_user(phonenumber, password) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/meme_generator/groupme.rb', line 37

def auth_user(phonenumber, password)
  groupme_url = "https://v2.groupme.com/access_tokens"
  uri = URI.parse(groupme_url)
  request = Net::HTTP::Post.new(uri.request_uri)
  request.set_form_data({"grant_type" => "password", "phone_number" => phonenumber, "password" => password, "app_id" => "memegen"});
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  response = http.request(request)
  json_res = JSON.parse(response.body)
  token = json_res["response"]["access_token"]
  return token
end

.configObject



12
13
14
15
# File 'lib/meme_generator/groupme.rb', line 12

def config
  return unless File.exists?(CONFIG_PATH)
  @config ||= read_config
end

.prompt_configObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/meme_generator/groupme.rb', line 17

def prompt_config
  require "readline"
  require "highline/import"
  puts "Set your GroupMe credentials..." unless config

  phonenumber     = Readline.readline("Phone #: ").strip
  password = ask("Password: ")  { |input| input.echo = false }
  room      = Readline.readline("Group Id: ").strip
  message   = Readline.readline("Message (optional): ").strip
  
  token = auth_user(phonenumber, password)
  
  if (token == nil)
    puts "Error authorizing user"
  else
    write_config([token, room, message])
    puts "Config saved successfully!"
  end
end

.upload(path) ⇒ Object



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
# File 'lib/meme_generator/groupme.rb', line 50

def upload(path)
  prompt_config unless config

  puts "Uploading... "
  silence_stream(STDERR) do
  
    url = URI.parse('http://groupme-image-service.heroku.com/images');
    File.open(path) do |jpg|
      req = Net::HTTP::Post::Multipart.new url.path, "file" => UploadIO.new(jpg, "image/jpeg", "file.jpg")
      res = Net::HTTP.start(url.host, url.port) do |http|
          http.request(req)
      end
      json_res = JSON.parse(res.body)
      img_url = json_res['payload']['picture_url']
    
      groupme_url = "https://v2.groupme.com/groups/#{config[:room]}/messages?app_id=memgen&token=#{config[:token]}"
    
      msg_json = {'message' => {'text' => config[:message], 'picture_url' => img_url}}.to_json
    
      puts "Posting Message..."
    
      url = URI.parse(groupme_url)
      req = Net::HTTP::Post.new url.request_uri
      req.body = msg_json.to_s
      req['Content-Type'] = "text/json"
      http = Net::HTTP.new(url.host, url.port)
      http.use_ssl = true
      http.request(req)
    
    end
  end
  puts "Done!"
end