openphoto-ruby 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/Readme.markdown +36 -0
- data/examples.rb +24 -0
- data/lib/openphoto-ruby/version.rb +5 -0
- data/lib/openphoto-ruby.rb +68 -0
- data/openphoto-ruby.gemspec +25 -0
- data/spec/logo.png +0 -0
- data/spec/openphoto_spec.rb +71 -0
- data/spec/settings.yml.example +9 -0
- metadata +113 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/Readme.markdown
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
Open Photo API / Ruby Library
|
2
|
+
=======================
|
3
|
+
#### OpenPhoto, a photo service for the masses
|
4
|
+
|
5
|
+
----------------------------------------
|
6
|
+
|
7
|
+
<a name="install"></a>
|
8
|
+
### Installation
|
9
|
+
gem install openphoto-ruby
|
10
|
+
|
11
|
+
<a name="ruby"></a>
|
12
|
+
### How to use the library
|
13
|
+
|
14
|
+
To use the library you need to first require 'openphoto-ruby', retrieve your OAuth credentials, then instantiate an instance of the client and start making calls.
|
15
|
+
|
16
|
+
require 'rubygems'
|
17
|
+
require 'openphoto-ruby'
|
18
|
+
|
19
|
+
@client = Openphoto::Client.new(@site, @consumer_key, @consumer_secret, @access_token, @access_token_secret)
|
20
|
+
photo = @client.connect(:post, "/photo/upload.json", {"photo"=>"/tmp/photo.png", "permission"=>"1", "title"=>"test upload"})
|
21
|
+
puts "#{photo.message} #{@site}/photo/#{photo.result["id"]}/view"
|
22
|
+
|
23
|
+
----------------------------------------
|
24
|
+
|
25
|
+
<a name="credentials"></a>
|
26
|
+
#### Getting your credentials
|
27
|
+
|
28
|
+
You can get your credentials by clicking on the arrow next to your email address once you're logged into your site and then clicking on settings.
|
29
|
+
If you don't have any credentials then you can create one for yourself by going to `/v1/oauth/flow`.
|
30
|
+
Once completed go back to the settings page and you should see the credential you just created
|
31
|
+
|
32
|
+
----------------------------------------
|
33
|
+
<a name="openphoto"></a>
|
34
|
+
#### OpenPhoto installation and API documentation
|
35
|
+
|
36
|
+
You can find more information at the main <a href="http://theopenphotoproject.org/documentation">OpenPhoto documentation page</a>.
|
data/examples.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'openphoto-ruby'
|
3
|
+
|
4
|
+
#load our credentials
|
5
|
+
options = YAML.load_file("spec/settings.yml")
|
6
|
+
@consumer_key = options["oauth"]["consumer_key"]
|
7
|
+
@consumer_secret = options["oauth"]["consumer_secret"]
|
8
|
+
@access_token = options["oauth"]["access_token"]
|
9
|
+
@access_token_secret = options["oauth"]["access_token_secret"]
|
10
|
+
@site = options["oauth"]["site"]
|
11
|
+
|
12
|
+
#create a client
|
13
|
+
@client = Openphoto::Client.new(@site, @consumer_key, @consumer_secret, @access_token, @access_token_secret)
|
14
|
+
|
15
|
+
#connect to GetHelloWorld
|
16
|
+
#http://theopenphotoproject.org/documentation/api/GetHelloWorld
|
17
|
+
puts @client.connect(:get, "/hello.json").message
|
18
|
+
|
19
|
+
#upload a photo
|
20
|
+
#http://theopenphotoproject.org/documentation/api/PostPhotoUpload
|
21
|
+
puts "uploading spec/logo.png..."
|
22
|
+
photo = @client.connect(:post, "/photo/upload.json", {"photo"=>"spec/logo.png", "permission"=>"1", "title"=>"test upload"})
|
23
|
+
puts "#{photo.message} #{@site}/photo/#{photo.result["id"]}/view"
|
24
|
+
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require "openphoto-ruby/version"
|
2
|
+
|
3
|
+
module Openphoto
|
4
|
+
|
5
|
+
class Client
|
6
|
+
|
7
|
+
require 'oauth'
|
8
|
+
require 'json'
|
9
|
+
require 'pp'
|
10
|
+
require 'base64'
|
11
|
+
|
12
|
+
attr_accessor :hostname, :consumer_key, :consumer_secret, :access_token, :access_token_secret
|
13
|
+
|
14
|
+
def initialize(hostname, consumer_key, consumer_secret, access_token, access_token_secret)
|
15
|
+
@hostname = hostname
|
16
|
+
@consumer_key = consumer_key
|
17
|
+
@consumer_secret = consumer_secret
|
18
|
+
@access_token = access_token
|
19
|
+
@access_token_secret = access_token_secret
|
20
|
+
end
|
21
|
+
|
22
|
+
def connect(method,path,params={})
|
23
|
+
params["photo"] = Base64.encode64(File.read(params["photo"])) if params.keys.index("photo")
|
24
|
+
access_token = prepare_access_token
|
25
|
+
response = access_token.request(method.to_sym, path, params)
|
26
|
+
response.extend Openphoto::Response
|
27
|
+
return response
|
28
|
+
end
|
29
|
+
|
30
|
+
# Exchange your oauth_token and oauth_token_secret for an AccessToken instance.
|
31
|
+
def prepare_access_token
|
32
|
+
consumer = OAuth::Consumer.new(@consumer_key, @consumer_secret,
|
33
|
+
{ :site => @hostname,
|
34
|
+
:scheme => :header,
|
35
|
+
:access_token_path=>"/v1/oauth/token/access",
|
36
|
+
:authorize_path => "/v1/oauth/authorize",
|
37
|
+
:request_token_path => "/v1/oauth/token/request"
|
38
|
+
})
|
39
|
+
|
40
|
+
access_token = OAuth::AccessToken.from_hash(consumer, :oauth_token => @access_token, :oauth_token_secret => @access_token_secret)
|
41
|
+
return access_token
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
module Response
|
47
|
+
#adds OpenPhoto related functions to Net::HTTPResponse
|
48
|
+
|
49
|
+
def json
|
50
|
+
JSON.parse(response.body)
|
51
|
+
end
|
52
|
+
|
53
|
+
def result
|
54
|
+
json["result"]
|
55
|
+
end
|
56
|
+
|
57
|
+
def message
|
58
|
+
json["message"]
|
59
|
+
end
|
60
|
+
|
61
|
+
def code
|
62
|
+
json["code"]
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "openphoto-ruby/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "openphoto-ruby"
|
7
|
+
s.version = Openphoto::Ruby::VERSION
|
8
|
+
s.authors = ["Brian Levine"]
|
9
|
+
s.email = ["beans@beanserver.net"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = "A ruby wrapper to the OpenPhoto API"
|
12
|
+
s.description = "A ruby wrapper to the OpenPhoto API"
|
13
|
+
|
14
|
+
s.rubyforge_project = "openphoto-ruby"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "rspec", "~> 2.6"
|
22
|
+
s.add_dependency "oauth", "~> 0.4.5"
|
23
|
+
s.add_dependency "json"
|
24
|
+
|
25
|
+
end
|
data/spec/logo.png
ADDED
Binary file
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'openphoto-ruby'
|
2
|
+
|
3
|
+
describe Openphoto do
|
4
|
+
before(:each) do
|
5
|
+
options = YAML.load_file("spec/settings.yml")
|
6
|
+
@consumer_key = options["oauth"]["consumer_key"]
|
7
|
+
@consumer_secret = options["oauth"]["consumer_secret"]
|
8
|
+
@access_token = options["oauth"]["access_token"]
|
9
|
+
@access_token_secret = options["oauth"]["access_token_secret"]
|
10
|
+
@site = options["oauth"]["site"]
|
11
|
+
@client = Openphoto::Client.new(@site, @consumer_key, @consumer_secret, @access_token, @access_token_secret)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "says hello world" do
|
15
|
+
out=@client.connect(:get, "/hello.json")
|
16
|
+
out.message.should=="Hello, world!"
|
17
|
+
out.code.should==200
|
18
|
+
end
|
19
|
+
|
20
|
+
it "says hello world with oauth" do
|
21
|
+
out=@client.connect(:get, "/hello.json", {"auth"=>"true"})
|
22
|
+
out.message.should=="Hello, world!"
|
23
|
+
out.code.should==200
|
24
|
+
|
25
|
+
@client = Openphoto::Client.new(@site, "bad credential", @consumer_secret, @access_token, @access_token_secret)
|
26
|
+
out=@client.connect(:get, "/hello.json?auth=true")
|
27
|
+
out.code.should==403
|
28
|
+
end
|
29
|
+
|
30
|
+
context "photos context" do
|
31
|
+
|
32
|
+
it "uploads a photo" do
|
33
|
+
|
34
|
+
out=@client.connect(:post, "/photo/upload.json", {"photo"=>"spec/logo.png", "permission"=>"1", "title"=>"rspec upload"})
|
35
|
+
out.code.should==202
|
36
|
+
out.message.should match("uploaded successfully")
|
37
|
+
$upload_id = out.result["id"]
|
38
|
+
end
|
39
|
+
|
40
|
+
it "views a photo" do
|
41
|
+
out=@client.connect(:get, "/photo/#{$upload_id}/view.json")
|
42
|
+
out.result["title"].should match("rspec upload")
|
43
|
+
out.code.should==200
|
44
|
+
end
|
45
|
+
|
46
|
+
it "adds an action" do
|
47
|
+
out=@client.connect(:post, "/action/photo/#{$upload_id}/create.json", {"email"=>"test@openphoto.me", "type"=>"comment", "value"=>"hi there"})
|
48
|
+
out.code.should==200
|
49
|
+
out.code.should_not==403
|
50
|
+
puts out.message
|
51
|
+
puts out.result
|
52
|
+
$comment_id = out.result["id"]
|
53
|
+
end
|
54
|
+
|
55
|
+
it "deletes an action" do
|
56
|
+
out=@client.connect(:post, "/action/photo/#{$comment_id}/delete.json")
|
57
|
+
out.code.should==200
|
58
|
+
out.code.should_not==403
|
59
|
+
end
|
60
|
+
|
61
|
+
it "deletes a photo" do
|
62
|
+
out=@client.connect(:post, "/photo/#{$upload_id}/delete.json")
|
63
|
+
out.code.should==200
|
64
|
+
out.code.should_not==403
|
65
|
+
out.message.should match("Photo deleted successfully")
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
#Getting your credentials
|
2
|
+
#You can get your credentials by clicking on the arrow next to your email address once you're logged into your site and #then clicking on settings. If you don't have any credentials then you can create one for yourself by going to #/v1/oauth/flow. Once completed go back to the settings page and you should see the credential you just created
|
3
|
+
|
4
|
+
oauth:
|
5
|
+
consumer_key: 3e35bbca3c5973efxxxxxxxxxx
|
6
|
+
consumer_secret: 3179xxxxx
|
7
|
+
access_token: 7158277d9aa5xxxxxxxxxxx
|
8
|
+
access_token_secret: d0f9xxxxx
|
9
|
+
site: http://www.openphoto.me
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: openphoto-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Brian Levine
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-12-21 00:00:00 -08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 2
|
29
|
+
- 6
|
30
|
+
version: "2.6"
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: oauth
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
segments:
|
41
|
+
- 0
|
42
|
+
- 4
|
43
|
+
- 5
|
44
|
+
version: 0.4.5
|
45
|
+
type: :runtime
|
46
|
+
version_requirements: *id002
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: json
|
49
|
+
prerelease: false
|
50
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
type: :runtime
|
58
|
+
version_requirements: *id003
|
59
|
+
description: A ruby wrapper to the OpenPhoto API
|
60
|
+
email:
|
61
|
+
- beans@beanserver.net
|
62
|
+
executables: []
|
63
|
+
|
64
|
+
extensions: []
|
65
|
+
|
66
|
+
extra_rdoc_files: []
|
67
|
+
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- Rakefile
|
72
|
+
- Readme.markdown
|
73
|
+
- examples.rb
|
74
|
+
- lib/openphoto-ruby.rb
|
75
|
+
- lib/openphoto-ruby/version.rb
|
76
|
+
- openphoto-ruby.gemspec
|
77
|
+
- spec/logo.png
|
78
|
+
- spec/openphoto_spec.rb
|
79
|
+
- spec/settings.yml.example
|
80
|
+
has_rdoc: true
|
81
|
+
homepage: ""
|
82
|
+
licenses: []
|
83
|
+
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
version: "0"
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
segments:
|
101
|
+
- 0
|
102
|
+
version: "0"
|
103
|
+
requirements: []
|
104
|
+
|
105
|
+
rubyforge_project: openphoto-ruby
|
106
|
+
rubygems_version: 1.3.6
|
107
|
+
signing_key:
|
108
|
+
specification_version: 3
|
109
|
+
summary: A ruby wrapper to the OpenPhoto API
|
110
|
+
test_files:
|
111
|
+
- spec/logo.png
|
112
|
+
- spec/openphoto_spec.rb
|
113
|
+
- spec/settings.yml.example
|