Class: Socialcastr::API

Inherits:
Object
  • Object
show all
Defined in:
lib/socialcastr/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, domain, format = "xml", debug = false) ⇒ API

Returns a new instance of API.



12
13
14
15
16
17
18
19
20
# File 'lib/socialcastr/api.rb', line 12

def initialize(username, password, domain, format="xml",debug=false)
  @debug    = debug
  @username = username
  @password = password
  @format   = format
  @domain   = domain
  @endpoint = "https://#{domain}/api/"
  return self
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



10
11
12
# File 'lib/socialcastr/api.rb', line 10

def debug
  @debug
end

Instance Method Details

#build_query_string(path, query = nil) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/socialcastr/api.rb', line 120

def build_query_string(path, query=nil)
  params = []
  unless query.nil?
    params = query.collect do |k,v| 
      "#{k.to_s}=#{CGI::escape(v.to_s)}" 
    end
  end
  "/api#{path.to_s =~ /^\// ? path.to_s : "/" + path.to_s }.#{@format}" + (params.any? ? "?" + params.join('&') : "")
end

#delete(path, args = {}) ⇒ Object



40
41
42
# File 'lib/socialcastr/api.rb', line 40

def delete(path, args={})
  https_request(:delete, path, args)
end

#get(path, args = {}) ⇒ Object



28
29
30
# File 'lib/socialcastr/api.rb', line 28

def get(path, args={})
  https_request(:get, path, args)
end

#handle_response(response) ⇒ Object

Handles response and error codes from the remote service.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/socialcastr/api.rb', line 78

def handle_response(response)
  case response.code.to_i
    when 301,302
      raise(Redirection.new(response))
    when 200...400
      response
    when 400
      raise(BadRequest.new(response))
    when 401
      raise(UnauthorizedAccess.new(response))
    when 403
      raise(ForbiddenAccess.new(response))
    when 404
      raise(ResourceNotFound.new(response))
    when 405
      raise(MethodNotAllowed.new(response))
    when 409
      raise(ResourceConflict.new(response))
    when 410
      raise(ResourceGone.new(response))
    when 422
      raise(ResourceInvalid.new(response))
    when 401...500
      raise(ClientError.new(response))
    when 500...600
      raise(ServerError.new(response))
    else
      raise(ConnectionError.new(response, "Unknown response code: #{response.code}"))
  end
end

#https_request(method, path, args) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/socialcastr/api.rb', line 44

def https_request(method, path, args)
  https = setup_https

  case method
    when :get
      request_class = Net::HTTP::Get
      query=args
    when :post
      request_class = Net::HTTP::Post
      form_data = args
    when :put
      request_class = Net::HTTP::Put
      form_data = args
    when :delete
      request_class = Net::HTTP::Delete
    else
      raise InvalidMethod
  end
  response = nil
  https.start do |session|
    query_string = build_query_string(path, query)
    req = request_class.new(query_string)
    req.basic_auth @username, @password
    if form_data
      req.set_form_data(args, ';')
    end
    response = session.request(req)
  end

  return handle_response(response).body
end

#post(path, args = {}) ⇒ Object



36
37
38
# File 'lib/socialcastr/api.rb', line 36

def post(path, args={})
  https_request(:post, path, args)
end

#profileObject



22
23
24
25
26
# File 'lib/socialcastr/api.rb', line 22

def profile
  @profile ||= Socialcastr::Community.parse(post("authentication", {:email => @username, :password => @password })).
                  select { |community| community.domain == @domain }.
                  first.profile
end

#put(path, args = {}) ⇒ Object



32
33
34
# File 'lib/socialcastr/api.rb', line 32

def put(path, args={})
  https_request(:put, path, args)
end

#setup_httpsObject



109
110
111
112
113
114
115
116
117
118
# File 'lib/socialcastr/api.rb', line 109

def setup_https
  url   = URI.parse(@endpoint) 
  https = Net::HTTP.new(url.host, url.port)
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
  if @debug
    https.set_debug_output $stderr
  end
  https.use_ssl = true
  return https
end