pocket 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +89 -0
  3. data/Rakefile +8 -0
  4. data/lib/pocket.rb +68 -0
  5. metadata +49 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 Ryan Fitzgerald
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # Pocket
2
+
3
+ A curl gem that doesn't suck.
4
+
5
+ ### Installation
6
+
7
+ In your Gemfile, add this line:
8
+
9
+ gem "pocket"
10
+
11
+
12
+ ### Options & Defaults
13
+
14
+ Here are the possible options. Whatever you pass in will merge with and overwrite these defaults. The :headers options will merge as well, so if you don't want those default headers, you'll
15
+ need to pass in a :headers option that overwrites (e.g. 'Content-Type') with nil.
16
+
17
+ # defaults
18
+ _options = {
19
+ :method => :get,
20
+ :username => nil,
21
+ :password => nil,
22
+ :use_ssl => false,
23
+ :data => nil,
24
+ :headers => {
25
+ "Content-Type" => "application/json",
26
+ "Accept" => "application/json",
27
+ }
28
+ }
29
+
30
+ ### Example Usage
31
+
32
+ (testing off http://button.herokuapp.com)
33
+
34
+ Simple get:
35
+
36
+ response = Pocket.make_request("http://button.herokuapp.com/stitches")
37
+
38
+ Simple post:
39
+
40
+ response = Pocket.make_request("http://button.herokuapp.com/stitches", {
41
+ :method => :post,
42
+ :data => {
43
+ :stitch => {
44
+ :thread_color => "blue",
45
+ :length => 2
46
+ }
47
+ }
48
+ })
49
+
50
+ Simple put:
51
+
52
+ response = Pocket.make_request("http://button.herokuapp.com/stitches/" + stitch["id"].to_s, {
53
+ :method => :put,
54
+ :data => {
55
+ :stitch => {
56
+ :thread_color => "blue",
57
+ :length => 3
58
+ }
59
+ }
60
+ })
61
+
62
+
63
+ Simple delete:
64
+
65
+ response = Pocket.make_request("http://button.herokuapp.com/stitches/" + stitch["id"].to_s, {
66
+ :method => :delete,
67
+ })
68
+
69
+ Get with authentication:
70
+
71
+ response = Pocket.make_request("http://button.herokuapp.com/needles", {
72
+ :username => "username",
73
+ :password => "password"
74
+ })
75
+
76
+ Post with authentication:
77
+
78
+ response = Pocket.make_request("http://button.herokuapp.com/needles", {
79
+ :method => :post,
80
+ :data => {
81
+ :needle => {
82
+ :sharpness => 7,
83
+ :length => 2
84
+ }
85
+ },
86
+ :username => "username",
87
+ :password => "password"
88
+ })
89
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
data/lib/pocket.rb ADDED
@@ -0,0 +1,68 @@
1
+ require "net/http"
2
+ require 'uri'
3
+ require 'json'
4
+
5
+ class Pocket
6
+
7
+ def self.make_request(url, options = {})
8
+
9
+ # defaults
10
+ _options = {
11
+ :method => :get,
12
+ :username => nil,
13
+ :password => nil,
14
+ :use_ssl => false,
15
+ :data => nil,
16
+ :headers => {
17
+ "Content-Type" => "application/json",
18
+ "Accept" => "application/json",
19
+ }
20
+ }
21
+
22
+ # merge passed in options with default options
23
+ _options = _options.merge(options)
24
+
25
+ # consolidate needed vars
26
+ _url = URI.parse(url)
27
+ _method = _options[:method]
28
+ _username = _options[:username]
29
+ _password = _options[:password]
30
+ _use_ssl = _options[:use_ssl]
31
+ _data = _options[:data]
32
+ _headers = _options[:headers]
33
+ _path = _url.path ? _url.path : ""
34
+ _query = _url.query ? "?" + _url.query : ""
35
+ _api_query = _path + _query
36
+
37
+ # create http object
38
+ http = Net::HTTP.new(_url.host, _url.port)
39
+ http.use_ssl = _use_ssl
40
+
41
+ # create request
42
+ if _method == :get
43
+ request = Net::HTTP::Get.new(_api_query, _headers)
44
+ elsif _method == :post
45
+ request = Net::HTTP::Post.new(_api_query, _headers)
46
+ elsif _method == :put
47
+ request = Net::HTTP::Put.new(_api_query, _headers)
48
+ elsif _method == :delete
49
+ request = Net::HTTP::Delete.new(_api_query, _headers)
50
+ else
51
+ request = Net::HTTP::Get.new(_api_query, _headers)
52
+ end
53
+
54
+ # add data (if any)
55
+ request.body = _data ? _data.to_json : nil
56
+
57
+ # add authentication (if present)
58
+ if _username && _password
59
+ request.basic_auth _username, _password
60
+ end
61
+
62
+ # make request
63
+ response = http.request(request)
64
+
65
+ # return response
66
+ response
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pocket
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adam Kirk
9
+ - Mysterious Trousers, LLC
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2011-11-24 00:00:00.000000000Z
14
+ dependencies: []
15
+ description: curl done right
16
+ email: adam@mysterioustrousers.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/pocket.rb
22
+ - MIT-LICENSE
23
+ - Rakefile
24
+ - README.md
25
+ homepage: https://github.com/mysterioustrousers/Pocket
26
+ licenses: []
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 1.8.10
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: curl done right
49
+ test_files: []