File: utils.rb

package info (click to toggle)
mikutter 3.5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,256 kB
  • ctags: 2,165
  • sloc: ruby: 19,079; sh: 205; makefile: 20
file content (52 lines) | stat: -rw-r--r-- 1,452 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
# -*- coding: utf-8 -*-

require "mikutwitter/basic"
require "lazy"
require "cgi"

module MikuTwitter::Utils

  attr_accessor :twitter_host, :base_path

  EXCLUDE_OPTIONS = [:cache].freeze

  def initialize(*a, &b)
    @twitter_host = 'api.twitter.com'
    @base_path = "https://#{@twitter_host}/1.1".freeze
    super(*a, &b)
  end

  # 連想配列を受け取って、QueryStringを生成して返す
  # ==== Args
  # [args] QueryStringのHash
  # ==== Return
  # URLの?以降のクエリ文字列("?"含む)。無い場合は空の文字列
  def get_args(args)
    filtered = lazy{ args.select{|k, v| not EXCLUDE_OPTIONS.include? k } }
    if not(args.empty? or filtered.empty?)
      "?" + filtered.map{|pair| "#{CGI.escape(pair[0].to_s).to_s}=#{CGI.escape(pair[1].to_s).to_s}"}.join('&')
    else
      '' end end

  def line_accumlator(splitter="\n", &proc)
    splitter.freeze
    buffer = ""
    push = ->(str){ proc.(buffer + str); buffer = "" }
    ->(chunk){
      if chunk.end_with?(splitter)
        objects = chunk.split(splitter)
        if objects.empty? then push.("") else objects.each(&push) end
      else
        *objects, last = chunk.split(splitter)
        objects.each(&push)
        buffer += (last || "") end } end
end

class << CGI
  alias __escape_aoVit97__ escape
  def escape(*args)
    __escape_aoVit97__(*args).gsub(/[*:]/){|m| "%" + m.unpack("H2") }
  end
end

class MikuTwitter; include MikuTwitter::Utils end