Parent

Wonderfl::Client

Attributes

api_key[RW]

(Not documented)

Public Class Methods

new(api_key = nil, options = {}) click to toggle source

Requires api key, get yours from your account page (wonderfl.net/account/api_keys/create)

# File lib/wonderfl/client.rb, line 9
    def initialize(api_key = nil, options = {})
      @api_key = api_key
      @cache = Wonderfl::Utils::Cache.new(options[:cache_file],
                                          options[:expire])
    end

Public Instance Methods

get_code(code_id) click to toggle source

Returns a Wonderfl::Code object detailing the code information.

# File lib/wonderfl/client.rb, line 48
    def get_code(code_id)
      _validate?(code_id)
      path = '/code/' + code_id
      @cache.transaction do
        data = @cache[path]
        if data.nil?
          @cache[path] = Code.new(_request(path)[:body]['code'], code_id)
        else
          data
        end
      end
    end
get_code_forks(code_id) click to toggle source

Returns a Wonderfl::CodeForks object (Wonderfl::Code collection) detailing the recent 50 codes information forked from specified code.

# File lib/wonderfl/client.rb, line 64
    def get_code_forks(code_id)
      _validate?(code_id)
      path = '/code/' + code_id  + '/forks'
      @cache.transaction do
        data = @cache[path]
        if data.nil?
          @cache[path] = CodeForks.new(_request(path)[:body]['forks'])
        else
          data
        end
      end
    end
get_user(user_name) click to toggle source

Returns a Wonderfl::User object detailing the user information.

# File lib/wonderfl/client.rb, line 17
    def get_user(user_name)
      _validate?(user_name)
      path = '/user/' + user_name
      @cache.transaction do
        data = @cache[path]
        if data.nil?
          @cache[path] = User.new(_request(path)[:body]['user'])
        else
          data          
        end
      end
    end
get_user_codes(user_name) click to toggle source

Returns a Wonderfl::UserCodes object (Wonderfl::Code collection) detailing the recent 20 codes information posted by specified user.

# File lib/wonderfl/client.rb, line 33
    def get_user_codes(user_name)
      _validate?(user_name)
      path = '/user/' + user_name + '/codes'
      @cache.transaction do
        data = @cache[path]
        if data.nil?
          @cache[path] = UserCodes.new(_request(path)[:body]['codes'])
        else
          data
        end
      end
    end

Private Instance Methods

_request(path) click to toggle source

Private methods.

# File lib/wonderfl/client.rb, line 80
    def _request(path)
      raise BadRequest, "api_key required" if @api_key.nil?
      url = BASE_URL + path + '?api_key=' + @api_key
      begin
        response = Net::HTTP.get_response(URI.parse(url))
        raise InternalServerError unless response.is_a? Net::HTTPOK
        parsed = JSON.parse(response.body)
      rescue JSON::ParserError => e
        raise InternalServerError, 'JSON parse failed'
      end
      # dirty.. (cannot handle the HTTP Status Code)
      # see also => http://wonderfl.net/apis/methods
      if parsed['stat'].include? 'fail'
        case parsed['message']
        when 'invalid api key'
          raise Unauthorized, "Request failed [#{parsed['message']}]"
        when 'not found'
          raise NotFound, "Request failed [#{parsed['message']}]"
        end
      end
      { :code => response.code, :body => parsed }
    end
_validate?(arg) click to toggle source

(Not documented)

# File lib/wonderfl/client.rb, line 103
    def _validate?(arg)
      return true if arg.is_a? String
      method_name = caller.first.scan(/`(.*)'/).to_s
      raise BadRequest, 'String argument expected (' + method_name + ')'
    end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.