twitter_api 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2c6f063d323e4b5ffc8d9da41f755ff8ab599604
4
- data.tar.gz: 8f479fb41d9b9beb14adaec660e52c2aadfa997d
3
+ metadata.gz: 687522614099fad8793bf736f27656511b23f6f9
4
+ data.tar.gz: 1ab167f48de4390e4cc094ba6e64f705cee7867e
5
5
  SHA512:
6
- metadata.gz: 9f8986dba125010bcea820f0060c9c8542e964572d273ad41d75c83e79c82cec75439076c7af68580c5bf197fd3d2206e3408512a40b55bf90050c5c33352279
7
- data.tar.gz: 3e2617d3675fa43cf3deee8384163d89721da50e9248d38ef5be1102d6fdeaa3979ab7035ca94620195f4e3ad1b033bcc7be668a6d0ab5431bf9d3fd9773a968
6
+ metadata.gz: dd9415e903be0cf2dbc90b8225536ca52791e12c5a1b49fbc7960acd0224b9b3c6a2c8723e7bba103183f8472735c53a347775062842f29a002dc438809261f4
7
+ data.tar.gz: '02295227407b49e5d3d651075cfa270a64ce1a909704ffcd28c67487ce29fc3cbec9e1cf21d5560c7ac400fceccfe33a08083e4b01fc50868cf223694890b520'
@@ -9,15 +9,94 @@ require 'tempfile'
9
9
  # {https://github.com/niwasawa/twitter-api-ruby-thin-client-wrapper}
10
10
  module TwitterAPI
11
11
 
12
+ # A base client class.
13
+ class BaseClient
14
+
15
+ # Initializes a BaseClient object.
16
+ #
17
+ # @param credentials [Hash] Credentials
18
+ # @return [TwitterAPI::BaseClient]
19
+ def initialize(credentials)
20
+ @credentials = credentials
21
+ end
22
+
23
+ # Calls a Twitter REST API using GET method.
24
+ #
25
+ # @param resource_url [String] Resource URL
26
+ # @param params [Hash] Parameters
27
+ # @return [TwitterAPI::Response]
28
+ def get(resource_url, params)
29
+ headers = {'Authorization' => authorization('GET', resource_url, params)}
30
+ url = resource_url + '?' + URI.encode_www_form(params)
31
+ res = open(url, headers)
32
+ Response.new(res)
33
+ end
34
+
35
+ # Calls a Twitter REST API using POST method.
36
+ #
37
+ # @param resource_url [String] Resource URL
38
+ # @param params [Hash] Parameters
39
+ # @param data [String] Posts data
40
+ # @return [TwitterAPI::Response]
41
+ def post(resource_url, params, data=nil)
42
+ headers = {'Authorization' => authorization('POST', resource_url, params)}
43
+ url = resource_url + '?' + URI.encode_www_form(params)
44
+ uri = URI.parse(url)
45
+ http = Net::HTTP.new(uri.host, uri.port)
46
+ http.use_ssl = true
47
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
48
+ res = http.request_post(url, data, headers)
49
+ Response.new(res)
50
+ end
51
+
52
+ # Calls a Twitter REST API using POST (multipart/form-data) method.
53
+ #
54
+ # @param resource_url [String] Resource URL
55
+ # @param params [Hash] Parameters
56
+ # @param data [Array] Posts Multipart data
57
+ # @return [TwitterAPI::Response]
58
+ def post_multipart(resource_url, params, data={})
59
+ headers = {'Authorization' => authorization('POST', resource_url, params)}
60
+ url = resource_url + '?' + URI.encode_www_form(params)
61
+ uri = URI.parse(url)
62
+ form_data = []
63
+ data.each{|k,v|
64
+ form_data << [k,v]
65
+ }
66
+ http = Net::HTTP.new(uri.host, uri.port)
67
+ http.use_ssl = true
68
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
69
+ req = Net::HTTP::Post.new(uri.request_uri, headers)
70
+ req.set_form(form_data, 'multipart/form-data')
71
+ res = http.start{|h|
72
+ h.request(req)
73
+ }
74
+ Response.new(res)
75
+ end
76
+
77
+ private
78
+
79
+ # Returns string of authorization.
80
+ #
81
+ # @param method [String] A HTTP method
82
+ # @param url [String] A URL
83
+ # @param params [Hash] Parameters
84
+ # @return [String]
85
+ def authorization(method, url, params)
86
+ SimpleOAuth::Header.new(method, url, params, @credentials).to_s
87
+ end
88
+
89
+ end
90
+
12
91
  # A client class.
