Permalink
Browse files

Ability to create an image.

  • Loading branch information...
1 parent e5a5c2d commit f4eb5b211ad5f05aad2bf2415cb898088ddba06a @waratuman waratuman committed Aug 7, 2012
Showing with 181 additions and 22 deletions.
  1. +14 −10 lib/mls.rb
  2. +1 −0 lib/mls/errors.rb
  3. +2 −2 lib/mls/models/listing.rb
  4. +25 −5 lib/mls/models/photo.rb
  5. +2 −1 mls.gemspec
  6. +0 −1 test/test_helper.rb
  7. +136 −3 test/units/models/test_photo.rb
  8. +1 −0 test/units/test_mls.rb
View
24 lib/mls.rb
@@ -31,12 +31,9 @@ class MLS
attr_accessor :url, :api_key, :auth_key, :logger, :asset_host
def url=(uri)
- @url = uri
-
- uri = URI.parse(uri)
- @api_key = CGI.unescape(uri.user)
- @host = uri.host
- @port = uri.port
+ @url = URI.parse(uri)
+ @api_key = CGI.unescape(@url.user)
+ @host, @port = @url.host, @url.port
end
def logger
@@ -51,11 +48,18 @@ def asset_host
@asset_host ||= get('/asset_host').body
end
+ def headers
+ h = {
+ 'Content-Type' => 'application/json',
+ 'X-42Floors-API-Version' => API_VERSION,
+ 'X-42Floors-API-Key' => api_key
+ }
+ h['X-42Floors-API-Auth-Key'] = auth_key if auth_key
+ h
+ end
+
def add_headers(req)
- req['Content-Type'] = 'application/json'
- req['X-42Floors-API-Version'] = API_VERSION
- req['X-42Floors-API-Key'] = api_key
- req['X-42Floors-API-Auth-Key'] = auth_key if auth_key
+ headers.each { |k, v| req[k] = v }
end
def put(url, body={})
View
1 lib/mls/errors.rb
@@ -15,3 +15,4 @@ class MLS::ApiVersionUnsupported < MLS::Exception
class MLS::RecordInvalid < MLS::Exception
end
+
View
4 lib/mls/models/listing.rb
@@ -62,7 +62,7 @@ class MLS::Listing < MLS::Resource
property :updated_at, DateTime
- attr_accessor :address, :agents, :account, :photos
+ attr_accessor :address, :agents, :account, :photos, :address_attributes, :agents_attributes
def sublease?
kind == 'sublease'
@@ -105,7 +105,7 @@ def find(id)
class MLS::Listing::Parser < MLS::Parser
def photos=(photos)
- @object.photos = photos.map {|d| MLS::Photo.new(d) }
+ @object.photos = photos.map {|d| MLS::Photo.new(:digest => d) }
end
def address=(address)
View
30 lib/mls/models/photo.rb
@@ -1,13 +1,33 @@
-class MLS::Photo < MLS::Resource
+require 'restclient'
- attr_accessor :digest
+class MLS::Photo < MLS::Resource
- def initialize(digest)
- @digest = digest
- end
+ property :id, Fixnum
+ property :digest, String
+ property :created_at, DateTime
+ property :updated_at, DateTime
+ property :file_content_type, String
+ property :file_name, String
+ property :file_size, Fixnum
+ property :url_template, String
def url(style='large', protocol='http')
"#{protocol}://#{MLS.asset_host}/photos/#{style}/#{@digest}.jpg"
end
+ def self.create(image_file)
+ image_file.rewind
+ url = MLS.url.dup
+ url.user = nil
+ url.path = "/api/photos"
+ response = RestClient.post(url.to_s, {:file => image_file}, MLS.headers)
+ image_file.close
+
+ MLS::Photo::Parser.parse(response.body)
+ end
+
+end
+
+class MLS::Photo::Parser < MLS::Parser
+
end
View
3 mls.gemspec
@@ -29,5 +29,6 @@ Gem::Specification.new do |s|
# Runtime
s.add_runtime_dependency 'activesupport'
s.add_runtime_dependency 'yajl-ruby'
- s.add_runtime_dependency 'simplecov'
+ s.add_runtime_dependency 'rest-client'
+
end
View
1 test/test_helper.rb
@@ -12,4 +12,3 @@
puts FactoryGirl.find_definitions
-MLS.url = 'http://xSgYUhc1XckzB6glk598XGVDPnGtyLQKItKXgWfP5xcwwjD9XY7O9dWFjVblZA%3D%3D@mls.dev'
View
139 test/units/models/test_photo.rb
@@ -2,10 +2,143 @@
class TestPhoto < ::Test::Unit::TestCase
- def test_instance_methods
- photo = MLS::Photo.new({})
- assert photo.respond_to?(:url)
+ def test_create_photo
+ photo = MLS::Photo.create(TEST_IMAGE)
+ assert photo.id
+ assert photo.digest
end
end
+
+TEST_IMAGE = StringIO.new(Base64.decode64(<<PHOTO))
+/9j/4AAQSkZJRgABAQEASABIAAD/4gxYSUNDX1BST0ZJTEUAAQEAAAxITGlu
+bwIQ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
+GpaJ2DZ+6OlNCNK1mMlv9mcgM/G7HSoXi8tyskigg4IHNM0/m4jLE9ck0jMW
+csTyTmotZlva47MY6KW/3jS+a3RcKP8AZGKjozTJFPPJOaKTNJmgBaKTNFMC
+DzW9qPNajym9qTy29KrQWou9/T9KQs7dV/SnKHXoop2X/uj86AI/JY9wKVY2
+B++afl/7o/OmtI4/gpagWbc4lY9whx+VMplvMokUkDPfNWrqNAfMhIMZ/Q1N
+rFXuQUlFFAgozS0UAGaKKKYDe1LSZooAKKKKACiiigBCoPUClHFFFAC0tNpa
+QxaKSigBaKAM9xRTArBjS7zRRVEhvNG80UUALvNKHNFFIBQ9LuzRRQMN1Luo
+opALmjNFFAwzRRRQB//Z
+PHOTO
View
1 test/units/test_mls.rb
@@ -31,4 +31,5 @@ def test_attr_accessors
def test_class_methods
assert MLS.respond_to?(:parse)
end
+
end

0 comments on commit f4eb5b2

Please sign in to comment.