Code Monkey home page Code Monkey logo

openphoto-ruby's People

Contributors

beans0063 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

openphoto-ruby's Issues

Method GET and params

Is it normal that, as opposed to POST, the GET method doesn’t take parameters in the params hash?

client.connect( :get, '/hello.json', { 'foo' => 'bar' } ).json
# => {"message"=>"Hello, world!", "code"=>200, "result"=>{"__route__"=>"/hello.json"}} 

client.connect( :get, '/hello.json?foo=bar' ).json
# => {"message"=>"Hello, world!", "code"=>200, "result"=>{"__route__"=>"/hello.json", "foo"=>"bar"}}

Invalid JSON response when running example code

I'm running the example code, having defined @site as "http://trovebox.com" and all other instance variables as required. I have an account on trovebox and generated all tokens.

require 'rubygems'
require 'openphoto-ruby'

@client = Openphoto::Client.new(@site, @consumer_key, @consumer_secret, @access_token, @access_token_secret)
photo = @client.connect(:post, "/photo/upload.json", {"photo"=>"/tmp/photo.png", "permission"=>"1", "title"=>"test upload"})
puts "#{photo.message} #{@site}/photo/#{photo.result["id"]}/view"

I get the following error:

[rekado@mango openphoto-ruby]$ bundle exec ruby test-upload.rb 
/home/rekado/dev/openphoto-ruby/vendor/ruby/1.9.1/gems/json-1.7.6/lib/json/common.rb:155:in `initialize': A JSON text must at least contain two octets! (JSON::ParserError)
    from /home/rekado/dev/openphoto-ruby/vendor/ruby/1.9.1/gems/json-1.7.6/lib/json/common.rb:155:in `new'
    from /home/rekado/dev/openphoto-ruby/vendor/ruby/1.9.1/gems/json-1.7.6/lib/json/common.rb:155:in `parse'
    from /home/rekado/dev/openphoto-ruby/lib/openphoto-ruby.rb:50:in `json'
    from /home/rekado/dev/openphoto-ruby/lib/openphoto-ruby.rb:54:in `result'
    from test-upload.rb:25:in `<main>'

It appears that the result is empty, so the parse fails. Are there any obvious problems with the code?

Use multipart post for upload

The library currently encodes the image inline as base64 in the post data.
It would be better to send a multipart/related request instead.

One way to do it would be to add an upload method, eg:

module Openphoto
  require 'net/http/post/multipart'

  class Client
    def upload photo, params = {}
      params["photo"] = UploadIO.new(File.open(photo), "image/jpeg", File.basename(photo))
      access_token = prepare_access_token
      request = Net::HTTP::Post::Multipart.new('/photo/upload.json', params)
      access_token.sign! request
      url = URI.parse @hostname
      response = Net::HTTP.start(url.host, url.port, :use_ssl => true) do |http|
        http.request request
      end
      response.extend Openphoto::Response
      response
    end
  end
end

(used in https://github.com/jo/trovebox-uploader)

One could also enhance the connect method.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.