/
client.rb
145 lines (128 loc) · 3.74 KB
/
client.rb
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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# -*- coding: utf-8 -*-
require 'net/http'
require 'uri'
module OpensocialWap
module Client
class Base
attr_accessor :options, :endpoint, :port, :headers, :post_data
def initialize(args)
args.each do |k, v|
instance_variable_set "@#{k.to_s}", v
end
@port ||= 80
end
# Authorizationヘッダを計算して返す.
def authorization_header
client_helper.header
end
# API 呼び出し実行.
def run
raise 'Run method should be implemented in the sub classes.'
end
# HTTPリクエストオブジェクトを返す.
def request
raise 'Request method should be implemented in the sub classes.'
end
# 引数から接続先URLを構築してセットする.
def method_missing(name, *args)
unless endpoint
raise "Endpoint is not set."
end
@url = endpoint
# 最後の引数が Hash であれば、GETパラメータとする.
get_params = nil
@url << '/' if @url[-1] != '/'
if args[-1] && (args[-1].is_a? Hash)
get_params = args.pop
end
# URL を構築.
@url << "#{name}/"
@url << args.map{|arg| arg.to_s}.join('/')
@url << '/'
if get_params && get_params.size > 0
@url << (@url.index('?') ? '&' : '?')
@url << parameter_string(get_params)
end
@uri = URI.parse(@url)
self
end
# リクエスト先URLを返す.
def url
@url
end
private
def client_helper
OpensocialWap::Client::Helper.new(request, options.merge(:request_uri => @url))
end
def parameter_string(hash)
str = ''
if hash && hash.size > 0
str << hash.map{|k,v| "#{k.to_s}=#{URI.encode(v.to_s, /[^\w]/)}"}.join('&')
end
str
end
def post_data_string
case @post_data
when String
@post_data
when Hash
parameter_string(@post_data)
else
nil
end
end
end
class Get < Base
def request
Net::HTTP::Get.new(@uri.request_uri)
end
def run
request_headers = (headers || {}).merge("Authorization" => authorization_header)
Net::HTTP::start(@uri.host, @port) do |http|
@response = http.get(@uri.request_uri, request_headers)
end
end
@response
end
class Post < Base
def request
req = Net::HTTP::Post.new(@uri.request_uri)
req.body = post_data_string
req
end
def run
request_headers = (headers || {}).merge("Authorization" => authorization_header)
Net::HTTP::start(@uri.host, @port) do |http|
@response = http.post(@uri.request_uri, post_data_string, request_headers)
end
@response
end
end
class Head < Base
def request
Net::HTTP::Head.new(@uri.request_uri)
end
def run
request_headers = (headers || {}).merge("Authorization" => authorization_header)
Net::HTTP::start(@uri.host, @port) do |http|
@response = http.head(@uri.request_uri, request_headers)
end
@response
end
end
class Put < Base
def request
Net::HTTP::Put.new(@uri.request_uri)
req.body = post_data_string
req
end
def run
request_headers = (headers || {}).merge("Authorization" => authorization_header)
Net::HTTP::start(@uri.host, @port) do |http|
@response = http.put(@uri.request_uri, post_data_string, request_headers)
end
@response
end
end
end
end