Class: Happyscribe::Transcript

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

Overview

Your code goes here…

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Transcript

Returns a new instance of Transcript.



11
12
13
14
15
# File 'lib/happyscribe.rb', line 11

def initialize(key)
  @api_key = key
  @base = "https://www.happyscribe.co/api/v1"
  @auth = "Bearer #{@api_key}"
end

Instance Method Details

#create(url, title = "", language = "fr-FR") ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/happyscribe.rb', line 16

def create(url,title="",language="fr-FR")
  endpoint = "#{@base}/transcriptions"
  uri = URI.parse(endpoint)
  request = Net::HTTP::Post.new(uri)
  request.content_type = "application/json"
  request["Authorization"] = @auth

  request.body = JSON.dump({
    "transcription" => {
      "name" => (title=="" ? SecureRandom.alphanumeric : title ),
      "language" => language,
      "tmp_url" => url
    }
  })

  req_options = {
    use_ssl: uri.scheme == "https",
  }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
  return JSON.parse response.body
end

#create_export(id, format = "txt", timestamps = false, speakers = false, comments = false, highlights = false) ⇒ Object



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
# File 'lib/happyscribe.rb', line 70

def create_export(id,format="txt",timestamps=false,speakers=false,comments=false,highlights=false)
  uri = URI.parse("#{@base}/exports")
  request = Net::HTTP::Post.new(uri)
  request.content_type = "application/json"
  request["Authorization"] = @auth
  request.body = JSON.dump({
    "export" => {
      "format" => format,
      "show_timestamps" => timestamps,
      "show_speakers" => speakers,
      "show_comments" => comments,
      "show_highlights" => highlights,
      "transcription_ids" => [
        id
      ]
    }
  })

  req_options = {
    use_ssl: uri.scheme == "https",
  }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
  return JSON.parse response.body
end

#export_in_txt(export_id) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/happyscribe.rb', line 112

def export_in_txt(export_id)
  export = create_export(export_id,"txt")["id"]
  sleep(5)
  while true
    retrieved = retrieve_export(export)
    if(retrieved["state"]=="ready")
      break
    else
      sleep(2)
    end
  end
  txt = URI.parse(retrieved["download_link"]).open.read.force_encoding("utf-8")
  return txt

end

#listObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/happyscribe.rb', line 40

def list
  endpoint = "#{@base}/transcriptions"
  uri = URI.parse(endpoint)
  request = Net::HTTP::Get.new(uri)
  request["Authorization"] = @auth

  req_options = {
    use_ssl: uri.scheme == "https",
  }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
  return JSON.parse response.body
end

#retrieve(id) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/happyscribe.rb', line 55

def retrieve(id)
  endpoint = "#{@base}/transcriptions/#{id}"

  uri = URI.parse(endpoint)
  request = Net::HTTP::Get.new(uri)
  request["Authorization"] = @auth

  req_options = {
    use_ssl: uri.scheme == "https",
  }
  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
  return JSON.parse(response.body)
end

#retrieve_export(export_id) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/happyscribe.rb', line 97

def retrieve_export(export_id)
  endpoint = "#{@base}/exports/#{export_id}"
  uri = URI.parse(endpoint)
  request = Net::HTTP::Get.new(uri)
  request["Authorization"] = @auth

  req_options = {
    use_ssl: uri.scheme == "https",
  }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
  return JSON.parse response.body
end