Module: TwicasStream

Extended by:
RequestHeader
Defined in:
lib/twicas_stream/version.rb,
lib/twicas_stream.rb,
lib/twicas_stream/user.rb,
lib/twicas_stream/movie.rb,
lib/twicas_stream/search.rb,
lib/twicas_stream/comment.rb,
lib/twicas_stream/category.rb,
lib/twicas_stream/supporter.rb,
lib/twicas_stream/live_thumbnail.rb,
lib/twicas_stream/request_header.rb,
lib/twicas_stream/twicas_api_object/app.rb,
lib/twicas_stream/twicas_api_object/user.rb,
lib/twicas_stream/twicas_api_object/error.rb,
lib/twicas_stream/twicas_api_object/movie.rb,
lib/twicas_stream/twicas_api_object/comment.rb,
lib/twicas_stream/twicas_api_object/category.rb,
lib/twicas_stream/twicas_api_object/sub_category.rb,
lib/twicas_stream/twicas_api_object/supporter_user.rb

Overview

! /opt/local/bin/ruby coding: utf-8

Defined Under Namespace

Modules: Category, Comment, LiveThumbnail, Movie, RequestHeader, Search, Supporter, TwicasApiObject, User

Constant Summary collapse

BASE_URL =
'https://apiv2.twitcasting.tv'
VERSION =
'1.3.1'

Constants included from RequestHeader

RequestHeader::DEFAULT_ACCEPT_ENCODING, RequestHeader::DEFAULT_ACCESS_TOKEN, RequestHeader::DEFAULT_API_VERSION, RequestHeader::REQUEST_HEADER

Class Method Summary collapse

Methods included from RequestHeader

configure, extended, reset

Class Method Details

.body(result) ⇒ Object



81
82
83
# File 'lib/twicas_stream.rb', line 81

def body result
	result[:body]
end

.get(url) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/twicas_stream.rb', line 89

def get url
	c = Curl.get(url) do |curl|
		TwicasStream.configure do |request_header|
			curl.headers['X-Api-Version'] = request_header.api_version
			curl.headers['Accept'] = request_header.accept_encoding
			curl.headers['Authorization'] = 'Bearer ' + request_header.access_token
		end
	end

	response, *headers = c.header_str.split(/[\r\n]+/).map(&:strip)
	headers = Hash[headers.flat_map{ |s| s.scan(/^(\S+): (.+)/) }]

	{ :header => headers, :body => JSON.parse(c.body_str), :response_code => c.response_code }
end

.get_image(url, path) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/twicas_stream.rb', line 104

def get_image url, path
	c = Curl.get(url)

	response, *headers = c.header_str.split(/[\r\n]+/).map(&:strip)
	headers = Hash[headers.flat_map{ |s| s.scan(/^(\S+): (.+)/) }]

	save_image(path, headers['Location'])
end

.header(result) ⇒ Object



85
86
87
# File 'lib/twicas_stream.rb', line 85

def header result
	result[:header]
end

.make_query_string(options) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/twicas_stream.rb', line 113

def make_query_string options
	query = "?"
	options.each do |key, value|
		query += "#{key}=#{value.to_s.gsub(" ", "+")}&"
	end

	query[0...-1]
end

.parse(result) ⇒ Object



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

def parse result
	response = Hash.new

	response_code = response_code(result)
	body = body(result)
	header = header(result)

	body.each{ |key, value|
		# special if statement for the key into following method
		# * 'movies' key of 'SearchLiveMovies' method
		# * 'movies' key of 'GetMoviesbyUser' method
		if key == 'movies'
			# 'SearchLiveMovies'
			if is_TwicasApiObject?(value.first.keys.first)
				response[key.to_sym] = value.map{ |i|
					child_response = Hash.new

					i.each{ |child_key, child_value| child_response[child_key.to_sym] = parse_deep(child_key, child_value) }

					child_response
				}
			# 'GetMoviesbyUser'
			else
				response[key.to_sym] = value.map{ |i| parse_deep(key, i) }
			end

		# special if statement for 'SupporterUser' as TwicasApiObject into 'SupportingList' and 'SupporterList' method, because these keys are is_TwicasApiObject?(key) = false
		# * 'supporting' key of 'SupportingList' method
		# * 'supporters' key of 'SupporterList' method
		elsif key == 'supporting' or key == 'supporters'
			response[key.to_sym] = value.map{ |i| parse_deep('SupporterUser', i) }

		# plural form
		# * 'users' key of 'SearchUsers' method
		# * 'categories' key of 'GetCategories' method
		# * 'comments' key of 'GetComments' method
		elsif (key != key.singularize) and is_TwicasApiObject?(key)
			response[key.to_sym] = value.map{ |i| parse_deep(key, i) }

		# singular form, and others
		else
			response[key.to_sym] = parse_deep(key, value)

		end
	}

	response
end

.response_code(result) ⇒ Object



77
78
79
# File 'lib/twicas_stream.rb', line 77

def response_code result
	result[:response_code]
end