13
- class Client
92
+ class Client < BaseClient
14
93
 
15
94
  # Initializes a Client object.
16
95
  #
17
96
  # @param credentials [Hash] Credentials
18
97
  # @return [TwitterAPI::Client]
19
98
  def initialize(credentials)
20
- @credentials = credentials
99
+ super
21
100
  end
22
101
 
23
102
  # Calls a Twitter REST API using GET method.
@@ -49,6 +128,31 @@ module TwitterAPI
49
128
  Response.new(res)
50
129
  end
51
130
 
131
+ # Calls a Twitter REST API using POST (multipart/form-data) method.
132
+ #
133
+ # @param resource_url [String] Resource URL
134
+ # @param params [Hash] Parameters
135
+ # @param data [Array] Posts Multipart data
136
+ # @return [TwitterAPI::Response]
137
+ def post_multipart(resource_url, params, data={})
138
+ headers = {'Authorization' => authorization('POST', resource_url, params)}
139
+ url = resource_url + '?' + URI.encode_www_form(params)
140
+ uri = URI.parse(url)
141
+ form_data = []
142
+ data.each{|k,v|
143
+ form_data << [k,v]
144
+ }
145
+ http = Net::HTTP.new(uri.host, uri.port)
146
+ http.use_ssl = true
147
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
148
+ req = Net::HTTP::Post.new(uri.request_uri, headers)
149
+ req.set_form(form_data, 'multipart/form-data')
150
+ res = http.start{|h|
151
+ h.request(req)
152
+ }
153
+ Response.new(res)
154
+ end
155
+
52
156
  # GET geo/id/:place_id
53
157
  # {https://dev.twitter.com/rest/reference/get/geo/id/place_id}
54
158
  #
@@ -99,26 +203,34 @@ module TwitterAPI
99
203
  get(resource_url, params)
100
204
  end
101
205
 
102
- # POST statuses/update
103
- # {https://dev.twitter.com/rest/reference/post/statuses/update}
206
+ # GET users/lookup
207
+ # {https://dev.twitter.com/rest/reference/get/users/lookup}
104
208
  #
105
209
  # @param params [Hash] Parameters
106
210
  # @return [TwitterAPI::Response]
107
- def statuses_update(params)
108
- resource_url = 'https://api.twitter.com/1.1/statuses/update.json'
109
- post(resource_url, params)
211
+ def users_lookup(params)
212
+ resource_url = 'https://api.twitter.com/1.1/users/lookup.json'
213
+ get(resource_url, params)
110
214
  end
111
215
 
112
- private
216
+ # POST media/upload
217
+ # {https://dev.twitter.com/rest/reference/post/media/upload}
218
+ #
219
+ # @param params [Hash] Parameters
220
+ # @return [TwitterAPI::Response]
221
+ def media_upload(params)
222
+ resource_url = 'https://upload.twitter.com/1.1/media/upload.json'
223
+ post_multipart(resource_url, {}, params)
224
+ end
113
225
 
114
- # Returns string of authorization.
226
+ # POST statuses/update
227
+ # {https://dev.twitter.com/rest/reference/post/statuses/update}
115
228
  #
116
- # @param method [String] A HTTP method
117
- # @param url [String] A URL
118
229
  # @param params [Hash] Parameters
119
- # @return [String]
120
- def authorization(method, url, params)
121
- SimpleOAuth::Header.new(method, url, params, @credentials).to_s
230
+ # @return [TwitterAPI::Response]
231
+ def statuses_update(params)
232
+ resource_url = 'https://api.twitter.com/1.1/statuses/update.json'
233
+ post(resource_url, params)
122
234
  end
123
235
 
124
236
  end
@@ -130,7 +242,7 @@ module TwitterAPI
130
242
  #
131
243
  # @param res [StringIO]
132
244
  # @param res [Tempfile]
133
- # @return [TwitterAPI::Client]
245
+ # @return [TwitterAPI::Response]
134
246
  def initialize(res)
135
247
  @res = res
136
248
  @headers = make_headers
@@ -153,7 +265,7 @@ module TwitterAPI
153
265
  end
154
266
 
155
267
  private
156
-
268
+
157
269
  # Returns HTTP headers.
158
270
  #
159
271
  # @return [Net::HTTPHeader]
@@ -1,3 +1,3 @@
1
1
  module TwitterAPI
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twitter_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naoki Iwasawa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-22 00:00:00.000000000 Z
11
+ date: 2017-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: simple_oauth