hyperpublic 0.1.0

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.
@@ -0,0 +1,27 @@
1
+ module Hyperpublic
2
+ class Things < Base
3
+ extend Forwardable
4
+ def_delegators :client, :get, :post, :post_multi, :put, :delete
5
+
6
+ attr_reader :client
7
+
8
+ def initialize(client)
9
+ @client = client
10
+ end
11
+
12
+ def find(params)
13
+ if params.is_a? String
14
+ perform_get("/things/#{params}")
15
+ else
16
+ q = Addressable::URI.new
17
+ q.query_values = stringify(params)
18
+ perform_get("/things?#{q.query}")
19
+ end
20
+ end
21
+
22
+ def create(options={})
23
+ perform_post("/things", :body => options)
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module Hyperpublic
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,148 @@
1
+ require "forwardable"
2
+ require "hashie"
3
+ require "httparty"
4
+ require "multi_json"
5
+ require 'openssl'
6
+
7
+ module OpenSSL
8
+ module SSL
9
+ remove_const :VERIFY_PEER
10
+ end
11
+ end
12
+ OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
13
+
14
+ module Hyperpublic
15
+ include HTTParty
16
+
17
+ class HyperpublicError < StandardError
18
+ attr_reader :data
19
+
20
+ def initialize(data)
21
+ @data = data
22
+ super
23
+ end
24
+ end
25
+
26
+ class Unauthorized < HyperpublicError; end
27
+ class General < HyperpublicError; end
28
+
29
+ class BadRequest < StandardError; end
30
+ class Unavailable < StandardError; end
31
+ class NotFound < StandardError; end
32
+
33
+ def self.user_agent
34
+ @user_agent ||= 'Ruby Hyperpublic Gem'
35
+ end
36
+
37
+ def self.user_agent=(value)
38
+ @user_agent = value
39
+ end
40
+
41
+ def self.api_endpoint
42
+ @api_endpoint ||= "https://api.hyperpublic.com/api/v#{self.api_version}"
43
+ end
44
+
45
+ def self.api_endpoint=(value)
46
+ @api_endpoint = value
47
+ end
48
+
49
+ def self.api_version
50
+ @api_version ||= "1"
51
+ end
52
+
53
+ def self.api_version=(value)
54
+ @api_version = value
55
+ end
56
+
57
+ private
58
+
59
+ def self.perform_get(uri, options = {})
60
+ base_uri self.api_endpoint
61
+ make_friendly(get(uri, options))
62
+ end
63
+
64
+ def self.make_friendly(response)
65
+ raise_errors(response)
66
+ data = parse(response)
67
+ # Don't mash arrays of integers
68
+ if data && data.is_a?(Array) && data.first.is_a?(Integer)
69
+ data
70
+ else
71
+ mash(data)
72
+ end
73
+ end
74
+
75
+ def self.raise_errors(response)
76
+ case response.code.to_i
77
+ when 400
78
+ data = parse(response)
79
+ raise BadRequest.new(data), "(#{response.code}): #{response.message} - #{data['error'] if data}"
80
+ when 401
81
+ data = parse(response)
82
+ raise Unauthorized.new(data), "(#{response.code}): #{response.message} - #{data['error'] if data}"
83
+ when 403
84
+ data = parse(response)
85
+ raise General.new(data), "(#{response.code}): #{response.message} - #{data['error'] if data}"
86
+ when 404
87
+ data = parse(response)
88
+ raise NotFound, "(#{response.code}): #{response.message}" +
89
+ (data ? " - " + data['error'] : "")
90
+ when 500..503
91
+ raise Unavailable, "(#{response.code}): #{response.message}"
92
+ end
93
+ end
94
+
95
+ def self.parse(response)
96
+ case response.body
97
+ when ''
98
+ nil
99
+ when 'true'
100
+ true
101
+ when 'false'
102
+ false
103
+ else
104
+ MultiJson.decode(response.body)
105
+ end
106
+ end
107
+
108
+ def self.mash(obj)
109
+ if obj.is_a?(Array)
110
+ obj.map{|item| Hashie::Mash.new(item)}
111
+ elsif obj.is_a?(Hash)
112
+ Hashie::Mash.new(obj)
113
+ else
114
+ obj
115
+ end
116
+ end
117
+ end
118
+
119
+ module Hashie
120
+ class Mash
121
+
122
+ # Converts all of the keys to strings, optionally formatting key name
123
+ def rubyify_keys!
124
+ keys.each{|k|
125
+ v = delete(k)
126
+ new_key = k.to_s.underscore
127
+ self[new_key] = v
128
+ v.rubyify_keys! if v.is_a?(Hash)
129
+ v.each{|p| p.rubyify_keys! if p.is_a?(Hash)} if v.is_a?(Array)
130
+ }
131
+ self
132
+ end
133
+
134
+ end
135
+ end
136
+
137
+ directory = File.expand_path(File.dirname(__FILE__))
138
+
139
+ require File.join(directory, "hyperpublic", "oauth")
140
+ require File.join(directory, "hyperpublic", "request")
141
+ require File.join(directory, "hyperpublic", "base")
142
+ require File.join(directory, "hyperpublic", "offers")
143
+ require File.join(directory, "hyperpublic", "people")
144
+ require File.join(directory, "hyperpublic", "all")
145
+ require File.join(directory, "hyperpublic", "things")
146
+ require File.join(directory, "hyperpublic", "places")
147
+ require File.join(directory, "hyperpublic", "categories")
148
+ require File.join(directory, "hyperpublic", "version")
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hyperpublic::Base do
4
+ context "base" do
5
+ before do
6
+ @auth = Hyperpublic::OAuth.new("username", "password")
7
+ @hyperpublic = Hyperpublic::Base.new(@auth)
8
+ end
9
+
10
+ it "should delegate get to the client" do
11
+ @auth.should_receive(:get).with("/foo")
12
+ @hyperpublic.get("/foo")
13
+ end
14
+
15
+ it "should delegate post to the client" do
16
+ @auth.should_receive(:post).with("/foo", {:bar => "baz"}).and_return(nil)
17
+ @hyperpublic.post("/foo", {:bar => "baz"})
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hyperpublic::People do
4
+ context "base" do
5
+ before do
6
+ @auth = Hyperpublic::OAuth.new("username", "password")
7
+ @hp_people = Hyperpublic::People.new(@auth)
8
+ end
9
+
10
+ it "should be able to get person" do
11
+ stub_get(@auth, "people/1?",
12
+ '{"all_tags":[],"locations":[],"id":"1",
13
+ "image":{"src_thumb":"http://some_url",
14
+ "id":1,"src_large":"http://some_url"}}')
15
+ person = @hp_people.find("1")
16
+ person.id.should == "1"
17
+ end
18
+
19
+ it "should be able to get a list of people by ids" do
20
+ stub_get(@auth, "people?ids[0]=1&ids[1]=2",
21
+ '[{"id":1}, {"id":2}]')
22
+ people = @hp_people.find({:ids => [1, 2]})
23
+ people.count.should == 2
24
+ end
25
+
26
+ it "should be able to get a list of people by tags" do
27
+ stub_get(@auth, "people?tags[0]=tag1&tags[1]=tag2",
28
+ '[{"id":1}, {"id":2}]')
29
+ people = @hp_people.find({:tags => ["tag1", "tag2"]})
30
+ people.count.should == 2
31
+ end
32
+
33
+ it "should be able to get a list of people by location" do
34
+ stub_get(@auth, "people?location=10012",
35
+ '[{"id":1}, {"id":2}]')
36
+ people = @hp_people.find({:location => 10012})
37
+ people.count.should == 2
38
+ end
39
+
40
+ #TODO: finish this
41
+ it "should be able to create a user" do
42
+ stub_post(@auth, "people", '{"email":"etang@hyperpublic.com"}')
43
+ person = @hp_people.create({:email => "etang@hyperpublic.com", :image_url => "www.hyperpublic.com/image/2832.jpg"})
44
+ person.email.should == "etang@hyperpublic.com"
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hyperpublic::Places do
4
+ context "places" do
5
+ before do
6
+ @auth = Hyperpublic::OAuth.new("username", "password")
7
+ @hp_places = Hyperpublic::Places.new(@auth)
8
+ end
9
+
10
+ it "should be able to get a place" do
11
+ stub_get(@auth, "places/1?", '{"id":"1", "name":"hyperpublic"}')
12
+ place = @hp_places.find("1")
13
+ place.id.should == "1"
14
+ end
15
+
16
+ it "should be able to get a list of place by ids" do
17
+ stub_get(@auth, "places?ids[0]=1&ids[1]=2", '[{"id":1},{"id":2}]')
18
+ places = @hp_places.find(:ids=>[1,2])
19
+ places.count.should == 2
20
+ end
21
+
22
+ it "should be able to get a list of places by tags" do
23
+ stub_get(@auth, "places?tags[0]=tag1&tags[1]=tag2", '[{"id":1},{"id":2}]')
24
+ places = @hp_places.find(:tags=>["tag1", "tag2"])
25
+ places.count.should == 2
26
+ end
27
+
28
+ it "should be able to get a list of places by location" do
29
+ stub_get(@auth, "places?location=10012", '[{"id":1}, {"id":2}]')
30
+ places = @hp_places.find(:location => 10012)
31
+ places.count.should == 2
32
+ end
33
+
34
+ it "should be able create a place" do
35
+ stub_post(@auth, "places?", '{"user_id":1, "name":"hyperpublic"}')
36
+ place = @hp_places.create({:user_id => 1, :name => "hyperpublic"})
37
+ place.name.should == "hyperpublic"
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hyperpublic::Request do
4
+ context "new get request" do
5
+ before do
6
+ @client = mock('hyperpublic client')
7
+ @request = Hyperpublic::Request.new(@client, :get, '/people', {:query => {:tags => "tag1"}})
8
+ end
9
+
10
+ it "should have a client" do
11
+ @request.client.should == @client
12
+ end
13
+
14
+ it "should have method" do
15
+ @request.method == :get
16
+ end
17
+
18
+ it "should have path" do
19
+ @request.path.should == "/people"
20
+ end
21
+
22
+ it "should have options" do
23
+ @request.options[:query][:tags].should == "tag1"
24
+ end
25
+
26
+ it "should have uri" do
27
+ @request.uri.should == "/people?tags=tag1"
28
+ end
29
+
30
+ context "performing request for collection" do
31
+ before do
32
+ response = mock('response') do
33
+
34
+ end
35
+ end
36
+ end
37
+
38
+ #TODO:finish error test cases
39
+ context "error raising" do
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hyperpublic::Things do
4
+ context "things" do
5
+ before do
6
+ @auth = Hyperpublic::OAuth.new("client_id", "client_secret")
7
+ @hp_things = Hyperpublic::Things.new(@auth)
8
+ end
9
+
10
+ it "should be able to get a thing" do
11
+ stub_get(@auth, "things/1?",
12
+ '{"id":"1", "label":"a thing"}')
13
+ thing = @hp_things.find("1")
14
+ thing.id.should == "1"
15
+ end
16
+
17
+ it "should be able to get a list of things by ids" do
18
+ stub_get(@auth, "things?ids[0]=1&ids[1]=2",
19
+ '[{"id":1},{"id":2}]')
20
+ things = @hp_things.find({:ids => [1,2]})
21
+ things.count.should == 2
22
+ end
23
+
24
+ it "should be able to get a list of things by tags" do
25
+ stub_get(@auth, "things?tags[0]=tag1&tags[1]=tag2",
26
+ '[{"id":1}, {"id":2}]')
27
+ things = @hp_things.find({:tags => ["tag1", "tag2"]})
28
+ things.count.should == 2
29
+ end
30
+
31
+ it "should be able to get a list of things by location" do
32
+ stub_get(@auth, "things?location=10012",
33
+ '[{"id":1}, {"id":2}]')
34
+ things = @hp_things.find({:location => 10012})
35
+ things.count.should == 2
36
+ end
37
+
38
+ it "should be able to create a thing" do
39
+ stub_post(@auth, "things?", '{"user_id":1, "title":"my fave"}')
40
+ person = @hp_things.create({:title=>"my fave", :image_url => "www.hyperpublic.com/image/2832.jpg"})
41
+ person.title.should == "my fave"
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Hyperpublic do
4
+ describe ".user_agent" do
5
+ it "should default User Agent to 'Ruby Hyperpublic Gem'" do
6
+ Hyperpublic.user_agent.should == 'Ruby Hyperpublic Gem'
7
+ end
8
+ end
9
+
10
+ context 'when overriding the user agent' do
11
+ describe '.user_agent' do
12
+ it 'should be able to specify the User Agent' do
13
+ Hyperpublic.user_agent = 'My Hyperpublic Gem'
14
+ Hyperpublic.user_agent.should == 'My Hyperpublic Gem'
15
+ end
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,42 @@
1
+ #$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ #$LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require "pathname"
5
+ #require "shoulda"
6
+ #require "mocha"
7
+ require 'fakeweb'
8
+ require 'hyperpublic'
9
+
10
+ FakeWeb.allow_net_connect = false
11
+
12
+ # Requires supporting files with custom matchers and macros, etc,
13
+ # in ./support/ and its subdirectories.
14
+ Dir["#{File.dirname(__FILE__)}/spec/support/**/*.rb"].each {|f| require f}
15
+
16
+ RSpec.configure do |config|
17
+ config.mock_with :rspec
18
+
19
+ end
20
+
21
+ def hyperpublic_url(auth, url, req)
22
+ url =~ /^http/ ? url : "https://api.hyperpublic.com/api/v1/#{(url[-1,1] == "?" || req != "get") ? url : url+'&' }" + ((req == "get") ? "client_id=#{auth.ctoken}&client_secret=#{auth.csecret}" : "")
23
+ end
24
+
25
+ def stub_get(auth, url, result, status=nil)
26
+ options = {:body => result}
27
+ options.merge!({:status => status}) unless status.nil?
28
+ FakeWeb.register_uri(:get, hyperpublic_url(auth, url, "get"), options)
29
+ end
30
+
31
+ def stub_post(auth, url, result)
32
+ FakeWeb.register_uri(:post, hyperpublic_url(auth, url, "post"), :body => result)
33
+ end
34
+
35
+ def stub_put(auth, url, result)
36
+ FakeWeb.register_uri(:put, hyperpublic_url(auth, url, "put"), :body => result)
37
+ end
38
+
39
+ def stub_delete(auth, url, result)
40
+ FakeWeb.register_uri(:delete, hyperpublic_url(auth, url, "delete"), :body => result)
41
+ end
42
+