micro-max-tool 0.0.1
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 +7 -0
- data/httparty-0.24.2/CONTRIBUTING.md +23 -0
- data/httparty-0.24.2/Changelog.md +624 -0
- data/httparty-0.24.2/Gemfile +27 -0
- data/httparty-0.24.2/Guardfile +17 -0
- data/httparty-0.24.2/MIT-LICENSE +20 -0
- data/httparty-0.24.2/README.md +79 -0
- data/httparty-0.24.2/Rakefile +10 -0
- data/httparty-0.24.2/bin/httparty +123 -0
- data/httparty-0.24.2/cucumber.yml +1 -0
- data/httparty-0.24.2/docs/README.md +223 -0
- data/httparty-0.24.2/examples/README.md +90 -0
- data/httparty-0.24.2/examples/aaws.rb +36 -0
- data/httparty-0.24.2/examples/basic.rb +28 -0
- data/httparty-0.24.2/examples/body_stream.rb +14 -0
- data/httparty-0.24.2/examples/crack.rb +19 -0
- data/httparty-0.24.2/examples/custom_parsers.rb +68 -0
- data/httparty-0.24.2/examples/delicious.rb +37 -0
- data/httparty-0.24.2/examples/google.rb +16 -0
- data/httparty-0.24.2/examples/headers_and_user_agents.rb +10 -0
- data/httparty-0.24.2/examples/idn.rb +10 -0
- data/httparty-0.24.2/examples/logging.rb +36 -0
- data/httparty-0.24.2/examples/microsoft_graph.rb +52 -0
- data/httparty-0.24.2/examples/multipart.rb +35 -0
- data/httparty-0.24.2/examples/nokogiri_html_parser.rb +19 -0
- data/httparty-0.24.2/examples/party_foul_mode.rb +90 -0
- data/httparty-0.24.2/examples/peer_cert.rb +9 -0
- data/httparty-0.24.2/examples/rescue_json.rb +17 -0
- data/httparty-0.24.2/examples/rubyurl.rb +14 -0
- data/httparty-0.24.2/examples/stackexchange.rb +24 -0
- data/httparty-0.24.2/examples/stream_download.rb +26 -0
- data/httparty-0.24.2/examples/tripit_sign_in.rb +44 -0
- data/httparty-0.24.2/examples/twitter.rb +31 -0
- data/httparty-0.24.2/examples/whoismyrep.rb +10 -0
- data/httparty-0.24.2/httparty.gemspec +32 -0
- data/httparty-0.24.2/lib/httparty/connection_adapter.rb +237 -0
- data/httparty-0.24.2/lib/httparty/cookie_hash.rb +23 -0
- data/httparty-0.24.2/lib/httparty/decompressor.rb +102 -0
- data/httparty-0.24.2/lib/httparty/exceptions.rb +66 -0
- data/httparty-0.24.2/lib/httparty/hash_conversions.rb +71 -0
- data/httparty-0.24.2/lib/httparty/headers_processor.rb +32 -0
- data/httparty-0.24.2/lib/httparty/logger/apache_formatter.rb +47 -0
- data/httparty-0.24.2/lib/httparty/logger/curl_formatter.rb +93 -0
- data/httparty-0.24.2/lib/httparty/logger/logger.rb +30 -0
- data/httparty-0.24.2/lib/httparty/logger/logstash_formatter.rb +62 -0
- data/httparty-0.24.2/lib/httparty/module_inheritable_attributes.rb +56 -0
- data/httparty-0.24.2/lib/httparty/net_digest_auth.rb +135 -0
- data/httparty-0.24.2/lib/httparty/parser.rb +157 -0
- data/httparty-0.24.2/lib/httparty/request/body.rb +125 -0
- data/httparty-0.24.2/lib/httparty/request/multipart_boundary.rb +13 -0
- data/httparty-0.24.2/lib/httparty/request/streaming_multipart_body.rb +190 -0
- data/httparty-0.24.2/lib/httparty/request.rb +466 -0
- data/httparty-0.24.2/lib/httparty/response/headers.rb +35 -0
- data/httparty-0.24.2/lib/httparty/response.rb +156 -0
- data/httparty-0.24.2/lib/httparty/response_fragment.rb +21 -0
- data/httparty-0.24.2/lib/httparty/text_encoder.rb +72 -0
- data/httparty-0.24.2/lib/httparty/utils.rb +13 -0
- data/httparty-0.24.2/lib/httparty/version.rb +5 -0
- data/httparty-0.24.2/lib/httparty.rb +699 -0
- data/httparty-0.24.2/script/release +42 -0
- data/httparty-0.24.2/website/css/common.css +47 -0
- data/httparty-0.24.2/website/index.html +73 -0
- data/micro-max-tool.gemspec +11 -0
- metadata +102 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
2
|
+
require File.join(dir, 'httparty')
|
|
3
|
+
require 'pp'
|
|
4
|
+
|
|
5
|
+
class ParseAtom
|
|
6
|
+
include HTTParty
|
|
7
|
+
|
|
8
|
+
# Support Atom along with the default parsers: xml, json, etc.
|
|
9
|
+
class Parser::Atom < HTTParty::Parser
|
|
10
|
+
SupportedFormats.merge!({"application/atom+xml" => :atom})
|
|
11
|
+
|
|
12
|
+
protected
|
|
13
|
+
|
|
14
|
+
# perform atom parsing on body
|
|
15
|
+
def atom
|
|
16
|
+
body.to_atom
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
parser Parser::Atom
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class OnlyParseAtom
|
|
24
|
+
include HTTParty
|
|
25
|
+
|
|
26
|
+
# Only support Atom
|
|
27
|
+
class Parser::OnlyAtom < HTTParty::Parser
|
|
28
|
+
SupportedFormats = { "application/atom+xml" => :atom }
|
|
29
|
+
|
|
30
|
+
protected
|
|
31
|
+
|
|
32
|
+
# perform atom parsing on body
|
|
33
|
+
def atom
|
|
34
|
+
body.to_atom
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
parser Parser::OnlyAtom
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
class SkipParsing
|
|
42
|
+
include HTTParty
|
|
43
|
+
|
|
44
|
+
# Parse the response body however you like
|
|
45
|
+
class Parser::Simple < HTTParty::Parser
|
|
46
|
+
def parse
|
|
47
|
+
body
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
parser Parser::Simple
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
class AdHocParsing
|
|
55
|
+
include HTTParty
|
|
56
|
+
parser(
|
|
57
|
+
proc do |body, format|
|
|
58
|
+
case format
|
|
59
|
+
when :json
|
|
60
|
+
body.to_json
|
|
61
|
+
when :xml
|
|
62
|
+
body.to_xml
|
|
63
|
+
else
|
|
64
|
+
body
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
)
|
|
68
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
2
|
+
require File.join(dir, 'httparty')
|
|
3
|
+
require 'pp'
|
|
4
|
+
config = YAML.load(File.read(File.join(ENV['HOME'], '.delicious')))
|
|
5
|
+
|
|
6
|
+
class Delicious
|
|
7
|
+
include HTTParty
|
|
8
|
+
base_uri 'https://api.del.icio.us/v1'
|
|
9
|
+
|
|
10
|
+
def initialize(u, p)
|
|
11
|
+
@auth = { username: u, password: p }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# query params that filter the posts are:
|
|
15
|
+
# tag (optional). Filter by this tag.
|
|
16
|
+
# dt (optional). Filter by this date (CCYY-MM-DDThh:mm:ssZ).
|
|
17
|
+
# url (optional). Filter by this url.
|
|
18
|
+
# ie: posts(query: {tag: 'ruby'})
|
|
19
|
+
def posts(options = {})
|
|
20
|
+
options.merge!({ basic_auth: @auth })
|
|
21
|
+
self.class.get('/posts/get', options)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# query params that filter the posts are:
|
|
25
|
+
# tag (optional). Filter by this tag.
|
|
26
|
+
# count (optional). Number of items to retrieve (Default:15, Maximum:100).
|
|
27
|
+
def recent(options = {})
|
|
28
|
+
options.merge!({ basic_auth: @auth })
|
|
29
|
+
self.class.get('/posts/recent', options)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
delicious = Delicious.new(config['username'], config['password'])
|
|
34
|
+
pp delicious.posts(query: { tag: 'ruby' })
|
|
35
|
+
pp delicious.recent
|
|
36
|
+
|
|
37
|
+
delicious.recent['posts']['post'].each { |post| puts post['href'] }
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
2
|
+
require File.join(dir, 'httparty')
|
|
3
|
+
require 'pp'
|
|
4
|
+
|
|
5
|
+
class Google
|
|
6
|
+
include HTTParty
|
|
7
|
+
format :html
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# google.com redirects to www.google.com so this is live test for redirection
|
|
11
|
+
pp Google.get('http://google.com')
|
|
12
|
+
|
|
13
|
+
puts '', '*' * 70, ''
|
|
14
|
+
|
|
15
|
+
# check that ssl is requesting right
|
|
16
|
+
pp Google.get('https://www.google.com')
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# To send custom user agents to identify your application to a web service (or mask as a specific browser for testing), send "User-Agent" as a hash to headers as shown below.
|
|
2
|
+
|
|
3
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
4
|
+
require File.join(dir, 'httparty')
|
|
5
|
+
require 'pp'
|
|
6
|
+
|
|
7
|
+
response = HTTParty.get('http://example.com', {
|
|
8
|
+
headers: {"User-Agent" => "Httparty"},
|
|
9
|
+
debug_output: STDOUT, # To show that User-Agent is Httparty
|
|
10
|
+
})
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
2
|
+
require File.join(dir, 'httparty')
|
|
3
|
+
require 'logger'
|
|
4
|
+
require 'pp'
|
|
5
|
+
|
|
6
|
+
my_logger = Logger.new STDOUT
|
|
7
|
+
|
|
8
|
+
my_logger.info "Logging can be used on the main HTTParty class. It logs redirects too."
|
|
9
|
+
HTTParty.get "http://google.com", logger: my_logger
|
|
10
|
+
|
|
11
|
+
my_logger.info '*' * 70
|
|
12
|
+
|
|
13
|
+
my_logger.info "It can be used also on a custom class."
|
|
14
|
+
|
|
15
|
+
class Google
|
|
16
|
+
include HTTParty
|
|
17
|
+
logger ::Logger.new STDOUT
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
Google.get "http://google.com"
|
|
21
|
+
|
|
22
|
+
my_logger.info '*' * 70
|
|
23
|
+
|
|
24
|
+
my_logger.info "The default formatter is :apache. The :curl formatter can also be used."
|
|
25
|
+
my_logger.info "You can tell which method to call on the logger too. It is info by default."
|
|
26
|
+
HTTParty.get "http://google.com", logger: my_logger, log_level: :debug, log_format: :curl
|
|
27
|
+
|
|
28
|
+
my_logger.info '*' * 70
|
|
29
|
+
|
|
30
|
+
my_logger.info "These configs are also available on custom classes."
|
|
31
|
+
class Google
|
|
32
|
+
include HTTParty
|
|
33
|
+
logger ::Logger.new(STDOUT), :debug, :curl
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
Google.get "http://google.com"
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
require 'httparty'
|
|
2
|
+
|
|
3
|
+
class MicrosoftGraph
|
|
4
|
+
MS_BASE_URL = "https://login.microsoftonline.com".freeze
|
|
5
|
+
TOKEN_REQUEST_PATH = "oauth2/v2.0/token".freeze
|
|
6
|
+
|
|
7
|
+
def initialize(tenant_id)
|
|
8
|
+
@tenant_id = tenant_id
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Make a request to the Microsoft Graph API, for instance https://graph.microsoft.com/v1.0/users
|
|
12
|
+
def request(url)
|
|
13
|
+
return false unless (token = bearer_token)
|
|
14
|
+
|
|
15
|
+
response = HTTParty.get(
|
|
16
|
+
url,
|
|
17
|
+
headers: {
|
|
18
|
+
Authorization: "Bearer #{token}"
|
|
19
|
+
}
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
return false unless response.code == 200
|
|
23
|
+
|
|
24
|
+
return JSON.parse(response.body)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
# A post to the Microsoft Graph to get a bearer token for the specified tenant. In this example
|
|
30
|
+
# our Rails application has already been given permission to request these tokens by the admin of
|
|
31
|
+
# the specified tenant_id.
|
|
32
|
+
#
|
|
33
|
+
# See here for more information https://developer.microsoft.com/en-us/graph/docs/concepts/auth_v2_service
|
|
34
|
+
#
|
|
35
|
+
# This request also makes use of the multipart/form-data post body.
|
|
36
|
+
def bearer_token
|
|
37
|
+
response = HTTParty.post(
|
|
38
|
+
"#{MS_BASE_URL}/#{@tenant_id}/#{TOKEN_REQUEST_PATH}",
|
|
39
|
+
multipart: true,
|
|
40
|
+
body: {
|
|
41
|
+
client_id: Rails.application.credentials[Rails.env.to_sym][:microsoft_client_id],
|
|
42
|
+
client_secret: Rails.application.credentials[Rails.env.to_sym][:microsoft_client_secret],
|
|
43
|
+
scope: 'https://graph.microsoft.com/.default',
|
|
44
|
+
grant_type: 'client_credentials'
|
|
45
|
+
}
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
return false unless response.code == 200
|
|
49
|
+
|
|
50
|
+
JSON.parse(response.body)['access_token']
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# If you are uploading file in params, multipart will used as content-type automatically
|
|
2
|
+
|
|
3
|
+
HTTParty.post(
|
|
4
|
+
'http://localhost:3000/user',
|
|
5
|
+
body: {
|
|
6
|
+
name: 'Foo Bar',
|
|
7
|
+
email: 'example@email.com',
|
|
8
|
+
avatar: File.open('/full/path/to/avatar.jpg')
|
|
9
|
+
}
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# However, you can force it yourself
|
|
14
|
+
|
|
15
|
+
HTTParty.post(
|
|
16
|
+
'http://localhost:3000/user',
|
|
17
|
+
multipart: true,
|
|
18
|
+
body: {
|
|
19
|
+
name: 'Foo Bar',
|
|
20
|
+
email: 'example@email.com'
|
|
21
|
+
}
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# For large file uploads, use stream_body: true to reduce memory usage.
|
|
26
|
+
# Instead of loading the entire file into memory, HTTParty will stream it in chunks.
|
|
27
|
+
# Note: Some servers may not handle streaming uploads correctly.
|
|
28
|
+
|
|
29
|
+
HTTParty.post(
|
|
30
|
+
'http://localhost:3000/upload',
|
|
31
|
+
body: {
|
|
32
|
+
document: File.open('/full/path/to/large_file.zip')
|
|
33
|
+
},
|
|
34
|
+
stream_body: true
|
|
35
|
+
)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'nokogiri'
|
|
3
|
+
|
|
4
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
5
|
+
require File.join(dir, 'httparty')
|
|
6
|
+
require 'pp'
|
|
7
|
+
|
|
8
|
+
class HtmlParserIncluded < HTTParty::Parser
|
|
9
|
+
def html
|
|
10
|
+
Nokogiri::HTML(body)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class Page
|
|
15
|
+
include HTTParty
|
|
16
|
+
parser HtmlParserIncluded
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
pp Page.get('http://www.google.com')
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
require 'httparty'
|
|
2
|
+
|
|
3
|
+
class APIClient
|
|
4
|
+
include HTTParty
|
|
5
|
+
base_uri 'api.example.com'
|
|
6
|
+
|
|
7
|
+
def self.fetch_user(id)
|
|
8
|
+
begin
|
|
9
|
+
get("/users/#{id}", foul: true)
|
|
10
|
+
rescue HTTParty::NetworkError => e
|
|
11
|
+
handle_network_error(e)
|
|
12
|
+
rescue HTTParty::ResponseError => e
|
|
13
|
+
handle_api_error(e)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def self.handle_network_error(error)
|
|
20
|
+
case error.cause
|
|
21
|
+
when Errno::ECONNREFUSED
|
|
22
|
+
{
|
|
23
|
+
error: :server_down,
|
|
24
|
+
message: "The API server appears to be down",
|
|
25
|
+
details: error.message
|
|
26
|
+
}
|
|
27
|
+
when Net::OpenTimeout, Timeout::Error
|
|
28
|
+
{
|
|
29
|
+
error: :timeout,
|
|
30
|
+
message: "The request timed out",
|
|
31
|
+
details: error.message
|
|
32
|
+
}
|
|
33
|
+
when SocketError
|
|
34
|
+
{
|
|
35
|
+
error: :network_error,
|
|
36
|
+
message: "Could not connect to the API server",
|
|
37
|
+
details: error.message
|
|
38
|
+
}
|
|
39
|
+
when OpenSSL::SSL::SSLError
|
|
40
|
+
{
|
|
41
|
+
error: :ssl_error,
|
|
42
|
+
message: "SSL certificate verification failed",
|
|
43
|
+
details: error.message
|
|
44
|
+
}
|
|
45
|
+
else
|
|
46
|
+
{
|
|
47
|
+
error: :unknown_network_error,
|
|
48
|
+
message: "An unexpected network error occurred",
|
|
49
|
+
details: error.message
|
|
50
|
+
}
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def self.handle_api_error(error)
|
|
55
|
+
{
|
|
56
|
+
error: :api_error,
|
|
57
|
+
message: "API returned error #{error.response.code}",
|
|
58
|
+
details: error.response.body
|
|
59
|
+
}
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Example usage:
|
|
64
|
+
|
|
65
|
+
# 1. When server is down
|
|
66
|
+
result = APIClient.fetch_user(123)
|
|
67
|
+
puts "Server down example:"
|
|
68
|
+
puts result.inspect
|
|
69
|
+
puts
|
|
70
|
+
|
|
71
|
+
# 2. When request times out
|
|
72
|
+
result = APIClient.fetch_user(456)
|
|
73
|
+
puts "Timeout example:"
|
|
74
|
+
puts result.inspect
|
|
75
|
+
puts
|
|
76
|
+
|
|
77
|
+
# 3. When SSL error occurs
|
|
78
|
+
result = APIClient.fetch_user(789)
|
|
79
|
+
puts "SSL error example:"
|
|
80
|
+
puts result.inspect
|
|
81
|
+
puts
|
|
82
|
+
|
|
83
|
+
# 4. Simple example without a wrapper class
|
|
84
|
+
begin
|
|
85
|
+
HTTParty.get('https://api.example.com/users', foul: true)
|
|
86
|
+
rescue HTTParty::Foul => e
|
|
87
|
+
puts "Direct usage example:"
|
|
88
|
+
puts "Error type: #{e.cause.class}"
|
|
89
|
+
puts "Error message: #{e.message}"
|
|
90
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
2
|
+
require File.join(dir, 'httparty')
|
|
3
|
+
|
|
4
|
+
peer_cert = nil
|
|
5
|
+
HTTParty.get("https://www.example.com") do |fragment|
|
|
6
|
+
peer_cert ||= fragment.connection.peer_cert
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
puts "The server's certificate expires #{peer_cert.not_after}"
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
2
|
+
require File.join(dir, 'httparty')
|
|
3
|
+
|
|
4
|
+
# Take note of the "; 1" at the end of the following line. It's required only if
|
|
5
|
+
# running this in IRB, because IRB will try to inspect the variable named
|
|
6
|
+
# "request", triggering the exception.
|
|
7
|
+
request = HTTParty.get 'https://rubygems.org/api/v1/versions/doesnotexist.json' ; 1
|
|
8
|
+
|
|
9
|
+
# Check an exception due to parsing the response
|
|
10
|
+
# because HTTParty evaluate the response lazily
|
|
11
|
+
begin
|
|
12
|
+
request.inspect
|
|
13
|
+
# This would also suffice by forcing the request to be parsed:
|
|
14
|
+
# request.parsed_response
|
|
15
|
+
rescue => e
|
|
16
|
+
puts "Rescued #{e.inspect}"
|
|
17
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
2
|
+
require File.join(dir, 'httparty')
|
|
3
|
+
require 'pp'
|
|
4
|
+
|
|
5
|
+
class Rubyurl
|
|
6
|
+
include HTTParty
|
|
7
|
+
base_uri 'rubyurl.com'
|
|
8
|
+
|
|
9
|
+
def self.shorten(website_url)
|
|
10
|
+
post('/api/links.json', query: { link: { website_url: website_url } })
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
pp Rubyurl.shorten('http://istwitterdown.com/')
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
2
|
+
require File.join(dir, 'httparty')
|
|
3
|
+
require 'pp'
|
|
4
|
+
|
|
5
|
+
class StackExchange
|
|
6
|
+
include HTTParty
|
|
7
|
+
base_uri 'api.stackexchange.com'
|
|
8
|
+
|
|
9
|
+
def initialize(service, page)
|
|
10
|
+
@options = { query: { site: service, page: page } }
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def questions
|
|
14
|
+
self.class.get("/2.2/questions", @options)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def users
|
|
18
|
+
self.class.get("/2.2/users", @options)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
stack_exchange = StackExchange.new("stackoverflow", 1)
|
|
23
|
+
pp stack_exchange.questions
|
|
24
|
+
pp stack_exchange.users
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
2
|
+
require File.join(dir, 'httparty')
|
|
3
|
+
require 'pp'
|
|
4
|
+
|
|
5
|
+
# download file linux-4.6.4.tar.xz without using the memory
|
|
6
|
+
response = nil
|
|
7
|
+
filename = "linux-4.6.4.tar.xz"
|
|
8
|
+
url = "https://cdn.kernel.org/pub/linux/kernel/v4.x/#{filename}"
|
|
9
|
+
|
|
10
|
+
File.open(filename, "w") do |file|
|
|
11
|
+
response = HTTParty.get(url, stream_body: true) do |fragment|
|
|
12
|
+
if [301, 302].include?(fragment.code)
|
|
13
|
+
print "skip writing for redirect"
|
|
14
|
+
elsif fragment.code == 200
|
|
15
|
+
print "."
|
|
16
|
+
file.write(fragment)
|
|
17
|
+
else
|
|
18
|
+
raise StandardError, "Non-success status code while streaming #{fragment.code}"
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
puts
|
|
23
|
+
|
|
24
|
+
pp "Success: #{response.success?}"
|
|
25
|
+
pp File.stat(filename).inspect
|
|
26
|
+
File.unlink(filename)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
2
|
+
require File.join(dir, 'httparty')
|
|
3
|
+
|
|
4
|
+
class TripIt
|
|
5
|
+
include HTTParty
|
|
6
|
+
base_uri 'https://www.tripit.com'
|
|
7
|
+
debug_output
|
|
8
|
+
|
|
9
|
+
def initialize(email, password)
|
|
10
|
+
@email = email
|
|
11
|
+
get_response = self.class.get('/account/login')
|
|
12
|
+
get_response_cookie = parse_cookie(get_response.headers['Set-Cookie'])
|
|
13
|
+
|
|
14
|
+
post_response = self.class.post(
|
|
15
|
+
'/account/login',
|
|
16
|
+
body: {
|
|
17
|
+
login_email_address: email,
|
|
18
|
+
login_password: password
|
|
19
|
+
},
|
|
20
|
+
headers: {'Cookie' => get_response_cookie.to_cookie_string }
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
@cookie = parse_cookie(post_response.headers['Set-Cookie'])
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def account_settings
|
|
27
|
+
self.class.get('/account/edit', headers: { 'Cookie' => @cookie.to_cookie_string })
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def logged_in?
|
|
31
|
+
account_settings.include? "You're logged in as #{@email}"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def parse_cookie(resp)
|
|
37
|
+
cookie_hash = CookieHash.new
|
|
38
|
+
resp.get_fields('Set-Cookie').each { |c| cookie_hash.add_cookies(c) }
|
|
39
|
+
cookie_hash
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
tripit = TripIt.new('email', 'password')
|
|
44
|
+
puts "Logged in: #{tripit.logged_in?}"
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
2
|
+
require File.join(dir, 'httparty')
|
|
3
|
+
require 'pp'
|
|
4
|
+
config = YAML.load(File.read(File.join(ENV['HOME'], '.twitter')))
|
|
5
|
+
|
|
6
|
+
class Twitter
|
|
7
|
+
include HTTParty
|
|
8
|
+
base_uri 'twitter.com'
|
|
9
|
+
|
|
10
|
+
def initialize(u, p)
|
|
11
|
+
@auth = {username: u, password: p}
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# which can be :friends, :user or :public
|
|
15
|
+
# options[:query] can be things like since, since_id, count, etc.
|
|
16
|
+
def timeline(which = :friends, options = {})
|
|
17
|
+
options.merge!({ basic_auth: @auth })
|
|
18
|
+
self.class.get("/statuses/#{which}_timeline.json", options)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def post(text)
|
|
22
|
+
options = { query: { status: text }, basic_auth: @auth }
|
|
23
|
+
self.class.post('/statuses/update.json', options)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
twitter = Twitter.new(config['email'], config['password'])
|
|
28
|
+
pp twitter.timeline
|
|
29
|
+
# pp twitter.timeline(:friends, query: {since_id: 868482746})
|
|
30
|
+
# pp twitter.timeline(:friends, query: 'since_id=868482746')
|
|
31
|
+
# pp twitter.post('this is a test of 0.2.0')
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
2
|
+
require File.join(dir, 'httparty')
|
|
3
|
+
require 'pp'
|
|
4
|
+
|
|
5
|
+
class Rep
|
|
6
|
+
include HTTParty
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
pp Rep.get('http://whoismyrepresentative.com/getall_mems.php?zip=46544')
|
|
10
|
+
pp Rep.get('http://whoismyrepresentative.com/getall_mems.php', query: { zip: 46544 })
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
$LOAD_PATH.push File.expand_path("../lib", __FILE__)
|
|
3
|
+
require "httparty/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |s|
|
|
6
|
+
s.name = "httparty"
|
|
7
|
+
s.version = HTTParty::VERSION
|
|
8
|
+
s.platform = Gem::Platform::RUBY
|
|
9
|
+
s.licenses = ['MIT']
|
|
10
|
+
s.authors = ["John Nunemaker", "Sandro Turriate"]
|
|
11
|
+
s.email = ["nunemaker@gmail.com"]
|
|
12
|
+
s.homepage = "https://github.com/jnunemaker/httparty"
|
|
13
|
+
s.summary = 'Makes http fun! Also, makes consuming restful web services dead easy.'
|
|
14
|
+
s.description = 'Makes http fun! Also, makes consuming restful web services dead easy.'
|
|
15
|
+
s.metadata["changelog_uri"] = 'https://github.com/jnunemaker/httparty/releases'
|
|
16
|
+
|
|
17
|
+
s.required_ruby_version = '>= 2.7.0'
|
|
18
|
+
|
|
19
|
+
s.add_dependency 'csv'
|
|
20
|
+
s.add_dependency 'multi_xml', ">= 0.5.2"
|
|
21
|
+
s.add_dependency 'mini_mime', ">= 1.0.0"
|
|
22
|
+
|
|
23
|
+
# If this line is removed, all hard partying will cease.
|
|
24
|
+
s.post_install_message = "When you HTTParty, you must party hard!"
|
|
25
|
+
|
|
26
|
+
all_files = `git ls-files`.split("\n")
|
|
27
|
+
test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
|
28
|
+
|
|
29
|
+
s.files = all_files - test_files
|
|
30
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
|
31
|
+
s.require_paths = ["lib"]
|
|
32
|
+
end
|