Module: Betterific::JsonClient

Extended by:
ClientHelpers
Includes:
ClientConstants
Defined in:
lib/betterific/json_client.rb

Constant Summary

Constants included from ClientConstants

ClientConstants::BASE_URL, ClientConstants::BETTERIFS_BASE_URL, ClientConstants::PROTO_PACKAGE_NAME, ClientConstants::SEARCH_BASE_URL, ClientConstants::TAGS_BASE_URL, ClientConstants::TMP_DIR, ClientConstants::USERS_BASE_URL

Class Method Summary collapse

Class Method Details

.betterifs(opts = {}) ⇒ Object

Get a list of betterifs.

Parameters

  • opts - If most_popular, gets the most popular betterifs of the last week.

    If most_recent, gets the most recent betterifs.

    => [id0, id1, …, idx] specifies the ids of the betterif(s) to return.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/betterific/json_client.rb', line 30

def self.betterifs(opts={})
  if [:most_popular, 'most_popular'].include?(opts) || (opts.is_a?(Hash) && [:most_popular, 'most_popular'].include?(opts[:filter]))
    return get_json("#{BETTERIFS_BASE_URL}/most-popular")
  elsif [:most_recent, 'most_recent'].include?(opts) || (opts.is_a?(Hash) && [:most_recent, 'most_recent'].include?(opts[:filter]))
    return get_json("#{BETTERIFS_BASE_URL}/most-recent")
  elsif opts[:ids]
    return get_json("#{BETTERIFS_BASE_URL}?betterifs[ids]=#{Array(opts[:ids]).map(&:to_s).join(',')}")
  else
    raise "No filter and no ids given."
  end
end

.get_json(url, opts = {}, url_params = {}) ⇒ Object

:nodoc:



9
10
11
12
13
14
15
16
# File 'lib/betterific/json_client.rb', line 9

def get_json(url, opts={}, url_params={}) #:nodoc:
  url = add_page_params(url, page_params_from_opts(opts))
  uri = URI(url)
  unless url_params.empty?
    uri.query = URI.encode_www_form(url_params)
  end
  Hashie::Mash.new(JSON.parse(get_http(uri).body))
end

.search(opts = {}) ⇒ Object

Search for betterifs, tags, and users.

Parameters

  • opts - => (all|betterifs|tags|users) specifies the type of object(s) to return.

    => <query> specifies the search query.



78
79
80
81
82
83
84
85
86
87
# File 'lib/betterific/json_client.rb', line 78

def self.search(opts={})
  raise "No namespace given." if opts[:namespace].nil?
  raise "No q given." if opts[:q].nil?
  raise "q is blank." if opts[:q].blank?
  if [:betterifs, 'betterifs', :tags, 'tags', :users, 'users', :all, 'all'].include?(opts[:namespace])
    return get_json("#{SEARCH_BASE_URL}/#{opts[:namespace]}?q=#{opts[:q]}")
  else
    raise "Invalid namespace: #{opts[:namespace]}"
  end
end

.tags(opts = {}) ⇒ Object

Get a list of tags.

Parameters

  • opts - => [id0, id1, …, idx] specifies the ids of the tag(s) to return.



48
49
50
51
52
53
54
# File 'lib/betterific/json_client.rb', line 48

def self.tags(opts={})
  if opts[:ids]
    return get_json("#{TAGS_BASE_URL}?tags[ids]=#{Array(opts[:ids]).map(&:to_s).join(',')}")
  else
    raise "No ids given."
  end
end

.users(opts = {}) ⇒ Object

Get a list of users.

Parameters

  • opts - => [id0, id1, …, idx] specifies the ids of the user(s) to return.



62
63
64
65
66
67
68
# File 'lib/betterific/json_client.rb', line 62

def self.users(opts={})
  if opts[:ids]
    return get_json("#{USERS_BASE_URL}?users[ids]=#{Array(opts[:ids]).map(&:to_s).join(',')}")
  else
    raise "No ids given."
  end
end